-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMP.cpp
64 lines (54 loc) · 1.42 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
/*
Searches for the string w in the string s (of length k). Returns the
0-based index of the first match (k if no match is found). Algorithm
runs in O(k) time.
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
void buildTable(string& w, VI& t)
{
t = VI(w.length());
int i = 2, j = 0;
t[0] = -1; t[1] = 0;
while(i < w.length())
{
if(w[i-1] == w[j]) { t[i] = j+1; i++; j++; }
else if(j > 0) j = t[j];
else { t[i] = 0; i++; }
}
}
int KMP(string& s, string& w)
{
int m = 0, i = 0;
VI t;
buildTable(w, t);
while(m+i < s.length())
{
if(w[i] == s[m+i])
{
i++;
if(i == w.length()) return m;
}
else
{
m += i-t[i];
if(i > 0) i = t[i];
}
}
return s.length();
}
int main()
{
string a = (string) "The example above illustrates the general technique for assembling "+
"the table with a minimum of fuss. The principle is that of the overall search: "+
"most of the work was already done in getting to the current position, so very "+
"little needs to be done in leaving it. The only minor complication is that the "+
"logic which is correct late in the string erroneously gives non-proper "+
"substrings at the beginning. This necessitates some initialization code.";
string b = "table";
int p = KMP(a, b);
cout << p << ": " << a.substr(p, b.length()) << " " << b << endl;
}