forked from lshtar13/PS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1786.cc
71 lines (60 loc) · 1.13 KB
/
1786.cc
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
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string t;
getline(cin, t);
string p;
getline(cin, p);
vector<int> kmp(p.size());
kmp[0] = -1;
for(int i = 1; i<kmp.size(); ++i)
{
int _kmp = kmp[i-1];
while(_kmp != -1 && p[i]!=p[_kmp+1])
{
_kmp = kmp[_kmp];
}
kmp[i] = p[_kmp+1] == p[i]?_kmp+1:-1;
}
vector<int> result;
int i = 0, j = 0;
while(i<t.size())
{
if(t[i] == p[j])
{
++i, ++j;
}
else
{
if(!j)
{
++i;
}
else
{
j = kmp[j-1]+1;
}
}
if(j == p.size())
{
j = kmp[j-1]+1;
result.push_back(i-p.size());
}
}
cout<<result.size()<<"\n";
for(auto r: result)
{
cout<<r+1<<" ";
}
// cout<<"\nkmp: ";
// for(auto a: kmp)
// {
// cout<<a<<" ";
// }
return 0;
}