-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRabin-Karp.cpp
55 lines (50 loc) · 1012 Bytes
/
Rabin-Karp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void solve(string& pat, string& s)
{
n=pat.length();
__int128 match1=0,match2=0,match3=0;
for(long long i=1;i<=n;i++)
{
match1=(match1+ pat[i-1])%MOD;
match2=(match2+ i*pat[i-1])%MOD;
match3=(match3+ i*i*pat[i-1])%MOD;
}
vector<int>found;
__int128 hash1=0,hash2=0,hash3=0,l=1,y=n+1,z=((n+1)*(n+1))%MOD;
queue<char>slide;
for(int i=0;i<s.length();i++)
{
char c=s[i];
slide.push(c);
if(l<=n)
{
hash1=(hash1+c)%MOD;
hash2=(hash2+l*c)%MOD;
hash3=(hash3+l*l*c)%MOD;
}
if(l>n)
{
hash1=(hash1+c)%MOD;
hash2=(hash2+y*c)%MOD;
hash3=(hash3+z*c)%MOD;
}
if(l>=n)
{
if(l>n)
{
hash3=(hash3 + hash1 - (hash2*2) +MOD)%MOD;
hash2=(hash2 - hash1 + MOD)%MOD;
hash1=(hash1 -slide.front() + MOD)%MOD;
slide.pop();
}
if(hash1==match1 && hash2==match2 && hash3==match3) cout<<int(l-n)<<"\n";
}
l++;
}
if(found.size()>0)
{
for(auto x: found)
cout<<x<<"\n";
}
else
cout<<"\n";
}