-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.cpp
More file actions
39 lines (34 loc) · 920 Bytes
/
Copy pathKMP.cpp
File metadata and controls
39 lines (34 loc) · 920 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
#include<bits/stdc++.h>
using namespace std;
vector<int> build_lps(vector<int>&LPS , string p){
int pre = 0 , suf = 1;
while(suf < p.size()){
if(p[pre] == p[suf]){
LPS[suf++] = ++pre;
}else{
pre == 0 ? suf++ : pre = LPS[pre-1];
}
}
return LPS;
}
bool KMP(vector<int>LPS , string t , string p){
int first = 0 , second = 0;
while(first < t.size()){
if(t[first] == p[second]){
first++ , second++;
}else{
second == 0 ? first++ : second = LPS[second-1];
}
if(second == p.size()) return true;
}
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
string text , pattern; cin >> text >> pattern;
vector<int>LPS(pattern.size() , 0);
LPS = build_lps(LPS , pattern);
KMP(LPS , text , pattern) ? cout<<"Found" : cout<<"Not Found";
return 0;
}