-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rabin_Karp.cpp
76 lines (64 loc) · 1.03 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <math.h>
// #include <bits/stdc++.h>
using namespace std;
void robin_karp(string P, string T, int d, int q)
{
int n = T.length();
int m = P.length();
int h = 1;
for(int i=0;i<m-1;i++)
{
h = h*d % q;
}
h%=q;
int p = 0;
int t = 0;
int i=0;
for(int i=0;i<m;i++)
{
p = (d*p + P[i]) % q;
t = (d*t +T[i]) % q;
}
for(int s=0;s<(n-m);s++)
{
if(p==t)
{
bool valid = true;
for ( i = 0; i < m; ++i)
{
if(T[i+s]!=P[i])
{
//cout<<"breaking"<<endl;
break;
//valid = false;
}
}
if(i==m)
{
cout<<"Pattern found, occurs with shift: "<<s<<endl;
//break;
}
}
if(s<(n-m))
{
t = (d*(t-T[s]*h) + T[s+m])%q;
if(t<0)
t = t+q;
}
}
return;
}
int main()
{
int q = 101;
int d = 256;
string p = "a";
string t = "What is the meaning of life during this time that we spend on earth as we ponder the reason behind our existence";
robin_karp(p, t, d, q);
return 0;
}