-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1786.cpp
More file actions
38 lines (35 loc) · 901 Bytes
/
icpc1786.cpp
File metadata and controls
38 lines (35 loc) · 901 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
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string t, p;
vector<int> failure;
queue<int> answer;
getline(cin, t);
getline(cin, p);
failure.resize(p.size());
for (int i = 1, f = 0; i < p.size(); i++) {
while (f > 0 && p[i] != p[f]) f = failure[f - 1];
if (p[i] == p[f]) failure[i] = ++f;
}
for (int i = 0, f = 0; i < t.size(); i++) {
while (f > 0 && t[i] != p[f]) f = failure[f - 1];
if (t[i] == p[f]) {
if (++f == p.size()) {
answer.push(i - f + 2);
f = failure[f - 1];
}
}
}
cout << answer.size() << '\n';
while (answer.size()) {
cout << answer.front() << '\n';
answer.pop();
}
return 0;
}