罗宾·卡普(Robin carp)的实施未取得预期的结果

时间:2019-08-31 23:17:37

标签: c++ rabin-karp

使用1000000007进行哈希处理后,以下代码未提供所需的输出。

使用101进行哈希处理可以使其正常工作。 您能帮我找到我哪里出问题了吗?

#include<bits/stdc++.h>
using namespace std;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())

int val(char s)
{
        return int(s)-int('A')+1;
}
void robinkarp(string text,string compare,int q)
{
    int hashofcompare=0;
    int windowhash=0;
    int valchanger=1;
    for(int i=0;i<compare.size();i++)
    {
        hashofcompare=(hashofcompare*26+val(compare[i]))%q;
        windowhash=(windowhash*26+val(text[i]))%q;
        if(i!=0)
            valchanger=(valchanger*26)%q;
    }
    for(int i=0;i<=text.size()-compare.size();i++)
    {
        if(windowhash==hashofcompare)
        {
            int j=0;
            for(;j<compare.size();j++)
            {
                if(val(compare[j])!=val(text[i+j]))
                    break;
            }
            if(j==compare.size())
                cout<<"Found at:"<<i+1<<"\n";
        }
        if(i!=text.size()-compare.size())
            windowhash=(26*((windowhash-valchanger*(val(text[i])))%q)+val(text[i+compare.size()]))%q;
        windowhash=(windowhash+q)%q;
    }
}
int main()
{
    string text;
    cin>>text;
    string compare;
    cin>>compare;
    int prime=1000000009;
    cout<<prime;
    robinkarp(text,compare,prime);

}

没有显示输入案例:eeeeeeeeeeeeeeeeeeeeee和eeeeeeeeeeeeee的输出。

0 个答案:

没有答案