Skip to content

Commit a9ce719

Browse files
authored
Merge pull request #262 from Abhishek-kumar82078/master
Added Rabin Karp algorithm
2 parents ce66ea0 + 9955e65 commit a9ce719

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Pattern_Matching/src/Rabin_Karp.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//Rapin karp pattern searching algorithm
2+
//Best and average time complexity - O(n+m)
3+
//Worst case time complexity [O(nm)] when all characters of pattern and text are same
4+
//as the hash values of all the substrings of txt[] match with hash value of pat[]
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
int main()
9+
{
10+
string s,pat;
11+
cin>>s>>pat;
12+
int n=s.size();
13+
int m=pat.size();
14+
int cnt=0;
15+
long long mod=1e9+7;
16+
// If length of string of string is >= than that of pattern
17+
if(n>=m)
18+
{
19+
//Calculating hash value for 1st m characters as well as for pattern also
20+
//hashfunction: [ hash+=pow(26,m - char no.)*hashcode_of_that_char ] for m characters
21+
int hashstring=0,j=0,hashpat=0;
22+
while(j<m)
23+
{
24+
hashstring=(hashstring + ( (s[j]-'a'+1) * int(pow(26,m-j-1))%mod ))%mod;
25+
hashpat=(hashpat + ( (pat[j]-'a'+1)*int(pow(26,m-j-1))%mod ))%mod;
26+
j++;
27+
}
28+
29+
if(hashpat==hashstring)
30+
{
31+
int i;
32+
//matching pattern and substring
33+
for( i=0;i<m;i++)
34+
{
35+
if(s[i]==pat[i])
36+
continue;
37+
else
38+
break;
39+
}
40+
if(i==m)
41+
cnt++;
42+
}
43+
44+
for(int i=1;i<n-m+1;i++)
45+
{
46+
//Ex:- for dbac, if length of pat is 3 and we want to calculate for index 1
47+
//then remove contribution of 'd' (at index 0) and add that of 'c' with proper degree in power
48+
49+
hashstring-=( (s[i-1]-'a'+1) * int(pow(26,m-1))%mod )%mod; //subtracting 1st string of previous group
50+
hashstring=(hashstring*26)%mod; //Increasing power of each remaining char by 1
51+
hashstring=(hashstring + (s[i+m-1]-'a'+1))%mod; //Adding hashcode for new char in that group that that of previous one
52+
53+
if(hashpat==hashstring)
54+
{
55+
int k;
56+
// Matching pattern and substring
57+
for( k=0;k<m;k++)
58+
{
59+
if(s[k+i]==pat[k])
60+
continue;
61+
else
62+
break;
63+
}
64+
if(k==m)
65+
cnt++;
66+
}
67+
}
68+
cout<<"No. of pattern found is "<<cnt;
69+
}
70+
else
71+
cout<<"String length is smaller than that of pattern";
72+
return 0;
73+
}
74+
75+
//Description
76+
//Rabin karp is one of the efficient algorithm for pattern searching.
77+
//It uses the concept that calculate the hashvalue for pattern
78+
//Also calculate for substring of m characters and equate it with that of pattern one
79+
//If it matches means 1 pattern found

0 commit comments

Comments
 (0)