-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.cpp
More file actions
53 lines (50 loc) · 935 Bytes
/
kmp.cpp
File metadata and controls
53 lines (50 loc) · 935 Bytes
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
#include<vector>
#include<iostream>
using namespace std;
int kmpMatch(string &str, string &pattern, vector<int> &lp){
int j = -1;
for(int i = 0; i < str.size(); i++){
if(j + 1 == pattern.size() - 1){
return i + 1 - pattern.size();
}else if(str[i] == pattern[j + 1]){
j++;
}else{
while(pattern[lp[j]] != str[i] && j != 0){
j = lp[j];
}
if(j == 0){
j = -1;
}
}
}
return -1;
}
vector<int> lsp(string &pattern){
vector<int> lp(pattern.size(), 0);
int i = 0, j = 1, l = 0;
while(j != pattern.size()){
if(pattern[i] == pattern[j]){
l++;
lp[j] = l;
i++;
j++;
}else{
if(i == 0){
j++;
}else{
i--;
}
l = lp[i];
}
}
return lp;
}
int main(){
string str;
string pattern;
cin>>str>>pattern;
vector<int> lp = lsp(pattern);
int s = kmpMatch(str, pattern, lp);
cout<<"For a given string: "<<str<<" and pattern: "<<pattern<<" output is: "<<s<<endl;
return 0;
}