-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.cc
More file actions
63 lines (59 loc) · 1.41 KB
/
kmp.cc
File metadata and controls
63 lines (59 loc) · 1.41 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
56
57
58
59
60
61
62
63
#include <cstring>
#include <vector>
#include <iostream>
using namespace std;
void kmp_table(char *W, int *T) {
T[0] = -1;
int lw = strlen(W);
int pos = 1, cnd = 0;
while(pos < lw) {
if(W[pos] == W[cnd]) {
// when we need to backtrack,
// the next character must be different from W[pos]
// which implies it will not match W[cnd] either
// so we can skip cnd and go to T[cnd] directly
T[pos] = T[cnd];
} else {
T[pos] = cnd;
do {
cnd = T[cnd];
} while (cnd >= 0 && W[pos] != W[cnd]);
}
pos++;
cnd++;
}
T[lw] = cnd;
}
// search W in S, time complexity O(|S|)
vector<int> kmp_search(char *S, char *W, int *T) {
int j = 0, k = 0;
int ls = strlen(S), lw = strlen(W);
vector<int> P;
while(j < ls) {
if (W[k] == S[j]) {
k++;
j++;
if(k == lw) {
P.push_back(j - lw);
k = T[k];
}
} else {
k = T[k];
if (k < 0) {
j++;
k = 0;
}
}
}
return P;
}
int advance(char *str, int l, int *kmp, char ch, int j) {
while (j == l || ch != str[j]) {
if (kmp[j] < 0) {
return 0;
}
j = kmp[j];
}
// j < ls && c[i] == str[j]
return j + 1;
}