-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmp.cpp
46 lines (37 loc) · 864 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
45
46
#include <bits/stdc++.h>
using namespace std;
ifstream fin("powerStrings_UVA10298.in");
ofstream fout("powerStrings_UVA10298.out");
int main()
{
string s = "ercaabcabcjjjj",
t = "abcabc";
int sizeS = s.size(), sizeT = t.size();
int _i = 0, _j = -1;
vector<int> next(sizeT + 1, 0); next[0] = -1;
while (_i <= sizeT - 1)
{
while (_j >= 0 && t[_i] != t[_j])
{
_j = next[_j];
}
++_i; ++_j;
next[_i] = _j;
}
int i = 0, j = 0;
vector<int> ans;
while (i <= sizeS - 1)
{
while (j >= 0 && s[i] != t[j])
{
j = next[j];
}
++i; ++j;
if (j == sizeT)
{
ans.push_back(i - sizeT);
j = next[j];
}
}
return 0;
}