-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZ-KMP.cpp
More file actions
65 lines (63 loc) · 1.22 KB
/
Z-KMP.cpp
File metadata and controls
65 lines (63 loc) · 1.22 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
56
57
58
59
60
61
62
63
64
65
// Z-function (Z-algorithm)
// cses 2107
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 200005;
vector<int> build(string s) {
vector<int>fail;
fail.push_back(-1);
int p1 = 0, p2 = -1;
while (p1 < (int)s.size()) {
if (p2 == -1 || s[p1] == s[p2]) {
p1++, p2++;
fail.push_back(p2);
}
else {
p2 = fail[p2];
}
}
return fail;
}
vector<int> Z(string &s) {
int L = 0, R = 0;
vector<int> z(s.size()); z[0] = s.size();
for (int i = 1 ; i < (int)s.size() ; i++) {
if (i <= R && z[i - L] <= R - i) z[i] = z[i - L];
else { L = i; if (i > R) R = i;
while (R < z[0] && s[R - L] == s[R]) R++;
z[i] = (R--) - L;
}
}
return z;
}
void go() {
string s;
cin >> s;
auto fail = build(s);
auto z = Z(s);
z[0] = 0;
for (auto &i : z) {
cout << i <<' ';
}
cout << endl;
for (int i = 1 ; i < fail.size() ; i++) {
cout << fail[i] << ' ';
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
}
else {
cin >> t;
}
while (t--) {
go();
}
}