-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.h
executable file
·57 lines (46 loc) · 1013 Bytes
/
kmp.h
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
/*
* kmp.h
*
* Created on: 2011-10-8
* Author: shanshan
*/
#ifndef KMP_H_
#define KMP_H_
//
void getNext(char p[], int len, int next[]) {
next[1] = 0;
int i = 1;
int j = 0;
while ( i < len) {
if (j == 0 || p[i] == p[j]) { //相等,直接next+1
i++;
j++;
next[i] = j;
} else {
j = next[j]; //不等,取j的next[j],再进行比较
}
}
}
int findIndexKMP(char source[], int source_len, char patt[], int patt_len) {
int next[patt_len+1];
getNext(patt, patt_len, next);
for (int i = 1; i <= patt_len; i++) {
printf("%d, ", next[i]);
}
int i = 1;
int j = 1;
while ( i <= source_len && j <= patt_len) {
// j=0,表示j的第一个元素匹配失败,取next【1】=0,用j的第一个元素和i的下一个元素进行比较
if ( j == 0 || source[i] == patt[j]) { //匹配成功,i,j同时后移
i++;
j++;
} else {
j = next[j]; //匹配失败,i不变,j回溯
}
}
if (j > patt_len)
return (i - patt_len);
else
return 0;
}
#endif /* KMP_H_ */