cp_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub downerkei/cp_library

:heavy_check_mark: verify/yosupo/yosupo_zalgorithm.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"

#include <bits/stdc++.h>
using namespace std;

#include "../../string/z_algorithm.hpp"

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    for(auto ans : z_algorithm(S)) {
        cout << ans << " ";
    }
    cout << endl;

    return 0;
}
#line 1 "verify/yosupo/yosupo_zalgorithm.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"

#include <bits/stdc++.h>
using namespace std;

#line 1 "string/z_algorithm.hpp"
vector<int> z_algorithm(const string& s) {
    int n = s.size();
    vector<int> z(n);
    z[0] = n;
    int i = 1, j = 0;
    while(i < n) {
        while(i + j < n && s[j] == s[i + j]) j++;
        z[i] = j;
        if(j == 0) { i++; continue; } 
        int k = 1;
        while(k < j && k + z[k] < j) { z[i + k] = z[k]; k++; }
        i += k; j -= k;
    }
    return z;
}
#line 7 "verify/yosupo/yosupo_zalgorithm.test.cpp"

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    for(auto ans : z_algorithm(S)) {
        cout << ans << " ";
    }
    cout << endl;

    return 0;
}
Back to top page