Rabin-Karp 滚动哈希字符串匹配算法

发布于 2024-12-21 01:39:05 字数 1572 浏览 0 评论 0原文

这是 Rabin-Karp 字符串匹配算法在 C# 中的实现...

static void Main(string[] args)
{
  string A = "String that contains a pattern.";
  string B = "pattern";
  ulong siga = 0;
  ulong sigb = 0;
  ulong Q = 100007;
  ulong D = 256;
for (int i = 0; i < B.Length; i++)
{
    siga = (siga * D + (ulong)A[i]) % Q;
    sigb = (sigb * D + (ulong)B[i]) % Q;
}
if (siga == sigb)
{
    Console.WriteLine(string.Format(">>{0}<<{1}", A.Substring(0, B.Length), A.Substring(B.Length)));
    return;
}
ulong pow = 1;
for (int k = 1; k <= B.Length - 1; k++)
    pow = (pow * D) % Q;

for (int j = 1; j <= A.Length - B.Length; j++)
{
    siga = (siga + Q - pow * (ulong)A[j - 1] %Q) % Q;
    siga = (siga * D + (ulong)A[j + B.Length - 1]) % Q;
    if (siga == sigb)
    {
        if (A.Substring(j, B.Length) == B)
        {
            Console.WriteLine(string.Format("{0}>>{1}<<{2}", A.Substring(0, j), 
                                                             A.Substring(j, B.Length), 
                                                             A.Substring(j +B.Length)));
            return;
        }
    }
}
Console.WriteLine("Not copied!");

}

但是如果我更改第二个字符串的位置,它会出现一个问题,它显示结果未复制,但

 string A = "String that contains a pattern.";
 string B = "pattern";

这里显示未复制

 string A = "String that contains a pattern.";
 string B = "Matches contains a pattern ";

我想检查它是否是复制从第一个字符串开始,甚至我会在其中添加一些内容或更改位置,但它应该没有什么区别,所以如何更改它,它只是比较字符串中每个单词的哈希值而不是实现它...... ......

Here is an implementation of Rabin-Karp String matching algorithm in C#...

static void Main(string[] args)
{
  string A = "String that contains a pattern.";
  string B = "pattern";
  ulong siga = 0;
  ulong sigb = 0;
  ulong Q = 100007;
  ulong D = 256;
for (int i = 0; i < B.Length; i++)
{
    siga = (siga * D + (ulong)A[i]) % Q;
    sigb = (sigb * D + (ulong)B[i]) % Q;
}
if (siga == sigb)
{
    Console.WriteLine(string.Format(">>{0}<<{1}", A.Substring(0, B.Length), A.Substring(B.Length)));
    return;
}
ulong pow = 1;
for (int k = 1; k <= B.Length - 1; k++)
    pow = (pow * D) % Q;

for (int j = 1; j <= A.Length - B.Length; j++)
{
    siga = (siga + Q - pow * (ulong)A[j - 1] %Q) % Q;
    siga = (siga * D + (ulong)A[j + B.Length - 1]) % Q;
    if (siga == sigb)
    {
        if (A.Substring(j, B.Length) == B)
        {
            Console.WriteLine(string.Format("{0}>>{1}<<{2}", A.Substring(0, j), 
                                                             A.Substring(j, B.Length), 
                                                             A.Substring(j +B.Length)));
            return;
        }
    }
}
Console.WriteLine("Not copied!");

}

but it has one problem if i change the position of second string than it shows result not copied but

 string A = "String that contains a pattern.";
 string B = "pattern";

here it shows not copied

 string A = "String that contains a pattern.";
 string B = "Matches contains a pattern ";

i want to check whether it is copy from first string or not even i would add something in it it or change the position but it shouldn't make difference so how to change it that it would just compare the hashes of each word in string than implement it............

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2024-12-28 01:39:05

更改

string B = "Matches contains a pattern ";

string B = "contains a pattern ";

,它将起作用

Change

string B = "Matches contains a pattern ";

to

string B = "contains a pattern ";

and it will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文