|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | + |
| 5 | +// Using Rabin Karp Algorithm for Pattern Searching |
| 6 | +// Rabin-Karp algorithm slides the pattern one by one. |
| 7 | +// But unlike the Naive algorithm, Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of text, |
| 8 | +// and if the hash values match then only it starts matching individual characters. |
| 9 | + |
| 10 | +// The average and best case running time of the Rabin-Karp algorithm is O(n+m), but its worst-case time is O(nm). |
| 11 | +// Worst case of Rabin-Karp algorithm occurs when all characters of pattern and text are same |
| 12 | +// as the hash values of all the substrings of txt[] match with hash value of pat[]. For example pat[] = “AAA” and txt[] = “AAAAAAA”. |
| 13 | + |
| 14 | +#define lli long long int |
| 15 | +#define prime 101 |
| 16 | +#define M (lli)(1009) |
| 17 | + |
| 18 | +// To compute x^y under modulo m (y,m>0) |
| 19 | +lli fastPower(int x,int y,int m){ |
| 20 | + if(y==0) return 1; |
| 21 | + lli smallAns = fastPower(x,y/2,m); |
| 22 | + smallAns*=smallAns; |
| 23 | + smallAns%=m; |
| 24 | + return (y%2==0)?(smallAns):(x*smallAns)%m; |
| 25 | +} |
| 26 | + |
| 27 | +lli createHash(string str, int len){ |
| 28 | + // p is base used for conversion, it is a prime (101) |
| 29 | + // hash = ( str[0]*(p^(len-1)) + str[1]*(p^(len-2)) + .... ) % M |
| 30 | + lli hash = 0; |
| 31 | + for(int i=0;i<len;i++) { |
| 32 | + hash = (prime*hash + str[i]) % M; |
| 33 | + } |
| 34 | + return hash; |
| 35 | +} |
| 36 | + |
| 37 | +int rabinKarp(const string text, const string pattern) { |
| 38 | + int m = pattern.size(); |
| 39 | + int n = text.size(); |
| 40 | + lli patternHash = createHash(pattern,m); |
| 41 | + lli textHash = createHash(text,m); |
| 42 | + lli h = fastPower(prime,m-1,M); |
| 43 | + for(int i=0;i<=n-m;i++) { |
| 44 | + if(patternHash==textHash) { |
| 45 | + bool isSubstring=true; |
| 46 | + for(int j=i,k=0;k<m;j++,k++){ |
| 47 | + if(pattern[k]!=text[j]){ |
| 48 | + isSubstring=false; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + if(isSubstring) return i; |
| 53 | + } |
| 54 | + if(i<n-m) { |
| 55 | + // recalculate hash |
| 56 | + textHash = (prime*(textHash-text[i]*h) + text[i+m])%M; |
| 57 | + if(textHash<0) textHash = textHash + M; |
| 58 | + } |
| 59 | + } |
| 60 | + return -1; |
| 61 | +} |
| 62 | + |
| 63 | +int main() { |
| 64 | + cout<<rabinKarp("RabinKarp","Karp")<<endl; // Output:5 |
| 65 | + cout<<rabinKarp("RabinKarp","karp")<<endl; // Output:-1 |
| 66 | +} |
0 commit comments