-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathkmp.cpp
More file actions
32 lines (28 loc) · 796 Bytes
/
kmp.cpp
File metadata and controls
32 lines (28 loc) · 796 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
//WARNING: Arrays are 1-based index.
const int LENGTH = XYZ;
char text[LENGTH], pat[LENGTH];
int pre[LENGTH];
void compute () {
int plen = strlen ( pat + 1 ), k = 0;
pre[1] = 0;
for ( int i = 2; i <= plen; i++ ) {
while ( k && pat[k+1] != pat[i] ) k = pre[k];
if ( pat[k+1] == pat[i] ) k++;
pre[i] = k;
}
}
int match () {
int tlen = strlen ( text + 1 ), plen = strlen ( pat + 1 );
int q = 0, res = 0;
for ( int i = 1; i <= tlen; i++ ) {
while ( q && pat[q+1] != text[i] ) q = pre[q];
if ( pat[q+1] == text[i] ) q++;
if ( q == plen ) {
res++;
q = pre[q];
}
}
return res;
}
/// Application
/// Period of a string. N - pre[N] is the length of period iff period length divides N.