-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMP.cpp
53 lines (49 loc) · 1.08 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
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void PrintVectorInt(int i) {
cout << i << " ";
}
bool KMP(string bigstr, string cstr) {
int len_bigstr = bigstr.length();
int len_cstr = cstr.length();
if (len_bigstr < len_cstr)return false;
if (len_cstr == 0)return true;
int next[len_cstr];
int j = 0;
int i = 1;
next[0] = 0;
while (len_cstr > i) {
if (cstr[j] == cstr[i]) {
next[i++] = ++j;
} else {
if (j == 0) {
next[i++] = 0;
} else {
j = next[j - 1];
}
}
}
i = 0, j = 0;
for_each(next, next + len_cstr, PrintVectorInt);
cout << endl;
while (i < len_bigstr) {
if (bigstr[i] == cstr[j]) {
i++;
j++;
} else {
if (j == 0) {
i++;
} else {
j = next[j - 1];
}
}
if (j == len_cstr)return true;
}
return false;
}
int main() {
cout << KMP("abxabcabcaby", "abcaby");
return 0;
}