forked from t3nsor/codebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMP.cpp
44 lines (39 loc) · 794 Bytes
/
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
/*
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.
*/
typedef vector<int> VI;
void buildTable(string& w, VI& t)
{
t = VI(w.length());
int i = 2, j = 0;
t[0] = -1;
if (w.length() > 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();
}