-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkmp.cpp
40 lines (27 loc) · 749 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
#include <bits/stdc++.h>
using namespace std;
vector<int> pi(const string &s) {
int n = s.size();
vector<int> pi_s(n);
for (int i = 1, j = 0; i < n; ++i) {
while (j > 0 && s[j] != s[i])
j = pi_s[j - 1];
if (s[i] == s[j])
j++;
pi_s[i] = j;
}
return pi_s;
}
int kmp(string &s, string &p) {
int n = s.size(), m = p.size();
string new_string = p + "#" + s; // -#- can be any string that doesn't appear both in p and s
vector<int> pi_p = pi(new_string);
for (int i = m + 1; i < n + m + 1; ++i)
if (pi_p[i] == m)
return i - (m + 1) - m + 1;
return -1;
}
int main() {
string x = "amalgamation", p = "amat";
cout << kmp(x, p);
}