不确定这在性能方面有多好,但我构建了一个逻辑来实现您想要的。任何人都可以进行调整以使其更清洁。
List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF", "AA" };
var result = ProcessChatLog(history,potentialNew);
//pass these two list to a function to process the chat log
核心逻辑在这里。
public List<string> ProcessChatLog(List<string> history, List<string> potentialNew)
{
var lastChat = history.Last();
var lastChatIndex = history.Count - 1;
var allIndexWithLastChat = potentialNew.Select((c, i) => new { chat = c, Index = i })
.Where(x => x.chat == lastChat)
.Select(x => x.Index).Reverse().ToList();
List<int> IndexToClear = new List<int>();
bool overlapFound = false;
foreach (var index in allIndexWithLastChat)
{
if (!overlapFound)
{
int hitoryChatIndex = lastChatIndex;
IndexToClear.Clear();
for (int i = index; i > -1; i--)
{
if (potentialNew[i] == history[hitoryChatIndex])
{
IndexToClear.Add(i);
if (i == 0)
{
overlapFound = true;
break;
}
hitoryChatIndex--;
}
else
{
break;
}
}
}
else
{
IndexToClear.Clear();
break;
}
}
if(IndexToClear.Count >0)
{
potentialNew.RemoveRange(IndexToClear.Min(), IndexToClear.Count);
}
return history.Concat(potentialNew).ToList();
}
这是一些结果
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB","AA", "CC", "AA" }
potentialNew = { "AA","CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","AA","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "CC", "AA", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "CC", "AA", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = {"AA", "CC", "AA", "DD", "EE", "FF", "AA" }
Result = { "AA", "BB","CC", "AA", "CC", "AA", "DD", "EE", "FF", "AA" }
如果这有帮助,请告诉我。
但我仍然说这不是您想要的理想输出。因为如果聊天包含相同的消息 20 次,并假设您在 2 个列表中分别包含 11 和 9 个项目,该怎么办。现在根据您想要的输出,您将省略所有 9 条消息新列表作为可能的重复和问题。所以我说不是这个修复,解决方案是跟踪聊天日志中传递的消息,并采取措施不在下一个日志中传递这些消息。这样可以保持逻辑和准确性