-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1786.cpp
More file actions
64 lines (53 loc) · 916 Bytes
/
1786.cpp
File metadata and controls
64 lines (53 loc) · 916 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
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
string str;
string str2;
int cnt = 0;
vector<int>ans;
vector<int> findPattern() {
int size = str2.length();
vector<int>pi(size);
int j = 0;
for (int i = 1; i < size; i++) {
while (j > 0 && str2[i] != str2[j]) {
j = pi[j - 1];
}
if (str2[i] == str2[j]) {
pi[i]=++j;
}
}
return pi;
}
void kmp() {
int size1 = str.length();
int size2 = str2.length();
vector<int> pi = findPattern();
int j = 0;
for (int i = 0; i < size1; i++) {
while (j > 0 && str[i] != str2[j]) {
j = pi[j-1];
}
if (str[i] == str2[j]) {
if (j == size2 - 1) {
cnt++;
ans.push_back(i - j+1);
j = pi[j];
}
else {
j++;
}
}
}
}
int main() {
getline(cin, str);
getline(cin, str2);
kmp();
cout << cnt << "\n";
for (int i = 0; i < ans.size(); i++) {
cout << ans[i]<< " ";
}
}