-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzalgorithm.cpp
More file actions
69 lines (63 loc) · 1.2 KB
/
Copy pathzalgorithm.cpp
File metadata and controls
69 lines (63 loc) · 1.2 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
66
67
68
69
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N = 1e6 + 10;
char s[N];
int z[N];
int n;
vector<int> compute(string s) {
int n = s.length();
vector<int> z(n, 0);
z[0] = n;
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
z[i] = max(0, min(z[i - L], R - i + 1));
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
L = i; R = i + z[i]; z[i]++;
}
}
return z;
}
vector<int> z_arr(string s) {
int n = s.length();
vector<int> z(n, 0);
z[0] = n;
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
z[i] = max(0, min(z[i - L], R - i + 1));
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
L = i; R = i + z[i]; z[i]++;
}
}
return z;
}
int main() {
scanf("%s", s);
n = strlen(s);
z[0] = n;
int L = -1, R = -1;
for (int i = 1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && s[R] == s[R - L]) R++;
z[i] = R - L;
R--;
} else {
int k = i - L;
if (z[k] < R - i + 1) {
z[i] = z[k];
} else {
L = i;
while (R < n && s[R] == s[R - L]) R++;
z[i] = R - L;
R--;
}
}
}
return 0;
}