templates

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

View the Project on GitHub plasmatic1/templates

:heavy_check_mark: tests/string/z_algorithm.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#include "../../template.hpp"
#include "../test_utils.hpp"
#include "../../string/z_algorithm.hpp"

const int MN = 5e5 + 1;
int N;
string s;

int main() {
    fast_io();

    cin >> s; N = s.length();
    for (auto x : z_algorithm(N, s))
        cout << x << ' ';
    cout << '\n';

    return 0;
}
#line 1 "tests/string/z_algorithm.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/zalgorithm"
#line 2 "template.hpp"
#include <bits/stdc++.h>
#define DEBUG 1
using namespace std;

// Defines
#define fs first
#define sn second
#define pb push_back
#define eb emplace_back
#define mpr make_pair
#define mtp make_tuple
#define all(x) (x).begin(), (x).end()
// Basic type definitions
#if __cplusplus == 201703L // CPP17 only things
template <typename T> using opt_ref = optional<reference_wrapper<T>>; // for some templates
#endif
using ll = long long; using ull = unsigned long long; using ld = long double;
using pii = pair<int, int>; using pll = pair<long long, long long>;
#ifdef __GNUG__
// PBDS order statistic tree
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T, class comp = less<T>> using os_tree = tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V, class comp = less<K>> using treemap = tree<K, V, comp, rb_tree_tag, tree_order_statistics_node_update>;
// HashSet
#include <ext/pb_ds/assoc_container.hpp>
template <typename T, class Hash> using hashset = gp_hash_table<T, null_type, Hash>;
template <typename K, typename V, class Hash> using hashmap = gp_hash_table<K, V, Hash>;
const ll RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash { ll operator()(ll x) const { return x ^ RANDOM; } };
#endif
// More utilities
int SZ(string &v) { return v.length(); }
template <typename C> int SZ(C &v) { return v.size(); }
template <typename C> void UNIQUE(vector<C> &v) { sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); }
template <typename T, typename U> void maxa(T &a, U b) { a = max(a, b); }
template <typename T, typename U> void mina(T &a, U b) { a = min(a, b); }
const ll INF = 0x3f3f3f3f, LLINF = 0x3f3f3f3f3f3f3f3f;
#line 3 "tests/test_utils.hpp"

// I/O
template <typename T> void print(T v) {
    cout << v << '\n';
}

template <typename T, typename... Rest> void print(T v, Rest... vs) {
    cout << v << ' ';
    print(vs...);
}

void fast_io() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

// Reading operators
template <typename T, typename U> istream& operator>>(istream& in, pair<T, U> &o) {
    return in >> o.first >> o.second;
}

// Read helpers
int readi() {
    int x; cin >> x;
    return x;
}

ll readl() {
    ll x; cin >> x;
    return x;
}

template <typename T> vector<T> readv(int n) {
    vector<T> res(n);
    for (auto &x : res) cin >> x;
    return res;
}

// Functional stuff
template <typename T> vector<pair<int, T>> enumerate(vector<T> v, int start = 0) {
    vector<pair<int, T>> res;
    for (auto &x : v)
        res.emplace_back(start++, x);
    return res;
}

#line 3 "string/z_algorithm.hpp"

/*
 * z[i] <- LCP(s[0..], s[i..])
 *
 * Finding z[0..N-1] quickly relies on finding an estimation for z[i] (that is less than the actual value), and
 * increasing it until it reaches the correct value.  This works because z[i] stores the LCP of some strings, so if z[i]
 * is a common prefix, then clearly z[i]-1 is one as well.
 *
 * To do this, we maintain the rightmost interval [l = i, r = i+z[i]-1] and solve for i-s sequentially.  At any point,
 * we have a few cases:
 *
 * (1) i > r : We start our estimate of z[i] as 0, and extend
 * (2) i <= r : Also note that in this case, l<=i<=r as we process i-s in order.  We know that s[l..r] == s[0..r-l] as
 * it is an interval that is defined by [k, k+z[k]-1] (for some k).  Thus, we use one of our previously computed values
 * to initialize z[i].  In this case, we use min(z[i-l], r-i+1), which is when we shift the interval [k, k+z[k]-1] to
 * [0, z[k]-1].
 *
 * Complexity Proof:
 *
 * (1) : We are always extending r and since r<N, we will extend at most N times
 * (2) :
 *   (2a) z[i-l] == r-i+1 : Again, we are extending r so this will happen at most N times (combined with case 1)
 *   (2b) z[i-l] < r-i+1 : We will extend 0 times, as if we could extend z[i], we could also extend z[i-l] as we know
 *        that s[l..r] == s[0..r-l] (and we are still within that range, so the extended character will be the same
 *        at both locations)
 *
 * Note: while z[0] is technically undefined, we'll define it as N for now
 */
template <typename Container> vector<int> z_algorithm(int N, const Container &s) {
    vector<int> z(N); z[0] = N;
    int l = 0, r = -1;
    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) {
            r = i+z[i]-1;
            l = i;
        }
    }
    return z;
}
#line 5 "tests/string/z_algorithm.test.cpp"

const int MN = 5e5 + 1;
int N;
string s;

int main() {
    fast_io();

    cin >> s; N = s.length();
    for (auto x : z_algorithm(N, s))
        cout << x << ' ';
    cout << '\n';

    return 0;
}
Back to top page