-
Notifications
You must be signed in to change notification settings - Fork 32
/
KMP.cpp
81 lines (71 loc) · 1.4 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <string>
using namespace std;
/*
P为模式串,下标从0开始
*/
void GetNext(string P, int next[]) {
int p_len = P.size();
int i = 0; //下标
int j = -1;
next[0] = -1;
while (i < p_len) {
if (j == -1 || P[i] == P[j]) {
i++;
j++;
next[i] = j;
} else {
j = next[j];
}
}
}
/*
优化:P为模式串,下标从0开始
*/
void GetNext(string P, int next[]) {
int p_len = P.size();
int i = 0; //下标
int j = -1;
nextval[0] = -1;
while (i < p_len) {
if (j == -1 || P[i] == P[j]) {
i++;
j++;
if (P[i] == P[j]) {
/* 如果与前缀字符相同,则将前缀 */
/* 字符的nextval值赋值给nextval在i位置的值 */
nextval[i] = nextval[j];
} else {
/*则当前的j为nextval在i位置的值*/
nextval[i] = j;
}
} else {
j = nextval[j];
}
}
}
/* 在S中找到P第一次出现的位置 */
int KMP(string S, string P, int next[]) {
GetNext(P, next);
int i = 0;
int j = 0;
int s_len = S.size();
int p_len = P.size();
while (i < s_len && j < p_len) {
if (j == -1 || S[i] == P[j]) {//P的第一个字符不匹配或S[i] == P[j]
i++;
j++;
} else {
j = next[j]; //当前字符匹配失败,跳转
}
}
if (j == p_len) {
return i - j;
}
return -1;
}
int main(int argc, char *argv[]) {
int next[100] = {0};
cout << KMP("bbc abcdab abcdabcdabde", "abcdabd", next) << endl;
return 0;
}