This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub maspypy/library
#include "string/many_string_compare.hpp"
#include "string/suffix_array.hpp" // https://qoj.ac/contest/1803/problem/9406 template <bool USE_SPARSE_TABLE> struct Many_String_Compare { int n; string ALL; vc<int> pos; Suffix_Array<USE_SPARSE_TABLE> X; template <typename F> Many_String_Compare(int n, F f) : n(n) { pos = {0}; FOR(i, n) { ALL += f(i); pos.eb(len(ALL)); } X = Suffix_Array<USE_SPARSE_TABLE>(ALL); } // S[a][la:lb), S[b][lb:rb) int lcp(int a, int la, int ra, int b, int lb, int rb) { assert(0 <= a && a < n && 0 <= b && b < n); assert(0 <= la && la <= ra && ra <= length(a)); assert(0 <= lb && lb <= rb && rb <= length(b)); int n = X.lcp(pos[a] + la, pos[b] + lb); return min({n, ra - la, rb - lb}); } // [<]-1, [=]0, [>]1 int comp3(int a, int la, int ra, int b, int lb, int rb) { int na = ra - la, nb = rb - lb; if (na > nb) return -comp3(b, lb, rb, a, la, ra); int n = lcp(a, la, ra, b, lb, rb); if (n == na) { return (na == nb ? 0 : -1); } return (ALL[pos[a] + la + n] < ALL[pos[b] + lb + n] ? -1 : 1); } // [<]-1, [=]0, [>]1, (S+T) vs (T+S) int ST_TS_comp3(int a, int la, int ra, int b, int lb, int rb) { int na = ra - la, nb = rb - lb; if (na > nb) return -ST_TS_comp3(b, lb, rb, a, la, ra); int k = comp3(a, la, la + na, b, lb, lb + na); if (k != 0) return k; k = comp3(b, lb, lb + nb - na, b, lb + na, rb); if (k != 0) return k; return comp3(b, lb + nb - na, rb, a, la, ra); } int length(int a) { return pos[a + 1] - pos[a]; } };
#line 2 "string/suffix_array.hpp" #line 2 "alg/monoid/min.hpp" template <typename E> struct Monoid_Min { using X = E; using value_type = X; static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); } static constexpr X unit() { return infty<E>; } static constexpr bool commute = true; }; #line 2 "ds/sparse_table/sparse_table.hpp" // 冪等なモノイドであることを仮定。disjoint sparse table より x 倍高速 template <class Monoid> struct Sparse_Table { using MX = Monoid; using X = typename MX::value_type; int n, log; vvc<X> dat; Sparse_Table() {} Sparse_Table(int n) { build(n); } template <typename F> Sparse_Table(int n, F f) { build(n, f); } Sparse_Table(const vc<X>& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc<X>& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template <typename F> void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; dat.resize(log); dat[0].resize(n); FOR(i, n) dat[0][i] = f(i); FOR(i, log - 1) { dat[i + 1].resize(len(dat[i]) - (1 << i)); FOR(j, len(dat[i]) - (1 << i)) { dat[i + 1][j] = MX::op(dat[i][j], dat[i][j + (1 << i)]); } } } X prod(int L, int R) { if (L == R) return MX::unit(); if (R == L + 1) return dat[0][L]; int k = topbit(R - L - 1); return MX::op(dat[k][L], dat[k][R - (1 << k)]); } template <class F> int max_right(const F check, int L) { assert(0 <= L && L <= n && check(MX::unit())); if (L == n) return n; int ok = L, ng = n + 1; while (ok + 1 < ng) { int k = (ok + ng) / 2; bool bl = check(prod(L, k)); if (bl) ok = k; if (!bl) ng = k; } return ok; } template <class F> int min_left(const F check, int R) { assert(0 <= R && R <= n && check(MX::unit())); if (R == 0) return 0; int ok = R, ng = -1; while (ng + 1 < ok) { int k = (ok + ng) / 2; bool bl = check(prod(k, R)); if (bl) ok = k; if (!bl) ng = k; } return ok; } }; #line 2 "ds/segtree/segtree.hpp" template <class Monoid> struct SegTree { using MX = Monoid; using X = typename MX::value_type; using value_type = X; vc<X> dat; int n, log, size; SegTree() {} SegTree(int n) { build(n); } template <typename F> SegTree(int n, F f) { build(n, f); } SegTree(const vc<X>& v) { build(v); } void build(int m) { build(m, [](int i) -> X { return MX::unit(); }); } void build(const vc<X>& v) { build(len(v), [&](int i) -> X { return v[i]; }); } template <typename F> void build(int m, F f) { n = m, log = 1; while ((1 << log) < n) ++log; size = 1 << log; dat.assign(size << 1, MX::unit()); FOR(i, n) dat[size + i] = f(i); FOR_R(i, 1, size) update(i); } X get(int i) { return dat[size + i]; } vc<X> get_all() { return {dat.begin() + size, dat.begin() + size + n}; } void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); } void set(int i, const X& x) { assert(i < n); dat[i += size] = x; while (i >>= 1) update(i); } void multiply(int i, const X& x) { assert(i < n); i += size; dat[i] = Monoid::op(dat[i], x); while (i >>= 1) update(i); } X prod(int L, int R) { assert(0 <= L && L <= R && R <= n); X vl = Monoid::unit(), vr = Monoid::unit(); L += size, R += size; while (L < R) { if (L & 1) vl = Monoid::op(vl, dat[L++]); if (R & 1) vr = Monoid::op(dat[--R], vr); L >>= 1, R >>= 1; } return Monoid::op(vl, vr); } X prod_all() { return dat[1]; } template <class F> int max_right(F check, int L) { assert(0 <= L && L <= n && check(Monoid::unit())); if (L == n) return n; L += size; X sm = Monoid::unit(); do { while (L % 2 == 0) L >>= 1; if (!check(Monoid::op(sm, dat[L]))) { while (L < size) { L = 2 * L; if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); } } return L - size; } sm = Monoid::op(sm, dat[L++]); } while ((L & -L) != L); return n; } template <class F> int min_left(F check, int R) { assert(0 <= R && R <= n && check(Monoid::unit())); if (R == 0) return 0; R += size; X sm = Monoid::unit(); do { --R; while (R > 1 && (R % 2)) R >>= 1; if (!check(Monoid::op(dat[R], sm))) { while (R < size) { R = 2 * R + 1; if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); } } return R + 1 - size; } sm = Monoid::op(dat[R], sm); } while ((R & -R) != R); return 0; } // prod_{l<=i<r} A[i xor x] X xor_prod(int l, int r, int xor_val) { static_assert(Monoid::commute); X x = Monoid::unit(); for (int k = 0; k < log + 1; ++k) { if (l >= r) break; if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); } if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); } l /= 2, r /= 2, xor_val /= 2; } return x; } }; #line 6 "string/suffix_array.hpp" // 辞書順 i 番目の suffix が j 文字目始まりであるとき、 // SA[i] = j, ISA[j] = i // |S|>0 を前提(そうでない場合 dummy 文字を追加して利用せよ) template <bool USE_SPARSE_TABLE = true> struct Suffix_Array { vc<int> SA; vc<int> ISA; vc<int> LCP; using Mono = Monoid_Min<int>; using SegType = conditional_t<USE_SPARSE_TABLE, Sparse_Table<Mono>, SegTree<Mono> >; SegType seg; bool build_seg; Suffix_Array() {} Suffix_Array(string& s) { build_seg = 0; assert(len(s) > 0); char first = 127, last = 0; for (auto&& c: s) { chmin(first, c); chmax(last, c); } SA = calc_suffix_array(s, first, last); calc_LCP(s); } Suffix_Array(vc<int>& s) { build_seg = 0; assert(len(s) > 0); SA = calc_suffix_array(s); calc_LCP(s); } // lcp(S[i:], S[j:]) int lcp(int i, int j) { if (!build_seg) { build_seg = true; seg.build(LCP); } int n = len(SA); if (i == n || j == n) return 0; if (i == j) return n - i; i = ISA[i], j = ISA[j]; if (i > j) swap(i, j); return seg.prod(i, j); } // S[i:] との lcp が n 以上であるような半開区間 pair<int, int> lcp_range(int i, int n) { if (!build_seg) { build_seg = true; seg.build(LCP); } i = ISA[i]; int a = seg.min_left([&](auto e) -> bool { return e >= n; }, i); int b = seg.max_right([&](auto e) -> bool { return e >= n; }, i); return {a, b + 1}; } // -1: S[L1:R1) < S[L2, R2) // 0: S[L1:R1) = S[L2, R2) // +1: S[L1:R1) > S[L2, R2) int compare(int L1, int R1, int L2, int R2) { int n1 = R1 - L1, n2 = R2 - L2; int n = lcp(L1, L2); if (n == n1 && n == n2) return 0; if (n == n1) return -1; if (n == n2) return 1; return (ISA[L1 + n] > ISA[L2 + n] ? 1 : -1); } private: void induced_sort(const vc<int>& vect, int val_range, vc<int>& SA, const vc<bool>& sl, const vc<int>& lms_idx) { vc<int> l(val_range, 0), r(val_range, 0); for (int c: vect) { if (c + 1 < val_range) ++l[c + 1]; ++r[c]; } partial_sum(l.begin(), l.end(), l.begin()); partial_sum(r.begin(), r.end(), r.begin()); fill(SA.begin(), SA.end(), -1); for (int i = (int)lms_idx.size() - 1; i >= 0; --i) SA[--r[vect[lms_idx[i]]]] = lms_idx[i]; for (int i: SA) if (i >= 1 && sl[i - 1]) SA[l[vect[i - 1]]++] = i - 1; fill(r.begin(), r.end(), 0); for (int c: vect) ++r[c]; partial_sum(r.begin(), r.end(), r.begin()); for (int k = (int)SA.size() - 1, i = SA[k]; k >= 1; --k, i = SA[k]) if (i >= 1 && !sl[i - 1]) { SA[--r[vect[i - 1]]] = i - 1; } } vc<int> SA_IS(const vc<int>& vect, int val_range) { const int n = vect.size(); vc<int> SA(n), lms_idx; vc<bool> sl(n); sl[n - 1] = false; for (int i = n - 2; i >= 0; --i) { sl[i] = (vect[i] > vect[i + 1] || (vect[i] == vect[i + 1] && sl[i + 1])); if (sl[i] && !sl[i + 1]) lms_idx.push_back(i + 1); } reverse(lms_idx.begin(), lms_idx.end()); induced_sort(vect, val_range, SA, sl, lms_idx); vc<int> new_lms_idx(lms_idx.size()), lms_vec(lms_idx.size()); for (int i = 0, k = 0; i < n; ++i) if (!sl[SA[i]] && SA[i] >= 1 && sl[SA[i] - 1]) { new_lms_idx[k++] = SA[i]; } int cur = 0; SA[n - 1] = cur; for (size_t k = 1; k < new_lms_idx.size(); ++k) { int i = new_lms_idx[k - 1], j = new_lms_idx[k]; if (vect[i] != vect[j]) { SA[j] = ++cur; continue; } bool flag = false; for (int a = i + 1, b = j + 1;; ++a, ++b) { if (vect[a] != vect[b]) { flag = true; break; } if ((!sl[a] && sl[a - 1]) || (!sl[b] && sl[b - 1])) { flag = !((!sl[a] && sl[a - 1]) && (!sl[b] && sl[b - 1])); break; } } SA[j] = (flag ? ++cur : cur); } for (size_t i = 0; i < lms_idx.size(); ++i) lms_vec[i] = SA[lms_idx[i]]; if (cur + 1 < (int)lms_idx.size()) { auto lms_SA = SA_IS(lms_vec, cur + 1); for (size_t i = 0; i < lms_idx.size(); ++i) { new_lms_idx[i] = lms_idx[lms_SA[i]]; } } induced_sort(vect, val_range, SA, sl, new_lms_idx); return SA; } vc<int> calc_suffix_array(const string& s, const char first = 'a', const char last = 'z') { vc<int> vect(s.size() + 1); copy(begin(s), end(s), begin(vect)); for (auto& x: vect) x -= (int)first - 1; vect.back() = 0; auto ret = SA_IS(vect, (int)last - (int)first + 2); ret.erase(ret.begin()); return ret; } vc<int> calc_suffix_array(const vc<int>& s) { vc<int> ss = s; UNIQUE(ss); vc<int> vect(s.size() + 1); copy(all(s), vect.begin()); for (auto& x: vect) x = LB(ss, x) + 1; vect.back() = 0; auto ret = SA_IS(vect, MAX(vect) + 2); ret.erase(ret.begin()); return ret; } template <typename STRING> void calc_LCP(const STRING& s) { int n = s.size(), k = 0; ISA.resize(n); LCP.resize(n); for (int i = 0; i < n; i++) ISA[SA[i]] = i; for (int i = 0; i < n; i++, k ? k-- : 0) { if (ISA[i] == n - 1) { k = 0; continue; } int j = SA[ISA[i] + 1]; while (i + k < n && j + k < n && s[i + k] == s[j + k]) k++; LCP[ISA[i]] = k; } LCP.resize(n - 1); } }; #line 2 "string/many_string_compare.hpp" // https://qoj.ac/contest/1803/problem/9406 template <bool USE_SPARSE_TABLE> struct Many_String_Compare { int n; string ALL; vc<int> pos; Suffix_Array<USE_SPARSE_TABLE> X; template <typename F> Many_String_Compare(int n, F f) : n(n) { pos = {0}; FOR(i, n) { ALL += f(i); pos.eb(len(ALL)); } X = Suffix_Array<USE_SPARSE_TABLE>(ALL); } // S[a][la:lb), S[b][lb:rb) int lcp(int a, int la, int ra, int b, int lb, int rb) { assert(0 <= a && a < n && 0 <= b && b < n); assert(0 <= la && la <= ra && ra <= length(a)); assert(0 <= lb && lb <= rb && rb <= length(b)); int n = X.lcp(pos[a] + la, pos[b] + lb); return min({n, ra - la, rb - lb}); } // [<]-1, [=]0, [>]1 int comp3(int a, int la, int ra, int b, int lb, int rb) { int na = ra - la, nb = rb - lb; if (na > nb) return -comp3(b, lb, rb, a, la, ra); int n = lcp(a, la, ra, b, lb, rb); if (n == na) { return (na == nb ? 0 : -1); } return (ALL[pos[a] + la + n] < ALL[pos[b] + lb + n] ? -1 : 1); } // [<]-1, [=]0, [>]1, (S+T) vs (T+S) int ST_TS_comp3(int a, int la, int ra, int b, int lb, int rb) { int na = ra - la, nb = rb - lb; if (na > nb) return -ST_TS_comp3(b, lb, rb, a, la, ra); int k = comp3(a, la, la + na, b, lb, lb + na); if (k != 0) return k; k = comp3(b, lb, lb + nb - na, b, lb + na, rb); if (k != 0) return k; return comp3(b, lb + nb - na, rb, a, la, ra); } int length(int a) { return pos[a + 1] - pos[a]; } };