我有一个字节数组,我想,以确定此字节数组的内容的另一大数组作为一个连续的序列中存在。什么是去这样做的最简单的方法是什么?
I have an array of bytes and i want to determine if the contents of this array of bytes exists within another larger array as a continuous sequence. What is the simplest way to go about doing this?
天真的做法是:
public static bool IsSubsetOf(byte[] set, byte[] subset) {
for(int i = 0; i < set.Length && i + subset.Length <= set.Length; ++i)
if (set.Skip(i).Take(subset.Length).SequenceEqual(subset))
return true;
return false;
}
有关更高效的方法,你可能会考虑更高级的字符串匹配算法,如 KMP 。
For more efficient approaches, you might consider more advanced string matching algorithms like KMP.