-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmp_serach.c
59 lines (53 loc) · 1.23 KB
/
kmp_serach.c
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
//
// Created by 吴裕欣 on 2018/12/17.
//
#include "kmp_serach.h"
void get_next(char *p, int **next) {
size_t len = strlen(p);
*next = calloc(len, sizeof(char));
(*next)[0] = -1;
size_t i = 0;
int j = -1;
while (i < len) {
if (j == -1 || *(p+i) == *(p+j)) {
i++;
j++;
(*next)[i] = j;
} else {
j = (*next)[j];
}
}
}
int kmp(char *s, char *p) {
int i = 0;
int j = 0;
int sLen = (int)strlen(s);
int pLen = (int)strlen(p);
int *next = NULL;
get_next(p, &next);
while (i < sLen && j < pLen)
{
//①如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++
if (j == -1 || s[i] == p[j])
{
i++;
j++;
}
else
{
//②如果j != -1,且当前字符匹配失败(即S[i] != P[j]),则令 i 不变,j = next[j]
//next[j]即为j所对应的next值
j = next[j];
}
}
if (j == pLen)
return i - j;
else
return -1;
}
int main() {
char *p = "ababc";
int d = kmp("abcdfasdfasdfasdfasdfasdfasdfasdfafasdfababc", "ababc");
printf("%d\n", d);
return 0;
}