-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_2107.cpp
More file actions
55 lines (51 loc) · 1.04 KB
/
cses_2107.cpp
File metadata and controls
55 lines (51 loc) · 1.04 KB
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
#include <bits/extc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string str;
cin >> str;
vector<int> next(1, 0);
int pre = 0;
for (int i = 1; i < str.size();)
{
if (str[pre] == str[i])
{
pre++;
i++;
next.emplace_back(pre);
}
else
{
if (pre == 0)
{
next.emplace_back(0);
i++;
}
else
pre = next[pre - 1];
}
}
int n = str.size(), l = 0, r = 0;
vector<int> z(n);
for (int i = 1; i < n; i++)
{
if (i <= r && i + z[i - l] - 1 < r)
z[i] = z[i - l];
else
{
z[i] = max(0, r - i + 1);
while (str[z[i]] == str[i + z[i]])
z[i]++;
l = i, r = i + z[i] - 1;
}
}
for (auto x : z)
cout << x << ' ';
cout << '\n';
for (auto x : next)
cout << x << ' ';
cout << '\n';
return 0;
}