-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Functions.cpp
More file actions
executable file
·59 lines (56 loc) · 1.02 KB
/
Copy pathString Functions.cpp
File metadata and controls
executable file
·59 lines (56 loc) · 1.02 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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<int> KMP(string s) {
vector<int> P(s.size());
P[0] = 0;
int t = P[0];
for (int i = 1; i < int(s.size()); i++) {
while (t > 0 && s[t] != s[i]) t = P[t - 1];
if (s[t] == s[i]) t++;
P[i] = t;
}
return P;
}
vector<int> z_function(string s) {
int l = 0, r = 0;
int n = s.size();
vector<int> z(n);
for (int i = 1; i < n; i++) {
if (i >= r) {
while (i + z[i] < n && s[i + z[i]] == s[z[i]]) {
z[i]++;
}
if (z[i]) {
l = i;
r = z[i] + i - 1;
}
} else {
z[i] = min(z[i - l], r - i + 1);
if (z[i - l] >= r - i + 1) {
while (i + z[i] < n && s[i + z[i]] == s[z[i]]) {
z[i]++;
}
if (z[i] > z[i - l]) {
l = i;
r = z[i] + i - 1;
}
}
}
}
return z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
auto a = z_function(s);
for (auto x : a) cout << x << " ";
cout << "\n";
a = KMP(s);
for (auto x : a) cout << x << " ";
cout << "\n";
return 0;
}