-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMP.cpp
38 lines (34 loc) · 839 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
#include <vector>
#include <iostream>
#include <string>
using namespace std;
vector<int> prefixFunction(string &s);
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
string a, b, s;
getline(cin, a); getline(cin, b);
vector<int> pi;
s = b + "#" + a;
pi = prefixFunction(s);
for (int i = 0; i < a.length(); i++)
if (pi[b.length() + 1 + i] == b.length()) {
cout << i + 2 - b.length();
return 0;
}
cout << -1;
}
vector<int> prefixFunction(string &s){
vector<int> res;
res.resize(s.length());
int m;
for (int i = 1; i < s.length(); i++) {
m = res[i - 1];
while (m > 0 && s[i] != s[m])
m = res[m - 1];
if (s[i] == s[m])
m++;
res[i] = m;
}
return res;
}