-
Notifications
You must be signed in to change notification settings - Fork 21
/
KMP.cpp
42 lines (38 loc) · 820 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
/*
KMP string matching. O(n)
*/
#define NN 100100
int F[NN];
void computeFailure(char T[], int m) { // m is the length of T
int i = 1,j = 0;
F[0] = 0;
while(i < m) {
while(j < m && T[i] == T[j]) {
++j; F[i] = j; ++i;
}
if(i == m) break;
if(j == 0) {
F[i] = 0;
++i;
} else {
j = F[j-1];
}
}
}
int KMP(const string & S,char T[]) { // search T in S
int n = S.length();
int m = strlen(T);
int cnt = 0;
if(m > n) return 0;
computeFailure(T, m);
int i = 0, j = 0;
while((i-j) <= n - m) {
while(j < m && S[i] == T[j]) {
++j; ++i;
}
if(j == m) cnt++; // match found
if(j == 0) ++i;
else j = F[j-1];
}
return cnt;
}