ku-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub kogetsu0728/ku-library

:heavy_check_mark: test/string/rolling_hash.test.cpp

Depends on

Required by

Code

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

#include "../../string/rolling_hash.hpp"
#include "../../math/static_mod_int.hpp"
#include "../../template/template.hpp"

int main() {
    string S;
    cin >> S;

    int N = S.length();

    RollingHash<StaticModInt<999999937>> rh1(S);
    RollingHash<StaticModInt<998244353>> rh2(S, 4649);

    assert(rh1.size() == N);
    assert(rh2.size() == N);

    vector<int> ans(N);

    rep (i, 0, N) {
        auto check = [&](int m) -> bool {
            if (rh1.get(0, m) != rh1.get(i, i + m)) {
                return false;
            }

            if (rh2.get(0, m) != rh2.get(i, i + m)) {
                return false;
            }

            return true;
        };

        int ok = 0, ng = N - i + 1;
        while (1 < abs(ok - ng)) {
            int mid = (ok + ng) / 2;

            if (check(mid)) {
                ok = mid;
            } else {
                ng = mid;
            }
        }

        ans[i] = ok;
    }

    rep (i, 0, N) {
        if (0 < i) {
            cout << ' ';
        }
        cout << ans[i];
    }
    cout << LF;

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

#line 2 "string/rolling_hash.hpp"

#line 2 "template/template.hpp"

/**
 * @author ku_senjan
 * @title 提出用テンプレート
 * @see https://github.com/kogetsu0728/ku-library
 */

#line 2 "template/constant.hpp"

#line 2 "template/include.hpp"

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
using namespace std;
#line 2 "template/type_alias.hpp"

#line 4 "template/type_alias.hpp"

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

using ld = long double;

template <class T, bool REVERSE = false>
using heap = priority_queue<T, vector<T>, conditional_t<REVERSE, greater<T>, less<T>>>;
#line 5 "template/constant.hpp"

template <class T>
inline constexpr T INF = numeric_limits<T>::max() / 2;

inline constexpr array<int, 4> DY4 = {0, -1, 0, 1};
inline constexpr array<int, 4> DX4 = {1, 0, -1, 0};
inline constexpr array<int, 8> DY8 = {0, -1, -1, -1, 0, 1, 1, 1};
inline constexpr array<int, 8> DX8 = {1, 1, 0, -1, -1, -1, 0, 1};

inline constexpr char LF = '\n';
#line 2 "template/macro.hpp"

#line 5 "template/macro.hpp"

/**
 * @see https://trap.jp/post/1224/
 */

#ifdef LOCAL
inline constexpr bool IS_LOCAL = true;
#else
inline constexpr bool IS_LOCAL = false;
#endif

#define IF_LOCAL if constexpr (IS_LOCAL)

#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)

#define overload4(a, b, c, d, e, ...) e

#define rep1(i, a) for (ll i = 0; (i) < (ll)(a); ++(i))
#define rep2(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); ++(i))
#define rep3(i, a, b, c) for (ll i = (ll)(a); (i) < (ll)(b); (i) += (ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep1(i, a) for (ll i = (ll)(a); (i) >= 0; --(i))
#define rrep2(i, a, b) for (ll i = (ll)(a); (i) >= (ll)(b); --(i))
#define rrep3(i, a, b, c) for (ll i = (ll)(a); (i) >= (ll)(b); (i) -= (ll)(c))
#define rrep(...) overload4(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)
#line 13 "template/template.hpp"

#line 2 "utility/choose_min_max.hpp"

/**
 * @title Choose Minimum / Maximum
 */

template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return true;
    }

    return false;
}

template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }

    return false;
}
#line 2 "utility/set_io.hpp"

#line 4 "utility/set_io.hpp"

void set_io(int d = 16) {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(d);

    return;
}
#line 4 "string/rolling_hash.hpp"

/**
 * @brief Rolling Hash (ローリングハッシュ)
 */
template <class T>
class RollingHash {
  private:
    vector<T> hash, pow;

  public:
    RollingHash() : RollingHash("") {}
    RollingHash(const string& _s, const T& _base = T(2525))
        : hash(_s.length() + 1), pow(_s.length() + 1) {
        hash[0] = T(0);
        pow[0] = T(1);

        rep (i, 0, _s.length()) {
            hash[i + 1] = hash[i] * _base + T(_s[i]);
            pow[i + 1] = pow[i] * _base;
        }
    }

    int size() const { return hash.size() - 1; }

    T shift(const T& h, int d) const { return h * pow[d]; }

    T get() const { return hash.back(); }

    T get(int l, int r) const { return hash[r] - shift(hash[l], r - l); }
};
#line 2 "math/static_mod_int.hpp"

#line 4 "math/static_mod_int.hpp"

/**
 * @brief Static Mod Int
 */
template <int M>
class StaticModInt {
    static_assert(2 < M);
    static_assert(M <= INT_MAX / 2);

  private:
    int v;

  public:
    StaticModInt() : v(0) {}
    StaticModInt(ll _v) : v(int(((_v % M) + M) % M)) {}

    static StaticModInt raw(int _v) {
        StaticModInt res;
        res.v = _v;

        return res;
    }

    static int mod() { return M; }
    int val() const { return v; }

    friend bool operator==(const StaticModInt& lhs, const StaticModInt& rhs) {
        return lhs.v == rhs.v;
    }

    friend bool operator!=(const StaticModInt& lhs, const StaticModInt& rhs) {
        return !(lhs == rhs);
    }

    StaticModInt& operator+=(const StaticModInt& rhs) {
        v += rhs.val();
        if (M <= v) {
            v -= M;
        }

        return *this;
    }

    StaticModInt& operator-=(const StaticModInt& rhs) {
        if (v < rhs.val()) {
            v += M;
        }
        v -= rhs.val();

        return *this;
    }

    StaticModInt& operator*=(const StaticModInt& rhs) {
        v = (ull(v) * rhs.val()) % M;

        return *this;
    }

    StaticModInt& operator/=(const StaticModInt& rhs) {
        assert(rhs.val() != 0);

        return *this *= rhs.inv();
    }

    friend StaticModInt operator+(const StaticModInt& lhs, const StaticModInt& rhs) {
        return StaticModInt(lhs) += rhs;
    }

    friend StaticModInt operator-(const StaticModInt& lhs, const StaticModInt& rhs) {
        return StaticModInt(lhs) -= rhs;
    }

    friend StaticModInt operator*(const StaticModInt& lhs, const StaticModInt& rhs) {
        return StaticModInt(lhs) *= rhs;
    }

    friend StaticModInt operator/(const StaticModInt& lhs, const StaticModInt& rhs) {
        return StaticModInt(lhs) /= rhs;
    }

    StaticModInt pow(ll y) const {
        StaticModInt res = raw(1);
        StaticModInt x = *this;

        while (0 < y) {
            if (y & 1) {
                res *= x;
            }

            x *= x;
            y >>= 1;
        }

        return res;
    }

    StaticModInt inv() const { return pow(M - 2); }
};
#line 6 "test/string/rolling_hash.test.cpp"

int main() {
    string S;
    cin >> S;

    int N = S.length();

    RollingHash<StaticModInt<999999937>> rh1(S);
    RollingHash<StaticModInt<998244353>> rh2(S, 4649);

    assert(rh1.size() == N);
    assert(rh2.size() == N);

    vector<int> ans(N);

    rep (i, 0, N) {
        auto check = [&](int m) -> bool {
            if (rh1.get(0, m) != rh1.get(i, i + m)) {
                return false;
            }

            if (rh2.get(0, m) != rh2.get(i, i + m)) {
                return false;
            }

            return true;
        };

        int ok = 0, ng = N - i + 1;
        while (1 < abs(ok - ng)) {
            int mid = (ok + ng) / 2;

            if (check(mid)) {
                ok = mid;
            } else {
                ng = mid;
            }
        }

        ans[i] = ok;
    }

    rep (i, 0, N) {
        if (0 < i) {
            cout << ' ';
        }
        cout << ans[i];
    }
    cout << LF;

    return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ all_same_00 :heavy_check_mark: AC 135 ms 14 MB
g++ all_same_01 :heavy_check_mark: AC 131 ms 14 MB
g++ all_same_02 :heavy_check_mark: AC 135 ms 13 MB
g++ all_same_03 :heavy_check_mark: AC 130 ms 14 MB
g++ all_same_04 :heavy_check_mark: AC 129 ms 13 MB
g++ binary_carry_00 :heavy_check_mark: AC 92 ms 14 MB
g++ binary_carry_01 :heavy_check_mark: AC 92 ms 14 MB
g++ example_00 :heavy_check_mark: AC 4 ms 4 MB
g++ example_01 :heavy_check_mark: AC 4 ms 3 MB
g++ example_02 :heavy_check_mark: AC 4 ms 3 MB
g++ example_03 :heavy_check_mark: AC 4 ms 4 MB
g++ fib_str_00 :heavy_check_mark: AC 93 ms 14 MB
g++ fib_str_01 :heavy_check_mark: AC 69 ms 11 MB
g++ fib_str_02 :heavy_check_mark: AC 66 ms 11 MB
g++ fib_str_03 :heavy_check_mark: AC 61 ms 10 MB
g++ fib_str_04 :heavy_check_mark: AC 91 ms 13 MB
g++ hack606_00 :heavy_check_mark: AC 5 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 94 ms 14 MB
g++ max_random_01 :heavy_check_mark: AC 94 ms 14 MB
g++ random_00 :heavy_check_mark: AC 74 ms 11 MB
g++ random_01 :heavy_check_mark: AC 85 ms 13 MB
g++ random_02 :heavy_check_mark: AC 13 ms 4 MB
g++ random_03 :heavy_check_mark: AC 78 ms 12 MB
g++ random_04 :heavy_check_mark: AC 53 ms 9 MB
g++ random_05 :heavy_check_mark: AC 60 ms 10 MB
g++ random_06 :heavy_check_mark: AC 84 ms 13 MB
g++ random_07 :heavy_check_mark: AC 20 ms 6 MB
g++ random_08 :heavy_check_mark: AC 50 ms 9 MB
g++ random_09 :heavy_check_mark: AC 22 ms 6 MB
Back to top page