-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.cpp
More file actions
57 lines (45 loc) · 1019 Bytes
/
kmp.cpp
File metadata and controls
57 lines (45 loc) · 1019 Bytes
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
// BOJ 1786 찾기
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
vector<int> kmp(string &x, string &y) {
vector<int> lps(y.sz);
int j = 0;
for (int i = 1; i < y.sz; i++) {
while (j > 0 && y[i] != y[j])
j = lps[j - 1];
if (y[i] == y[j])
lps[i] = ++j;
}
vector<int> ret;
j = 0;
for (int i = 0; i < x.sz; i++) {
while (j > 0 && x[i] != y[j])
j = lps[j - 1];
if (x[i] != y[j])
continue;
if (j + 1 == y.sz) {
ret.push_back(i - y.sz + 2);
j = lps[j];
} else
j++;
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string x, y;
getline(cin, x);
getline(cin, y);
vector<int> ans = kmp(x, y);
cout << ans.sz << '\n';
for (int i : ans)
cout << i << ' ';
}