-
Notifications
You must be signed in to change notification settings - Fork 153
/
KMP.cpp
49 lines (42 loc) · 790 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
49
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
/*
* get prefix list
*/
vector<int> computePrefix(string str) {
int m=int(str.length());
vector<int> prefix(m);
for(int i=1,k=0;i<m;++i)
{
if(k>0&&str[i]!=str[k])
k=prefix[k-1];
if(str[k]==str[i])
prefix[i]=++k;
else
prefix[i]=k;
}
return prefix;
}
void KMP(string &str, string pat) {
int m = int(str.length());
int n = int(pat.length());
vector<int> longestPrefix = computePrefix(pat);
for (int i = 0, k = 0; i < m; ++i) {
if (k > 0 && pat[k] != str[i])
k = longestPrefix[k - 1];
if (str[i] == pat[k])
++k;
if (k == n) {
cout << i - n + 1 << "\n";
//more than one pattern
k = longestPrefix[k - 1];
}
}
}
int main() {
string s,p;
cin>>s>>p;
KMP(s,p);
return 0;
}