-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.cpp
47 lines (39 loc) · 873 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
#include <string>
#include <vector>
#include <iostream>
std::vector<int> compute_prefix(const std::string& p) {
int m = p.size();
std::vector<int> pi(m);
int k = 0;
pi[0] = 0;
for (int q = 1; q < m; q++) {
while(k > 0 && p[k] != p[q])
k = pi[k - 1];
if (p[k] == p[q])
k++;
pi[q] = k;
}
return pi;
}
void kmp_match(const std::string& s, const std::string& p) {
std::vector<int> pi = compute_prefix(p);
int q = 0;
int n = s.size();
int m = p.size();
for(int i = 0; i < n; i++) {
while (q > 0 && p[q] != s[i])
q = pi[q - 1];
if (p[q] == s[i])
q++;
if (q == m) {
std::cout << "Match pos "
<< (i - m + 1)
<< std::endl;
}
}
}
int main() {
std::string s = "catdigdogcatdocatdog";
kmp_match(s, "cat");
return 0;
}