-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
kmp.cpp
48 lines (46 loc) · 845 Bytes
/
kmp.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
#include<bits/stdc++.h>
using namespace std;
int kmp(string text, string pattern){
if(pattern.size()==0) return -1;
int patLen=pattern.size();
int textLen=text.size();
int LPS[patLen]={0};
int i=1,j=0;
while(i<patLen){
if(pattern[i]==pattern[j]){
LPS[i++]=++j;
}
else{
if(j) j=LPS[j-1];
else LPS[i++]=0;
}
}
i=j=0;
while(i<textLen){
if(pattern[j]==text[i]){
i++;j++;
}
if(j==patLen) return i-j;
else{
if(i<textLen && pattern[j]!=text[i]){
if(j) j=LPS[j-1];
else i++;
}
}
}
return -1;
}
int main(){
string text,pattern;
cout<<"\nEnter text : ";
getline(cin>>ws,text);
cout<<"\nEnter pattern to search in "<<text<<" : ";
getline(cin>>ws,pattern);
int index=kmp(text,pattern);
if(index>0){
cout<<"\nPattern found at : "<<index<<"\n";
}
else{
cout<<"\nPattern not found!\n";
}
}