-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRabin-Karp Algorithm (modified) - Pattern matching.cpp
More file actions
78 lines (57 loc) · 1.69 KB
/
Rabin-Karp Algorithm (modified) - Pattern matching.cpp
File metadata and controls
78 lines (57 loc) · 1.69 KB
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
77
78
// Modified Rabin-Karp algorithm by usign two times hashing.
// It can be applied not only on strigns but also on array of integers.
#include <iostream>
using namespace std;
typedef long long LL;
const int BASE1 = 31, MOD1 = 1e9+7, BASE2 = 37, MOD2 = 1e9+9;
LL modular_pow(LL base, LL exponent, LL modulus)
{
if(modulus == 1) return 0;
LL result = 1;
base %= modulus;
while(exponent > 0) {
if(exponent & 1) {
result = (result * base) % modulus;
}
exponent >>= 1;
base = (base * base) % modulus;
}
return result;
}
void search_by_hashing(string &txt, string &pat)
{
int n = txt.size(), m = pat.size();
LL h1 = modular_pow(BASE1, m-1, MOD1), h2 = modular_pow(BASE2, m-1, MOD2);
LL hTxt1 = 0, hTxt2 = 0, hPat1 = 0, hPat2 = 0; // Hash values.
for(int i = 0; i < m; ++i) {
hPat1 = (hPat1 * BASE1 + pat[i]) % MOD1;
hPat2 = (hPat2 * BASE2 + pat[i]) % MOD2;
hTxt1 = (hTxt1 * BASE1 + txt[i]) % MOD1;
hTxt2 = (hTxt2 * BASE2 + txt[i]) % MOD2;
}
for(int i = 0; i <= n-m; ++i) {
if(hPat1 == hTxt1 && hPat2 == hTxt2) {
printf("Match found at index: %d\n", i);
}
if(i < n-m) {
hTxt1 = ((hTxt1 - txt[i]*h1) * BASE1 + txt[i+m]) % MOD1;
hTxt2 = ((hTxt2 - txt[i]*h2) * BASE2 + txt[i+m]) % MOD2;
if(hTxt1 < 0) hTxt1 += MOD1;
if(hTxt2 < 0) hTxt2 += MOD2;
}
}
}
int main()
{
string txt, pat;
txt = string("abc aabcaa abc");
pat = string("abc");
search_by_hashing(txt, pat);
return 0;
}
/*
Output:
Match found at index: 0
Match found at index: 5
Match found at index: 11
*/