-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.cpp
More file actions
39 lines (39 loc) · 825 Bytes
/
KMP.cpp
File metadata and controls
39 lines (39 loc) · 825 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
33
34
35
36
37
38
39
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> getNext(string pattern) {
vector<int> next(pattern.size());
next[0] = -1;
int i = 0, copyed = -1;
while (i < (int)pattern.size() - 1) {
if (copyed < 0 || pattern[copyed] == pattern[i] ){
i++, copyed++;
next[i] = copyed;
}
else {
copyed = next[copyed];
}
}
return next;
}
int KMP(string source, string pattern){
vector<int> next = getNext(pattern);
int i = 0, j = 0;
while (i < (int)source.size() && j < (int)pattern.size()){//注意返回的是size_t,所以需要转化成int才能用
if (j < 0 || pattern[j] == source[i]) {
i++, j++;
}
else {
j = next[j];
}
}
if (j == pattern.size()) {
return i - j;
}
return -1;
}
int main() {
cout << KMP("aaaABCDABD","ABCDABD");
return 0;
}