这是我想出的
var matches =
(from l in userSelect.Select((s, i) => new { s, i })
join r in original.Select((s, i) => new { s, i })
on l.s equals r.s
group l by r.i - l.i into g
from m in g.Select((l, j) => new { l.i, j = l.i - j, k = g.Key })
group m by new { m.j, m.k } into h
select h.Select(t => t.i).ToArray())
.ToArray();
这将输出
matches[0] // { 0, 1, 2 } the quick brown
matches[1] // { 4, 5 } jumps over
matches[2] // { 0 } the
matches[3] // { 3 } dog
使用输入{"the", "quick", "brown", "the", "lazy", "dog"}
yields:
matches[0] // { 0, 1, 2 } the quick brown
matches[1] // { 0 } the
matches[2] // { 3 } the
matches[3] // { 3, 4, 5 } the lazy dog
请注意,调用ToArray
是可选的。如果您实际上不需要数组中的结果,则可以将其省略并节省一些处理时间。
过滤掉任何序列完全包含对于其他更大的序列,您可以运行此代码(请注意orderby
在修改后的查询中):
var matches =
(from l in userSelect.Select((s, i) => new { s, i })
join r in original.Select((s, i) => new { s, i })
on l.s equals r.s
group l by r.i - l.i into g
from m in g.Select((l, j) => new { l.i, j = l.i - j, k = g.Key })
group m by new { m.j, m.k } into h
orderby h.Count() descending
select h.Select(t => t.i).ToArray());
int take = 0;
var filtered = matches.Where(m => !matches.Take(take++)
.Any(n => m.All(i => n.Contains(i))))
.ToArray();