/
rabin_karp.cpp
68 lines (65 loc) · 1.57 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
#include <bits/stdc++.h>
using namespace std;
#define d 256
#define q 101
void searchString(string pattern, char text[], int patLength, int txtLength)
{
int i, j;
int pat = 0;
int txt = 0;
int h = 1; // hash value
bool check_pattern_found = true;
for (i = 0; i < patLength - 1; i++)
h = (h * d) % q;
for (i = 0; i < patLength; i++)
{
pat = (d * pat + pattern[i]) % q;
txt = (d * txt + text[i]) % q;
}
i = 0;
while (i <= txtLength - patLength)
{
if (pat == txt)
{
bool flag = true;
for (j = 0; j < patLength; j++)
{
if (text[i + j] != pattern[j])
{
flag = false;
break;
}
}
if (j == patLength)
{
check_pattern_found = false;
cout << i << " ";
}
}
if (i < txtLength - patLength)
{
txt = (d * (txt - text[i] * h) + text[i + patLength]) % q;
if (txt < 0)
txt = (txt + q);
}
i++;
}
if (check_pattern_found)
{
cout << "----Pattern match not found at any index----";
}
}
int main()
{
char txt[d];
string pat;
cout << "Please Enter text string - ";
cin.getline(txt, d);
cout << endl
<< "Please Enter pattern string - ";
cin >> pat;
cout << endl
<< "All index numbers where pattern found:" << endl;
searchString(pat, txt, pat.length(), strlen(txt));
return 0;
}