不确定这个性能如何,但我构建了一个逻辑来实现你想要的。任何人都可以对其进行微调,使其更加清晰。
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);
核心逻辑在这里。
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次,并且您分别获得了两个列表,其中一个包含11个项目,另一个包含9个项目。现在根据您想要的输出,您将省略所有9条消息的新列表作为可能的重复项,这是一个问题。所以我建议,与其修复此问题,不如跟踪传递到聊天日志中的消息,并采取措施不将它们传递到下一个日志中。这样可以保持逻辑和准确性。
"AA", "BB", "CC", "AA"
,而列表2为"CC", "AA", "DD", "EE", "FF", "AA","BB"
。 - sujith karivelil