-
Notifications
You must be signed in to change notification settings - Fork 106
/
rabin_karp.cpp
84 lines (69 loc) · 1.48 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
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
#define int long long
int hashh[100];
int pr = 27;
int M = 1e9+7;
// Palindrome check for prefix
void Prefix_palincheck_byHashing(string s)
{
int n = s.size();
for(int i = 1;i<n-1;i++)
hashh[i] = (hashh[i-1] * pr)%M;
int val1 = 0, val2 = 0;
for(int i = 0;i<n;i++)
{
val1 = (val1 * pr + s[i])%M;
val2 = (val2 + s[i] * hashh[i])%M;
if(val1 == val2)
{
cout << "Prefix till i is palindrome";
}
}
}
// Function to find the occurrences of pattern
// in the string
void rabin_karp(string s, string t)
{
int md = 1e9 + 9;
int p = 31;
int n = s.size();
int m = t.size();
int poww[n];
poww[0] = 1;
for(int i = 1;i<n;i++)
poww[i] = (poww[i-1]*p)%md;
int hash_value = 0;
for(int i = 0; i < m; i++)
{
hash_value = (hash_value + (t[i]-'a'+1)*poww[i])%md;
}
int string_hash[n+1];
memset(string_hash,0,sizeof string_hash);
for(int i = 0;i<n;i++)
{
string_hash[i+1] = (string_hash[i] + (s[i]-'a'+1)*poww[i])%md;
}
// Stores all index where the
// string T occurs in S
vector<int>index_jahan_hai;
for(int i = 0;i<=n-m;i++)
{
int value = (string_hash[i+m]+md-string_hash[i])%md;
int temp = (hash_value * poww[i])%md;
if(value == temp)
index_jahan_hai.push_back(i);
}
// Prints all the index where it starts
// from
for(auto it:index_jahan_hai)
cout << it << " ";
}
signed main()
{
string s, t;
s = "arajaarajjraj";
t = "raj";
rabin_karp(s,t);
return 0;
}