我会假设你根据你链接的例子得到你的结果。
StringSearchResult[] results = searchAlg.FindAll(textToSearch);
有了这些results
,如果您假设唯一的重叠是子集,您可以按索引排序并一次性收集您想要的结果。
public class SearchResultComparer : IComparer<StringSearchResult> {
public int StringSearchResult(StringSearchResult x, StringSearchResult y)
{
// Try ordering by the start index.
int compare = x.Index.CompareTo(y.Index);
if (compare == 0)
{
// In case of ties, reverse order by keyword length.
compare = y.Keyword.Length.CompareTo(x.Keyword.Length);
}
return compare;
}
}
// ...
IComparer searchResultComparer = new SearchResultComparer();
Array.Sort(results, searchResultComparer);
int activeEndIndex = -1;
List<StringSearchResult> nonOverlappingResults = new List<StringSearchResult>();
foreach(StringSearchResult r in results)
{
if (r.Index < activeEndIndex)
{
// This range starts before the active range ends.
// Since it's an overlap, skip it.
continue;
}
// Save this result, track when it ends.
nonOverlappingResults.Add(r);
activeEndIndex = r.Index + r.Keyword.Length;
}
由于索引排序,循环保证只保留不重叠的范围。但是有些范围会被拒绝。发生这种情况只有两个原因。
- 候选从与活动范围相同的索引开始。由于排序打破了这些联系,所以最长的先行,候选必须比活动范围短并且可以跳过。
- 候选人在活动范围之后开始。由于唯一的重叠是子集,并且这与活动范围重叠,因此它是一个子集,开始较晚,但仍结束于或之前。
因此,唯一被拒绝的候选将是子集,并且必须在活动范围之前结束。所以活动范围仍然是唯一需要担心重叠的事情。