-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathz-function.cpp
More file actions
34 lines (27 loc) · 851 Bytes
/
z-function.cpp
File metadata and controls
34 lines (27 loc) · 851 Bytes
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
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
/*
- Find a vector, z, of length n such that z[i] is the length of the longest
prefix of s such that s[i:i+z[i]] is equal to s[:z[i]]
- The main idea is to keep track of the rightmost substring that is the prefix
of the string
- For more: https://cp-algorithms.com/string/z-function.html
*/
vector<int> zfunc(string& s) {
int n = s.size();
vector<int> z(n);
int l = 0, r = 0; // [l, r] - the rightmost substring that feats with prefix of s
for (int i = 1; i < n; ++i) {
if (r > i) {
z[i] = min(z[i - l], r - i + 1);
}
while (i + z[i] < n && s[i + z[i]] == s[z[i]]) {
z[i]++;
}
if (i + z[i] - 1 >= r) {
r = i + z[i] - 1, l = i;
}
}
return z;
}