-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringMatching_KMP.cpp
More file actions
88 lines (77 loc) · 1.72 KB
/
StringMatching_KMP.cpp
File metadata and controls
88 lines (77 loc) · 1.72 KB
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
82
83
84
85
86
87
88
/*
Implmentation of KMP algorithm
@see http://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm
*/
#include <string>
#include <vector>
using namespace std;
int kmpSearch(string &, string &);
void kmpFillTable(string &, vector<int> &);
/**
* @brief [perform kmp search using fault table]
* @details [m (the beginning of the current match in S)
* i (the position of the current character in W)
* T (the fault table, computed elsewhere)]
*
* @param W [the word sought]
* @param S [text to be searched in]
*
* @return [the start index at S at which W is found]
*/
int kmpSearch(string &S, string &W) {
int i = 0;
int m = 0;
vector<int> T(W.size(), 0);
kmpFillTable(W, T);
while(m+i < S.size()) {
if(W[i] == S[m+i] ) {
i++;
if(i == W.size()) {
return m;
}
} else {
if(T[i] > 0) {
m = m + i - T[i];
i = T[i];
} else {
i = 0;
m++;
}
}
}
return -1;
}
/**
* @brief [fill the fault table for the pattern word]
* @details [pos (the current position we are computing in T)
* cnd (the zero-based index of in W of the next
* character of the current candidate string)]
*
* @param W [pattern word]
* @param r [fault table to be filled]
*/
void kmpFillTable(string &W, vector<int> &T) {
if(T.size() < 1) return;
T[0] = 0;
if(T.size() < 2) return;
T[1] = 0;
if(T.size() < 3) return;
int pos = 2;
int cnd = 0;
while(pos < T.size()) {
if(W[pos-1] == W[cnd]) {
//++cnd; T[pos] = cnd; pos++;
T[pos++] = ++cnd;
} else if(cnd > 0) {
cnd = T[cnd];
} else {// cnd == 0
T[pos++] = 0;
}
}
}
int main(void) {
string S = "ABABDABACDABABCABABCD";
string W = "ABABCABAB";
printf("%d\n", kmpSearch(S, W));
return 0;
}