Z Algorithm (Library Checker)
Library Checker uses the convention that .
Uses: Z function
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> z_function(string const& S) {
int N = S.size();
vector<int> z(N);
int l = 0, r = 0;
for (int i = 1; i < N; i++) {
if (i < r) z[i] = min(r-i, z[i-l]);
while (i + z[i] < N && S[z[i]] == S[i+z[i]]) z[i]++;
if (i + z[i] > r) {
l = i;
r = i+z[i];
}
}
return z;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S;
cin >> S;
auto Z = z_function(S);
Z[0] = S.size();
for (auto x : Z) cout << x << ' ';
cout << endl;
}