-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkmp.cpp
More file actions
176 lines (168 loc) · 4.72 KB
/
kmp.cpp
File metadata and controls
176 lines (168 loc) · 4.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <vector>
#include <string>
#include <iostream>
using namespace std;
/**
* A implement of the KMP algorithm.
* Provide four function:
* findFirst: find the first exist of pattern string in the source string.
* findAll: find all pattern string exist position in the source string.
* findFirstStartAt: find the first exist of pattern string in the source
* string after the given position.
* findAllStartAt: find all pattern string exist position in the source string
* after the given position
*/
class KMP {
private:
/**
* The index table for the pattern stirng.
* 0: init with -1.
* 1: init with 1.
* The rest is init with the pattern that backtrack to the common prefix
* with the substring start at head.
*/
vector<int> indexSearch(string &str) {
vector<int> index(str.length(), -1);
if (str.length() < 2) {
return index;
}
index[1] = 0;
size_t pos = 2;
size_t cur = 0;
while (pos < str.length()) {
if (str[pos - 1] == str[cur]) {
index[pos] = cur + 1;
++cur;
++pos;
} else if (cur > 0) {
cur = index[cur];
index[pos] = 0;
} else {
index[pos] = 0;
++pos;
}
}
return index;
}
public:
int findFirst(string &s, string &p) {
vector<int> index = indexSearch(p);
int s_len = s.length();
int p_len = p.length();
int p_cur = 0;
int s_cur = 0;
while (s_cur < s_len) {
if (p_cur == -1) {
++p_cur;
++s_cur;
} else if (s[s_cur] == p[p_cur]) {
if (p_cur == p_len - 1) {
return s_cur - p_cur;
}
++p_cur;
++s_cur;
} else {
p_cur = index[p_cur];
}
}
return -1;
}
vector<int> findAll(string &s, string &p) {
vector<int> index = indexSearch(p);
vector<int> result;
int s_len = s.length();
int p_len = p.length();
int p_cur = 0;
int s_cur = 0;
while (s_cur < s_len) {
if (p_cur == -1) {
++p_cur;
++s_cur;
} else if (s[s_cur] == p[p_cur]) {
if (p_cur == p_len - 1) {
result.push_back(s_cur - p_cur);
p_cur = index[p_cur];
} else {
++p_cur;
++s_cur;
}
} else {
p_cur = index[p_cur];
}
}
return result;
}
int findFirstStartAt(string& s, int pos, string& p) {
vector<int> index = indexSearch(p);
int s_len = s.length();
int p_len = p.length();
int p_cur = pos;
int s_cur = 0;
while (s_cur < s_len) {
if (p_cur == -1) {
++p_cur;
++s_cur;
} else if (s[s_cur] == p[p_cur]) {
if (p_cur == p_len - 1) {
return s_cur - p_cur;
}
++p_cur;
++s_cur;
} else {
p_cur = index[p_cur];
}
}
return -1;
}
vector<int> findAllStartAt(string& s, int pos, string& p) {
vector<int> index = indexSearch(p);
vector<int> result;
int s_len = s.length();
int p_len = p.length();
int p_cur = pos;
int s_cur = 0;
while (s_cur < s_len) {
if (p_cur == -1) {
++p_cur;
++s_cur;
} else if (s[s_cur] == p[p_cur]) {
if (p_cur == p_len - 1) {
result.push_back(s_cur - p_cur);
p_cur = index[p_cur];
} else {
++p_cur;
++s_cur;
}
} else {
p_cur = index[p_cur];
}
}
return result;
}
};
int main(int argc, char* argv[]) {
KMP kmp;
string s = "abcabcefgabcd";
string p = "a";
// test case findFirst
cout << "findFirst: ";
cout << kmp.findFirst(s, p) << endl;
// test case findAll
auto re = kmp.findAll(s, p);
cout << "findAll: ";
for (auto ele : re) {
cout << ele << " ";
}
cout << endl;
// test case findFirstStartAt
cout << "findFirstStartAt: ";
cout << kmp.findFirstStartAt(s, 2, p) << endl;
// test case findAllStartAt
cout << "findAllStartAt: ";
re = kmp.findAllStartAt(s, 2, p);
for (auto ele : re) {
cout << ele << " ";
}
cout << endl;
return 0;
}