This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub hitonanode/cplib-cpp
#include "../../modint.hpp" #include "../lyndon.hpp" #include "../rolling_hash_1d.hpp" #include <iostream> #include <string> #define PROBLEM "https://judge.yosupo.jp/problem/runenumerate" using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); string S; cin >> S; auto ret = run_enumerate<rolling_hash<PairHash<ModInt998244353, ModInt998244353>>>(S); cout << ret.size() << '\n'; for (auto p : ret) cout << get<0>(p) << ' ' << get<1>(p) << ' ' << get<2>(p) << '\n'; }
#line 2 "modint.hpp" #include <cassert> #include <iostream> #include <set> #include <vector> template <int md> struct ModInt { using lint = long long; constexpr static int mod() { return md; } static int get_primitive_root() { static int primitive_root = 0; if (!primitive_root) { primitive_root = [&]() { std::set<int> fac; int v = md - 1; for (lint i = 2; i * i <= v; i++) while (v % i == 0) fac.insert(i), v /= i; if (v > 1) fac.insert(v); for (int g = 1; g < md; g++) { bool ok = true; for (auto i : fac) if (ModInt(g).pow((md - 1) / i) == 1) { ok = false; break; } if (ok) return g; } return -1; }(); } return primitive_root; } int val_; int val() const noexcept { return val_; } constexpr ModInt() : val_(0) {} constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; } constexpr ModInt(lint v) { _setval(v % md + md); } constexpr explicit operator bool() const { return val_ != 0; } constexpr ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val_ + x.val_); } constexpr ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val_ - x.val_ + md); } constexpr ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.val_ % md); } constexpr ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val_ * x.inv().val() % md); } constexpr ModInt operator-() const { return ModInt()._setval(md - val_); } constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; } constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; } constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; } constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; } friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; } friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; } friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; } friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; } constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; } constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; } constexpr bool operator<(const ModInt &x) const { return val_ < x.val_; } // To use std::map<ModInt, T> friend std::istream &operator>>(std::istream &is, ModInt &x) { lint t; return is >> t, x = ModInt(t), is; } constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val_; } constexpr ModInt pow(lint n) const { ModInt ans = 1, tmp = *this; while (n) { if (n & 1) ans *= tmp; tmp *= tmp, n >>= 1; } return ans; } static constexpr int cache_limit = std::min(md, 1 << 21); static std::vector<ModInt> facs, facinvs, invs; constexpr static void _precalculation(int N) { const int l0 = facs.size(); if (N > md) N = md; if (N <= l0) return; facs.resize(N), facinvs.resize(N), invs.resize(N); for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i; facinvs[N - 1] = facs.back().pow(md - 2); for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1); for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1]; } constexpr ModInt inv() const { if (this->val_ < cache_limit) { if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0}; while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return invs[this->val_]; } else { return this->pow(md - 2); } } constexpr ModInt fac() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facs[this->val_]; } constexpr ModInt facinv() const { while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2); return facinvs[this->val_]; } constexpr ModInt doublefac() const { lint k = (this->val_ + 1) / 2; return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac()) : ModInt(k).fac() * ModInt(2).pow(k); } constexpr ModInt nCr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv() * ModInt(r).facinv(); } constexpr ModInt nPr(int r) const { if (r < 0 or this->val_ < r) return ModInt(0); return this->fac() * (*this - r).facinv(); } static ModInt binom(int n, int r) { static long long bruteforce_times = 0; if (r < 0 or n < r) return ModInt(0); if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r); r = std::min(r, n - r); ModInt ret = ModInt(r).facinv(); for (int i = 0; i < r; ++i) ret *= n - i; bruteforce_times += r; return ret; } // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!) // Complexity: O(sum(ks)) template <class Vec> static ModInt multinomial(const Vec &ks) { ModInt ret{1}; int sum = 0; for (int k : ks) { assert(k >= 0); ret *= ModInt(k).facinv(), sum += k; } return ret * ModInt(sum).fac(); } // Catalan number, C_n = binom(2n, n) / (n + 1) // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ... // https://oeis.org/A000108 // Complexity: O(n) static ModInt catalan(int n) { if (n < 0) return ModInt(0); return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv(); } ModInt sqrt() const { if (val_ == 0) return 0; if (md == 2) return val_; if (pow((md - 1) / 2) != 1) return 0; ModInt b = 1; while (b.pow((md - 1) / 2) == 1) b += 1; int e = 0, m = md - 1; while (m % 2 == 0) m >>= 1, e++; ModInt x = pow((m - 1) / 2), y = (*this) * x * x; x *= (*this); ModInt z = b.pow(m); while (y != 1) { int j = 0; ModInt t = y; while (t != 1) j++, t *= t; z = z.pow(1LL << (e - j - 1)); x *= z, z *= z, y *= z; e = j; } return ModInt(std::min(x.val_, md - x.val_)); } }; template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1}; template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0}; using ModInt998244353 = ModInt<998244353>; // using mint = ModInt<998244353>; // using mint = ModInt<1000000007>; #line 2 "string/lyndon.hpp" #include <algorithm> #line 4 "string/lyndon.hpp" #include <functional> #include <string> #include <tuple> #include <utility> #line 9 "string/lyndon.hpp" // CUT begin // Lyndon factorization based on Duval's algorithm // **NOT VERIFIED YET** // Reference: // [1] K. T. Chen, R. H. Fox, R. C. Lyndon, // "Free Differential Calculus, IV. The Quotient Groups of the Lower Central Series," // Annals of Mathematics, 68(1), 81-95, 1958. // [2] J. P. Duval, "Factorizing words over an ordered alphabet," // Journal of Algorithms, 4(4), 363-381, 1983. // - https://cp-algorithms.com/string/lyndon_factorization.html // - https://qiita.com/nakashi18/items/66882bd6e0127174267a template <typename T> std::vector<std::pair<int, int>> lyndon_factorization(const std::vector<T> &S) { const int N = S.size(); std::vector<std::pair<int, int>> ret; for (int l = 0; l < N;) { int i = l, j = i + 1; while (j < N and S[i] <= S[j]) i = (S[i] == S[j] ? i + 1 : l), j++; int n = (j - l) / (j - i); for (int t = 0; t < n; t++) ret.emplace_back(l, j - i), l += j - i; } return ret; } std::vector<std::pair<int, int>> lyndon_factorization(const std::string &s) { const int N = int(s.size()); std::vector<int> v(N); for (int i = 0; i < N; i++) v[i] = s[i]; return lyndon_factorization<int>(v); } // Compute the longest Lyndon prefix for each suffix s[i:N] // (Our implementation is $O(N \cdot (complexity of lcplen()))$) // Example: // - `teletelepathy` -> [1,4,1,2,1,4,1,2,1,4,1,2,1] // Reference: // [1] H. Bannai et al., "The "Runs" Theorem," // SIAM Journal on Computing, 46(5), 1501-1514, 2017. template <typename String, typename LCPLENCallable> std::vector<int> longest_lyndon_prefixes(const String &s, const LCPLENCallable &lcp) { const int N = s.size(); std::vector<std::pair<int, int>> st{{N, N}}; std::vector<int> ret(N); for (int i = N - 1, j = i; i >= 0; i--, j = i) { while (st.size() > 1) { int iv = st.back().first, jv = st.back().second; int l = lcp.lcplen(i, iv); if (!(iv + l < N and s[i + l] < s[iv + l])) break; j = jv; st.pop_back(); } st.emplace_back(i, j); ret[i] = j - i + 1; } return ret; } // Compute all runs in given string // Complexity: $O(N \cdot (complexity of lcplen()))$ in this implementation // (Theoretically $O(N)$ achievable) // N = 2e5 -> ~120 ms // Reference: // [1] H. Bannai et al., "The "Runs" Theorem," // SIAM Journal on Computing, 46(5), 1501-1514, 2017. template <typename LCPLENCallable, typename String> std::vector<std::tuple<int, int, int>> run_enumerate(String s) { if (s.empty()) return {}; LCPLENCallable lcp(s); std::reverse(s.begin(), s.end()); LCPLENCallable revlcp(s); std::reverse(s.begin(), s.end()); auto t = s; auto lo = *std::min_element(s.begin(), s.end()), hi = *std::max_element(s.begin(), s.end()); for (auto &c : t) c = hi - (c - lo); auto l1 = longest_lyndon_prefixes(s, lcp), l2 = longest_lyndon_prefixes(t, lcp); int N = s.size(); std::vector<std::tuple<int, int, int>> ret; for (int i = 0; i < N; i++) { int j = i + l1[i], L = i - revlcp.lcplen(N - i, N - j), R = j + lcp.lcplen(i, j); if (R - L >= (j - i) * 2) ret.emplace_back(j - i, L, R); if (l1[i] != l2[i]) { j = i + l2[i], L = i - revlcp.lcplen(N - i, N - j), R = j + lcp.lcplen(i, j); if (R - L >= (j - i) * 2) ret.emplace_back(j - i, L, R); } } std::sort(ret.begin(), ret.end()); ret.erase(std::unique(ret.begin(), ret.end()), ret.end()); return ret; } // Enumerate Lyndon words up to length n in lexical order // https://github.com/bqi343/USACO/blob/master/Implementations/content/combinatorial%20(11.2)/DeBruijnSeq.h // Example: k=2, n=4 => [[0,],[0,0,0,1,],[0,0,1,],[0,0,1,1,],[0,1,],[0,1,1,],[0,1,1,1,],[1,],] // Verified: https://codeforces.com/gym/102001/problem/C / https://codeforces.com/gym/100162/problem/G std::vector<std::vector<int>> enumerate_lyndon_words(int k, int n) { assert(k > 0); assert(n > 0); std::vector<std::vector<int>> ret; std::vector<int> aux(n + 1); std::function<void(int, int)> gen = [&](int t, int p) { // t: current length // p: current min cycle length if (t == n) { std::vector<int> tmp(aux.begin() + 1, aux.begin() + p + 1); ret.push_back(std::move(tmp)); } else { ++t; aux[t] = aux[t - p]; gen(t, p); while (++aux[t] < k) gen(t, t); } }; gen(0, 1); return ret; } #line 3 "string/rolling_hash_1d.hpp" #include <chrono> #include <random> #line 8 "string/rolling_hash_1d.hpp" template <class T1, class T2> struct PairHash : public std::pair<T1, T2> { using PH = PairHash<T1, T2>; explicit PairHash(T1 x, T2 y) : std::pair<T1, T2>(x, y) {} explicit PairHash(int x) : std::pair<T1, T2>(x, x) {} PairHash() : PairHash(0) {} PH operator+(const PH &x) const { return PH(this->first + x.first, this->second + x.second); } PH operator-(const PH &x) const { return PH(this->first - x.first, this->second - x.second); } PH operator*(const PH &x) const { return PH(this->first * x.first, this->second * x.second); } PH operator+(int x) const { return PH(this->first + x, this->second + x); } static PH randgen(bool force_update = false) { static PH b(0); if (b == PH(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution<int> d(1 << 30); b = PH(T1(d(mt)), T2(d(mt))); } return b; } template <class OStream> friend OStream &operator<<(OStream &os, const PH &x) { return os << "(" << x.first << ", " << x.second << ")"; } }; template <class T1, class T2, class T3> struct TupleHash3 : public std::tuple<T1, T2, T3> { using TH = TupleHash3<T1, T2, T3>; explicit TupleHash3(T1 x, T2 y, T3 z) : std::tuple<T1, T2, T3>(x, y, z) {} explicit TupleHash3(int x) : std::tuple<T1, T2, T3>(x, x, x) {} TupleHash3() : TupleHash3(0) {} inline const T1 &v1() const noexcept { return std::get<0>(*this); } inline const T2 &v2() const noexcept { return std::get<1>(*this); } inline const T3 &v3() const noexcept { return std::get<2>(*this); } TH operator+(const TH &x) const { return TH(v1() + x.v1(), v2() + x.v2(), v3() + x.v3()); } TH operator-(const TH &x) const { return TH(v1() - x.v1(), v2() - x.v2(), v3() - x.v3()); } TH operator*(const TH &x) const { return TH(v1() * x.v1(), v2() * x.v2(), v3() * x.v3()); } TH operator+(int x) const { return TH(v1() + x, v2() + x, v3() + x); } static TH randgen(bool force_update = false) { static TH b(0); if (b == TH(0) or force_update) { std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count()); std::uniform_int_distribution<int> d(1 << 30); b = TH(T1(d(mt)), T2(d(mt)), T3(d(mt))); } return b; } template <class OStream> friend OStream &operator<<(OStream &os, const TH &x) { return os << "(" << x.v1() << ", " << x.v2() << ", " << x.v3() << ")"; } }; // Rolling Hash (Rabin-Karp), 1dim template <typename V> struct rolling_hash { int N; const V B; std::vector<V> hash; // hash[i] = s[0] * B^(i - 1) + ... + s[i - 1] static std::vector<V> power; // power[i] = B^i void _extend_powvec() { if (power.size() > 1 and power.at(1) != B) power = {V(1)}; while (static_cast<int>(power.size()) <= N) { auto tmp = power.back() * B; power.push_back(tmp); } } template <typename Int> rolling_hash(const std::vector<Int> &s, V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } rolling_hash(const std::string &s = "", V b = V::randgen()) : N(s.size()), B(b), hash(N + 1) { for (int i = 0; i < N; i++) hash[i + 1] = hash[i] * B + s[i]; _extend_powvec(); } void addchar(const char &c) { V hnew = hash[N] * B + c; N++, hash.emplace_back(hnew); _extend_powvec(); } struct Hash { int length; V val; Hash() : length(0), val(V()) {} Hash(int len, const V &v) : length(len), val(v) {} bool operator==(const Hash &r) const noexcept { return length == r.length and val == r.val; } bool operator<(const Hash &x) const { // To use std::map if (length != x.length) return length < x.length; return val < x.val; } Hash operator*(const Hash &r) const { return Hash(length + r.length, val * power.at(r.length) + r.val); } template <class OStream> friend OStream &operator<<(OStream &os, const Hash &x) { return os << "(length=" << x.length << ", val=" << x.val << ")"; } }; Hash get(int l, int r) const { // s[l] * B^(r - l - 1) + ... + s[r - 1] if (l >= r) return Hash(); return Hash(r - l, hash[r] - hash[l] * power[r - l]); } int lcplen(int l1, int l2) const { return longest_common_prefix(*this, l1, *this, l2); } }; template <typename V> std::vector<V> rolling_hash<V>::power{V(1)}; // Longest common prerfix between s1[l1, N1) and s2[l2, N2) template <typename T> int longest_common_prefix(const rolling_hash<T> &rh1, int l1, const rolling_hash<T> &rh2, int l2) { int lo = 0, hi = std::min(rh1.N + 1 - l1, rh2.N + 1 - l2); while (hi - lo > 1) { const int c = (lo + hi) / 2; auto h1 = rh1.get(l1, l1 + c), h2 = rh2.get(l2, l2 + c); (h1 == h2 ? lo : hi) = c; } return lo; } // Longest common suffix between s1[0, r1) and s2[0, r2) template <typename T> int longest_common_suffix(const rolling_hash<T> &rh1, int r1, const rolling_hash<T> &rh2, int r2) { int lo = 0, hi = std::min(r1, r2) + 1; while (hi - lo > 1) { const int c = (lo + hi) / 2; auto h1 = rh1.get(r1 - c, r1), h2 = rh2.get(r2 - c, r2); (h1 == h2 ? lo : hi) = c; } return lo; } #line 6 "string/test/run_enumerate_lyndon_hash.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/runenumerate" using namespace std; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); string S; cin >> S; auto ret = run_enumerate<rolling_hash<PairHash<ModInt998244353, ModInt998244353>>>(S); cout << ret.size() << '\n'; for (auto p : ret) cout << get<0>(p) << ' ' << get<1>(p) << ' ' << get<2>(p) << '\n'; }