-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_algorithm.cpp
47 lines (47 loc) · 1.18 KB
/
z_algorithm.cpp
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
// Z Algorithm
// Given a string S of length n, the Z Algorithm produces an array Z
// where Z[i] is the length of the longest substring starting from S[i] which is also a prefix of S
// BOJ 13713 AC Code
// https://www.acmicpc.net/problem/13713
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
const int MAXS = 1010101;
string s;
int z[MAXS];
void input() {
string du; cin >> du;
for (int i = sz(du) - 1; i >= 0; i--)
s.push_back(du[i]);
}
void zfunction() {
z[0] = sz(s);
int l = 0, r = 0;
for (int i = 1; i < sz(s); i++) {
if (i > r) {
l = r = i;
while (r < sz(s) && s[r - l] == s[r]) 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 < sz(s) && s[r - l] == s[r]) r++;
z[i] = r - l; r--;
}
}
}
}
int main() {
cin.tie(NULL); cout.tie(NULL);
ios_base::sync_with_stdio(false);
input();
zfunction();
int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << z[sz(s) - x] << '\n';
}
}