forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.cpp
More file actions
55 lines (49 loc) · 1.07 KB
/
Copy pathKMP.cpp
File metadata and controls
55 lines (49 loc) · 1.07 KB
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
#include <bits/stdc++.h>
using namespace std;
void make_lps(int *lps, string &pattern){
int j=0, i=1;
lps[0]=0;
while(i<pattern.length()){
if(pattern[i]==pattern[j]){
lps[i++]=(j++)+1;
}
else if(j>0){
j=lps[j-1];
}
else{
lps[i++]=0;
}
}
}
int match(string &orig, string &pattern, int *lps){
int patt_index=0;
int orig_index=0;
while(orig_index<orig.length()){
if(orig[orig_index]==pattern[patt_index]){
orig_index++;
patt_index++;
}
else if(patt_index==0){
orig_index++;
}
else{
patt_index=lps[patt_index-1];
}
if(patt_index==pattern.length()){
return 1;
}
}
return patt_index==pattern.length();
}
int main()
{
// freopen("input.txt", "rd", stdin);
string orig;
cin>>orig;
string pattern;
cin>>pattern;
int lps[pattern.length()];
make_lps(lps, pattern);
cout<<endl<<match(orig, pattern, lps);
return 0;
}