-
Notifications
You must be signed in to change notification settings - Fork 342
/
Rabin_Karp_Algorithm.cpp
66 lines (58 loc) · 2.16 KB
/
Rabin_Karp_Algorithm.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
#include <iostream>
using namespace std;
// Using Rabin Karp Algorithm for Pattern Searching
// Rabin-Karp algorithm slides the pattern one by one.
// But unlike the Naive algorithm, Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of text,
// and if the hash values match then only it starts matching individual characters.
// The average and best case running time of the Rabin-Karp algorithm is O(n+m), but its worst-case time is O(nm).
// Worst case of Rabin-Karp algorithm occurs when all characters of pattern and text are same
// as the hash values of all the substrings of txt[] match with hash value of pat[]. For example pat[] = “AAA” and txt[] = “AAAAAAA”.
#define lli long long int
#define prime 101
#define M (lli)(1009)
// To compute x^y under modulo m (y,m>0)
lli fastPower(int x,int y,int m){
if(y==0) return 1;
lli smallAns = fastPower(x,y/2,m);
smallAns*=smallAns;
smallAns%=m;
return (y%2==0)?(smallAns):(x*smallAns)%m;
}
lli createHash(string str, int len){
// p is base used for conversion, it is a prime (101)
// hash = ( str[0]*(p^(len-1)) + str[1]*(p^(len-2)) + .... ) % M
lli hash = 0;
for(int i=0;i<len;i++) {
hash = (prime*hash + str[i]) % M;
}
return hash;
}
int rabinKarp(const string text, const string pattern) {
int m = pattern.size();
int n = text.size();
lli patternHash = createHash(pattern,m);
lli textHash = createHash(text,m);
lli h = fastPower(prime,m-1,M);
for(int i=0;i<=n-m;i++) {
if(patternHash==textHash) {
bool isSubstring=true;
for(int j=i,k=0;k<m;j++,k++){
if(pattern[k]!=text[j]){
isSubstring=false;
break;
}
}
if(isSubstring) return i;
}
if(i<n-m) {
// recalculate hash
textHash = (prime*(textHash-text[i]*h) + text[i+m])%M;
if(textHash<0) textHash = textHash + M;
}
}
return -1;
}
int main() {
cout<<rabinKarp("RabinKarp","Karp")<<endl; // Output:5
cout<<rabinKarp("RabinKarp","karp")<<endl; // Output:-1
}