我正在使用Rabin-Karp算法检查任意两个源代码文件的抄袭情况。首先,我用C#实现了它的算法,以下是它的代码。但是,它的平均和最佳情况的运行时间为O(n+m),在空间上为O(p),但是其最坏情况的时间复杂度为O(nm)。
public void plagiarism(string [] file1, string [] file2)
{
int percent = 0;
for (int i = 0; i <(file1.Length - file2.Length +1); i++)
{
for (int j = 0; j < file1.Length; j++)
{
if (file1[i + j - 1] != file2[j])
{
}
percent++;
Console.WriteLine(percent);
}
Console.WriteLine("not copied");
}
}
那么如何通过使用滚动哈希函数使其更加高效呢?因为这比现有方法更好。