String/Z_Algorithm.test.cpp
Depends on
Code
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#include "../template.h"
#include "Z_Function.h"
void solve() {
string s; cin >> s;
vector<int> ans = Z_function(s);
for (int x : ans) cout << x << " ";
}
#line 1 "String/Z_Algorithm.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#line 2 "template.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
const int mod = 998244353;
void solve();
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
// cin.exceptions(cin.failbit);
// int t; cin >> t;
// while(t--)
solve();
cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
return 0;
}
#line 2 "String/Z_Function.h"
vector<int> Z_function(const string &s) {
int n = s.size();
vector<int> z(n);
z[0] = (int)s.size();
int l = 0, r = 0;
for (int i = 1; i < n; i++) {
if (i <= r) {
z[i] = min(r-i+1, z[i-l]);
}
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
z[i]++;
}
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
#line 5 "String/Z_Algorithm.test.cpp"
void solve() {
string s; cin >> s;
vector<int> ans = Z_function(s);
for (int x : ans) cout << x << " ";
}
Back to top page