This documentation is automatically generated by online-judge-tools/verification-helper
#include "../../number/modint_mersenne61.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<ModIntMersenne61>>(S);
cout << ret.size() << '\n';
for (auto p : ret) cout << get<0>(p) << ' ' << get<1>(p) << ' ' << get<2>(p) << '\n';
}
#line 2 "number/modint_mersenne61.hpp"
#include <cassert>
#include <chrono>
#include <random>
// F_p, p = 2^61 - 1
// https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
class ModIntMersenne61 {
static const long long md = (1LL << 61) - 1;
long long _v;
inline unsigned hi() const noexcept { return _v >> 31; }
inline unsigned lo() const noexcept { return _v & ((1LL << 31) - 1); }
public:
static long long mod() { return md; }
ModIntMersenne61() : _v(0) {}
// 0 <= x < md * 2
explicit ModIntMersenne61(long long x) : _v(x >= md ? x - md : x) {
assert(0 <= x and x < md * 2);
}
long long val() const noexcept { return _v; }
ModIntMersenne61 operator+(const ModIntMersenne61 &x) const {
return ModIntMersenne61(_v + x._v);
}
ModIntMersenne61 operator-(const ModIntMersenne61 &x) const {
return ModIntMersenne61(_v + md - x._v);
}
ModIntMersenne61 operator*(const ModIntMersenne61 &x) const {
using ull = unsigned long long;
ull uu = (ull)hi() * x.hi() * 2;
ull ll = (ull)lo() * x.lo();
ull lu = (ull)hi() * x.lo() + (ull)lo() * x.hi();
ull sum = uu + ll + ((lu & ((1ULL << 30) - 1)) << 31) + (lu >> 30);
ull reduced = (sum >> 61) + (sum & ull(md));
return ModIntMersenne61(reduced);
}
ModIntMersenne61 pow(long long n) const {
assert(n >= 0);
ModIntMersenne61 ans(1), tmp = *this;
while (n) {
if (n & 1) ans *= tmp;
tmp *= tmp, n >>= 1;
}
return ans;
}
ModIntMersenne61 inv() const { return pow(md - 2); }
ModIntMersenne61 operator/(const ModIntMersenne61 &x) const { return *this * x.inv(); }
ModIntMersenne61 operator-() const { return ModIntMersenne61(md - _v); }
ModIntMersenne61 &operator+=(const ModIntMersenne61 &x) { return *this = *this + x; }
ModIntMersenne61 &operator-=(const ModIntMersenne61 &x) { return *this = *this - x; }
ModIntMersenne61 &operator*=(const ModIntMersenne61 &x) { return *this = *this * x; }
ModIntMersenne61 &operator/=(const ModIntMersenne61 &x) { return *this = *this / x; }
ModIntMersenne61 operator+(unsigned x) const { return ModIntMersenne61(this->_v + x); }
bool operator==(const ModIntMersenne61 &x) const { return _v == x._v; }
bool operator!=(const ModIntMersenne61 &x) const { return _v != x._v; }
bool operator<(const ModIntMersenne61 &x) const { return _v < x._v; } // To use std::map
template <class OStream> friend OStream &operator<<(OStream &os, const ModIntMersenne61 &x) {
return os << x._v;
}
static ModIntMersenne61 randgen(bool force_update = false) {
static ModIntMersenne61 b(0);
if (b == ModIntMersenne61(0) or force_update) {
std::mt19937 mt(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_int_distribution<long long> d(1, ModIntMersenne61::mod());
b = ModIntMersenne61(d(mt));
}
return b;
}
};
#line 2 "string/lyndon.hpp"
#include <algorithm>
#line 4 "string/lyndon.hpp"
#include <functional>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// 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 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 4 "string/test/run_enumerate_lyndon_mersenne61.test.cpp"
#include <iostream>
#line 6 "string/test/run_enumerate_lyndon_mersenne61.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<ModIntMersenne61>>(S);
cout << ret.size() << '\n';
for (auto p : ret) cout << get<0>(p) << ' ' << get<1>(p) << ' ' << get<2>(p) << '\n';
}