QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#319685#4057. 子串统计hos_lyric100 ✓559ms33276kbC++1433.7kb2024-02-03 02:31:332024-02-03 02:31:33

Judging History

你现在查看的是最新测评结果

  • [2024-02-03 02:31:33]
  • 评测
  • 测评结果:100
  • 用时:559ms
  • 内存:33276kb
  • [2024-02-03 02:31:33]
  • 提交

answer

// virtual after knowing this is a substring structure problem, sorry

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


////////////////////////////////////////////////////////////////////////////////
// SA-IS
//   String: string, vector<int>, vector<long long>
//   if sigma <= n,  O(n) time, O(n) space
//   if sigma >  n,  O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
  const int n = as.size();
  if (n == 0) return {};
  const auto minmaxA = minmax_element(as.begin(), as.end());
  const auto minA = *minmaxA.first, maxA = *minmaxA.second;
  if (static_cast<unsigned long long>(maxA) - minA >=
      static_cast<unsigned long long>(n)) {
    vector<int> us(n);
    for (int u = 0; u < n; ++u) us[u] = u;
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (as[u] < as[v]);
    });
    int b = 0;
    vector<int> bs(n, 0);
    for (int i = 1; i < n; ++i) {
      if (as[us[i - 1]] != as[us[i]]) ++b;
      bs[us[i]] = b;
    }
    return suffixArrayRec(bs);
  }
  const int sigma = maxA - minA + 1;
  vector<int> pt(sigma + 1, 0), poss(sigma);
  for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
  for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
  // cmp[u] := (as[u, n) < as[u + 1, n))
  vector<bool> cmp(n);
  cmp[n - 1] = false;
  for (int u = n - 1; --u >= 0; ) {
    cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
  }
  // ><,  nn - id (0 <= id < n)
  int nn = 0;
  vector<int> ids(n, 0);
  int last = n;
  vector<int> nxt(n, 0);
  // put ><, from the tail of each bucket
  vector<int> us(n, 0);
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
    ids[u] = ++nn;
    nxt[u] = last;
    last = u;
    us[--poss[as[u] - minA]] = u;
  }
  // follow > backwards, from the head of each bucket
  memcpy(poss.data(), pt.data(), sigma * sizeof(int));
  us[poss[as[n - 1] - minA]++] = n - 1;
  for (int i = 0; i < n; ++i) {
    const int u = us[i];
    if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
  }
  // follow < backwards, from the tail of each bucket
  memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
  for (int i = n; --i >= 0; ) {
    const int u = us[i];
    if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
  }
  if (nn) {
    int vsLen = 0;
    vector<int> vs(nn);
    for (const int u : us) if (ids[u]) vs[vsLen++] = u;
    int b = 0;
    vector<int> bs(nn, 0);
    for (int j = 1; j < nn; ++j) {
      // as[v1, w1] (< or =) as[v0, w0]
      int v1 = vs[j - 1], v0 = vs[j];
      const int w1 = nxt[v1], w0 = nxt[v0];
      if (w1 - v1 != w0 - v0) {
        ++b;
      } else {
        for (; ; ++v1, ++v0) {
          if (v1 == n) { ++b; break; }
          if (as[v1] != as[v0]) { ++b; break; }
          if (v1 == w1) break;
        }
      }
      bs[nn - ids[vs[j]]] = b;
    }
    for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
    const auto sub = suffixArrayRec(bs);
    // put ><, from the tail of each bucket
    memset(us.data(), 0, n * sizeof(int));
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int j = nn; --j >= 0; ) {
      const int u = vs[sub[j]];
      us[--poss[as[u] - minA]] = u;
    }
    // follow > backwards, from the head of each bucket
    memcpy(poss.data(), pt.data(), sigma * sizeof(int));
    us[poss[as[n - 1] - minA]++] = n - 1;
    for (int i = 0; i < n; ++i) {
      const int u = us[i];
      if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
    }
    // follow < backwards, from the tail of each bucket
    memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
    for (int i = n; --i >= 0; ) {
      const int u = us[i];
      if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
    }
  }
  return us;
}

// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
  int n;
  bool rmq;
  vector<int> us, su, hs;
  SuffixArray() : n(0), rmq(false) {}
  SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
  SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
  template <class String> void build(const String &as) {
    n = as.size();
    us = suffixArrayRec(as);
    su.resize(n + 1);
    for (int i = 0; i < n; ++i) su[us[i]] = i;
    su[n] = -1;
    hs.assign(n, 0);
    for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
      for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
      hs[su[u]] = h;
      if (h) --h;
    }
    if (rmq) {
      const int logN = n ? (31 - __builtin_clz(n)) : 0;
      hs.resize((logN + 1) * n - (1 << logN) + 1);
      for (int e = 0; e < logN; ++e) {
        int *hes = hs.data() + e * n;
        for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
          hes[n + i] = min(hes[i], hes[i + (1 << e)]);
        }
      }
    }
  }
  // Returns longest common prefix of as[u, n) and as[v, n).
  //   0 <= u, v <= n
  //   Assumes rmq.
  inline int lcp(int u, int v) const {
    if (u == v) return n - u;
    int i = su[u], j = su[v];
    if (i > j) swap(i, j);
    const int e = 31 - __builtin_clz(j - i);
    return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
  }
};
////////////////////////////////////////////////////////////////////////////////

// before HLD:
//   0 <= u <= n: suffix [u, n)  (n: root, empty string)
//   n <  u <  m: LCA needed
// after HLD:
//   DFS-order
//   0: root, empty string
//   perm[u]: suffix[u, n)  (0 <= u <= n)
struct SuffixTree {
  int n, m;
  SuffixArray sa;
  struct Node {
    int par, len, occ;
    inline int l() const { return occ; }
    inline int r() const { return occ + len; }
  };
  vector<Node> nodes;
  vector<int> perm;
  SuffixTree() {}
  SuffixTree(const string &str, bool lastOcc) { build(str, lastOcc); }
  SuffixTree(const vector<int> &str, bool lastOcc) { build(str, lastOcc); }
  SuffixTree(const vector<long long> &str, bool lastOcc) { build(str, lastOcc); }
  template <class String> void build(const String &str, bool lastOcc) {
    n = str.size();
    m = n + 1;
    sa = SuffixArray(str, /*rmq=*/false);
    nodes.resize(2 * n + 1);
    nodes[n] = Node{/*par=*/-1, /*len=*/0, /*occ=*/lastOcc ? n : 0};
    int top = 0;
    vector<int> stack(n + 1);
    stack[0] = n;
    for (int i = 0; i < n; ++i) {
      const int u = sa.us[i];
      int v = -1;
      for (; nodes[stack[top]].len > sa.hs[i]; --top) {
        v = stack[top];
        nodes[nodes[v].par].occ = lastOcc ? max(nodes[nodes[v].par].occ, nodes[v].occ) : min(nodes[nodes[v].par].occ, nodes[v].occ);
      }
      if (nodes[stack[top]].len < sa.hs[i]) {
        const int w = m++;
        nodes[w] = Node{/*par=*/nodes[v].par, /*len=*/sa.hs[i], /*occ=*/nodes[v].occ};
        stack[++top] = nodes[v].par = w;
      }
      nodes[u] = Node{/*par=*/stack[top], /*len=*/n - u, /*occ=*/u};
      stack[++top] = u;
    }
    for (; top; --top) {
      const int v = stack[top];
      nodes[nodes[v].par].occ = lastOcc ? max(nodes[nodes[v].par].occ, nodes[v].occ) : min(nodes[nodes[v].par].occ, nodes[v].occ);
    }
    nodes.resize(m);
    runHld();
  }
  inline const Node &operator[](int u) const {
    return nodes[u];
  }
  inline int size(int u) const {
    return (~nodes[u].par) ? (nodes[u].len - nodes[nodes[u].par].len) : 1;
  }

  // Reindexes nodes by DFS-order.
  //   Ignores character order.
  //   Subtrees at the same "color" are isomorphic, should have the same HLD.
  //   old u -> new perm[u]
  vector<int> pt, g, head;
  void runHld() {
    pt.assign(m + 1, 0);
    for (int u = 0; u < m; ++u) if (u != n) ++pt[nodes[u].par];
    for (int u = 0; u < m; ++u) pt[u + 1] += pt[u];
    g.resize(pt[m]);
    for (int u = m; --u >= 0; ) if (u != n) g[--pt[nodes[u].par]] = u;
    vector<int> sz(m, 1);
    dfsSz(n, sz);
    int zeit = 0;
    perm.resize(m);
    head.resize(m);
    head[n] = 0;
    dfsHld(n, zeit, sz);
    assert(zeit == m);
    vector<Node> nodesReindexed(m);
    for (int u = 0; u < m; ++u) {
      Node &node = nodesReindexed[perm[u]] = nodes[u];
      if (~node.par) node.par = perm[node.par];
    }
    nodes.swap(nodesReindexed);
    for (int u = 0; u <= m; ++u) pt[u] = 0;
    for (int u = 1; u < m; ++u) ++pt[nodes[u].par];
    for (int u = 1; u < m; ++u) pt[u + 1] += pt[u];
    g.resize(pt[m]);
    for (int u = m; --u >= 1; ) g[--pt[nodes[u].par]] = u;
  }
  void dfsSz(int u, vector<int> &sz) {
    for (int i = pt[u]; i < pt[u + 1]; ++i) {
      dfsSz(g[i], sz);
      sz[u] += sz[g[i]];
    }
  }
  void dfsHld(int u, int &zeit, vector<int> &sz) {
    perm[u] = zeit++;
    if (pt[u] < pt[u + 1]) {
      int im = pt[u];
      for (int i = pt[u] + 1; i < pt[u + 1]; ++i) if (sz[g[im]] < sz[g[i]]) im = i;
      swap(g[pt[u]], g[im]);
      head[zeit] = head[zeit - 1];
      dfsHld(g[pt[u]], zeit, sz);
      for (int i = pt[u] + 1; i < pt[u + 1]; ++i) {
        head[zeit] = zeit;
        dfsHld(g[i], zeit, sz);
      }
    }
  }
  // Returns the shallowest node representing [l, r') for r <= r'.
  int locate(int l, int r) const {
    assert(0 <= l); assert(l <= r); assert(r <= n);
    for (int u = perm[l]; ; ) {
      const int h = head[u];
      const int p = nodes[h].par;
      if (!~p || nodes[p].len < r - l) {
        int lo = h - 1, hi = u;
        for (; lo + 1 < hi; ) {
          const int mid = (lo + hi) / 2;
          ((nodes[mid].len < r - l) ? lo : hi) = mid;
        }
        return hi;
      }
      u = p;
    }
  }
};

// block i contains [ls[i] + x, rs[i] - y) s.t.
//   0 <= x < sizeL(i),  0 <= y < sizeR(i, x)
//   0 <= y < sizeR(i),  0 <= x < sizeL(i, y)
struct Substring {
  // |str|
  int n;
  // stRev: occ is modified to represent the first occurrence in str
  SuffixTree st, stRev;
  // # of colors
  int size;
  // tree node -> block id
  vector<int> is, isRev;
  // [ls[i], rs[i]): representative of block i, i.e. [min l, max r)
  vector<int> ls, rs;
  // tree nodes for block i: us[js[i], js[i] + sizeL(i)), usRev[jsRev[i], jsRev[i] + sizeR(i))
  vector<int> js, jsRev, us, usRev;
  Substring() {}
  Substring(const string &str) { build(str); }
  Substring(const vector<int> &str) { build(str); }
  Substring(const vector<long long> &str) { build(str); }
  // O(n) time
  template <class String> void build(const String &str) {
    n = str.size();
    st = SuffixTree(str, /*lastOcc=*/false);
    String strRev = str;
    std::reverse(strRev.begin(), strRev.end());
    stRev = SuffixTree(strRev, /*lastOcc=*/true);
    for (int u = 0; u < stRev.m; ++u) stRev.nodes[u].occ = n - stRev.nodes[u].r();
    size = 0;
    is.assign(st.m, -1);
    isRev.assign(stRev.m, -1);
    js = jsRev = {1};
    us.assign(st.m, 0);
    usRev.assign(stRev.m, 0);
    {
      // radix sort: incr len, incr occ
      const int seqLen = (st.m - 1) + (stRev.m - 1);
      vector<int> ptLen(n + 1, 0), ptOcc(n + 1, 0);
      for (int u = 1; u < st.m; ++u) { ++ptLen[st[u].len]; ++ptOcc[st[u].occ]; }
      for (int u = 1; u < stRev.m; ++u) { ++ptLen[stRev[u].len]; ++ptOcc[stRev[u].occ]; }
      for (int len = 0; len < n; ++len) ptLen[len + 1] += ptLen[len];
      for (int occ = 0; occ < n; ++occ) ptOcc[occ + 1] += ptOcc[occ];
      vector<int> work(seqLen);
      for (int u = stRev.m; --u >= 1; ) work[--ptOcc[stRev[u].occ]] = ~u;
      for (int u = st.m; --u >= 1; ) work[--ptOcc[st[u].occ]] = u;
      vector<int> seq(seqLen);
      for (int k = seqLen; --k >= 0; ) seq[--ptLen[(work[k] >= 0) ? st[work[k]].len : stRev[~work[k]].len]] = work[k];
      for (int k = 0; k < seqLen - 1; ++k) if (seq[k] >= 0 && seq[k + 1] < 0 && st[seq[k]].len == stRev[~seq[k + 1]].len && st[seq[k]].occ == stRev[~seq[k + 1]].occ) {
        ls.push_back(st[seq[k]].l());
        rs.push_back(st[seq[k]].r());
        js.push_back(js.back() + stRev.size(~seq[k + 1]));
        jsRev.push_back(jsRev.back() + st.size(seq[k]));
        is[seq[k]] = isRev[~seq[k + 1]] = size++;
      }
    }
    {
      // radix sort: incr r, incr l
      const int seqLen = st.m - 1;
      vector<int> ptL(n + 1, 0), ptR(n + 1, 0);
      for (int u = 1; u < st.m; ++u) { ++ptL[st[u].l()]; ++ptR[st[u].r()]; }
      for (int l = 0; l < n; ++l) ptL[l + 1] += ptL[l];
      for (int r = 0; r < n; ++r) ptR[r + 1] += ptR[r];
      vector<int> work(seqLen);
      for (int u = st.m; --u >= 1; ) work[--ptL[st[u].l()]] = u;
      vector<int> seq(seqLen);
      for (int k = seqLen; --k >= 0; ) seq[--ptR[st[work[k]].r()]] = work[k];
      int i = -1, j = 0;
      for (int k = 0; k < seqLen; ++k) {
        if (~is[seq[k]]) j = js[i = is[seq[k]]];
        is[us[j++] = seq[k]] = i;
      }
    }
    {
      // radix sort: decr l, decr r
      const int seqLen = stRev.m - 1;
      vector<int> ptL(n + 1, 0), ptR(n + 1, 0);
      for (int u = 1; u < stRev.m; ++u) { ++ptL[stRev[u].l()]; ++ptR[stRev[u].r()]; }
      for (int l = n; l > 0; --l) ptL[l - 1] += ptL[l];
      for (int r = n; r > 0; --r) ptR[r - 1] += ptR[r];
      vector<int> work(seqLen);
      for (int u = stRev.m; --u >= 1; ) work[--ptR[stRev[u].r()]] = u;
      vector<int> seq(seqLen);
      for (int k = seqLen; --k >= 0; ) seq[--ptL[stRev[work[k]].l()]] = work[k];
      int i = -1, j = 0;
      for (int k = 0; k < seqLen; ++k) {
        if (~isRev[seq[k]]) j = jsRev[i = isRev[seq[k]]];
        isRev[usRev[j++] = seq[k]] = i;
      }
    }
  }
  // block id at representative position
  // st node id
  // stRev node id
  friend ostream &operator<<(ostream &os, const Substring &sub) {
    const int width = max(static_cast<int>(std::to_string(max(sub.st.m, sub.stRev.m) - 1).size()) + 1, 3);
    for (int phase = 0; phase < 3; ++phase) {
      for (int r = sub.n; r > 0; --r) {
        for (int l = 0; l < r; ++l) {
          const Location loc = sub.locate(l, r);
          string s;
          switch (phase) {
            case 0: {
              if (sub.ls[loc.i] == l && sub.rs[loc.i] == r) s = std::to_string(loc.i);
            } break;
            case 1: {
              if (sub.st[loc.u].len == r - l) s = std::to_string(loc.u);
            } break;
            case 2: {
              if (sub.stRev[loc.v].len == r - l) s = std::to_string(loc.v);
            } break;
          }
          os << "\x1b[" << (41 + loc.i % 6) << "m";
          os << string(width - static_cast<int>(s.size()), ' ') << s;
          os << "\x1b[m";
        }
        os << '\n';
      }
      os << '\n';
    }
    return os;
  }
  inline int id(int i, int x = 0) const {
    return us[js[i] + x];
  }
  inline int idRev(int i, int y = 0) const {
    return usRev[jsRev[i] + y];
  }
  inline int sizeR(int i, int x = 0) const {
    return st.size(id(i, x));
  }
  inline int sizeL(int i, int y = 0) const {
    return stRev.size(idRev(i, y));
  }
  // i: block id
  // x, y: coordinate within block i, [ls[i] + x, rs[i] - y)
  // u = st.locate(l, r)           : shallowest node of st    for [l, r') s.t. r <= r'
  // v = stRev.locate(n - r, n - l): shallowest node of stRev for [l', r) s.t. l' <= l
  // O(log(n)) time
  struct Location {
    int i, x, y, u, v;
  };
  Location locate(int l, int r) const {
    assert(0 <= l); assert(l <= r); assert(r <= n);
    if (l == r) return Location{-1, 0, 0, 0, 0};
    Location loc;
    loc.u = st.locate(l, r);
    loc.i = is[loc.u];
    loc.x = st[loc.u].l() - ls[loc.i];
    loc.y = ((l - loc.x) + (rs[loc.i] - ls[loc.i])) - r;
    loc.v = idRev(loc.i, loc.y);
    return loc;
  }

  // pattern ([l, r), t): (weight of str[l, r)) += t
  //   l < r
  //   T: commutative group
  // query [l, r): \sum[l<=l'<r'<=r] (weight of str[l', r'))
  // O(n + (|patterns| + |queries|) log(n)) time
  template <class T>
  vector<T> countOffline(const vector<pair<pair<int, int>, T>> &patterns,
                         const vector<pair<int, int>> &queries) const {
    const int patternsLen = patterns.size();
    const int queriesLen = queries.size();
    // x -> ((y, p or ~q))
    vector<vector<pair<int, int>>> eventss(st.m);
    // tree DP (path to root)
    vector<T> dp(st.m), dpRev(stRev.m);
    for (int p = 0; p < patternsLen; ++p) {
      const int l = patterns[p].first.first, r = patterns[p].first.second;
      assert(0 <= l); assert(l < r); assert(r <= n);
      const Location loc = locate(l, r);
      eventss[js[loc.i] + loc.x].emplace_back(loc.y, p);
      dp[loc.u] += patterns[p].second;
      dpRev[loc.v] += patterns[p].second;
    }
    for (int u = 1; u < st.m; ++u) dp[u] += dp[st[u].par];
    for (int u = 1; u < stRev.m; ++u) dpRev[u] += dpRev[stRev[u].par];
    // query [ls[i], rs[i])
    vector<T> corner(size);
    for (int i = 0; i < size; ++i) {
      for (int x = 0; x < sizeL(i); ++x) corner[i] += dp[id(i, x)];
      const int ii = isRev[stRev[idRev(i)].par];
      if (~ii) corner[i] += corner[ii];
    }
    // query [ls[i], rs[i] - y)
    vector<T> edge(stRev.m);
    for (int i = 0; i < size; ++i) {
      const int ii = is[st[id(i)].par];
      T sum = (~ii) ? corner[ii] : T();
      for (int y = sizeR(i); --y >= 0; ) edge[jsRev[i] + y] = sum += dpRev[idRev(i, y)];
    }
    // suffix sum of dp[st[id(i, x)].par]
    // can use segment tree if subtraction is unavailable
    vector<T> sumPar(st.m);
    for (int i = 0; i < size; ++i) {
      T sum = T();
      for (int x = sizeL(i); --x >= 0; ) sumPar[js[i] + x] = sum += dp[st[id(i, x)].par];
    }
    // query [l, r)
    vector<T> ans(queriesLen);
    vector<int> hasQuery(size, 0);
    for (int q = 0; q < queriesLen; ++q) {
      const int l = queries[q].first, r = queries[q].second;
      assert(0 <= l); assert(l <= r); assert(r <= n);
      if (l < r) {
        const Location loc = locate(queries[q].first, queries[q].second);
        if (loc.x == 0) {
          if (loc.y == 0) {
            ans[q] += corner[loc.i];
          } else {
            ans[q] += edge[jsRev[loc.i] + loc.y];
          }
        } else {
          hasQuery[loc.i] = 1;
          eventss[js[loc.i] + loc.x].emplace_back(loc.y, ~q);
          ans[q] += sumPar[js[loc.i] + loc.x];
          if (sizeL(loc.i, loc.y) < sizeL(loc.i)) ans[q] -= sumPar[js[loc.i] + sizeL(loc.i, loc.y)];
          const int vv = stRev[loc.v].par;
          const int ii = isRev[vv];
          if (~ii) ans[q] += edge[jsRev[ii] + (rs[ii] - stRev[vv].r())];
        }
      }
    }
    // offline 2D
    vector<T> bit(n + 1);
    for (int i = 0; i < size; ++i) if (hasQuery[i]) {
      for (int y = 1; y <= sizeR(i); ++y) bit[y] = T();
      for (int x = sizeL(i); --x >= 0; ) for (const auto &event : eventss[js[i] + x]) {
        if (event.second >= 0) {
          const T t = patterns[event.second].second;
          for (int y = sizeR(i) - event.first; y <= sizeR(i); y += y & -y) bit[y] += t;
        } else {
          T sum = T();
          for (int y = sizeR(i) - event.first; y > 0; y &= y - 1) sum += bit[y];
          ans[~event.second] += sum;
        }
      }
    }
    return ans;
  }
};

////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353U;
constexpr unsigned MO2 = 2U * MO;
constexpr int FFT_MAX = 23;
using Mint = ModInt<MO>;
constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U};
constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U};
constexpr Mint FFT_RATIOS[FFT_MAX] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U};
constexpr Mint INV_FFT_RATIOS[FFT_MAX] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U};

// as[rev(i)] <- \sum_j \zeta^(ij) as[j]
void fft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = n;
  if (m >>= 1) {
    for (int i = 0; i < m; ++i) {
      const unsigned x = as[i + m].x;  // < MO
      as[i + m].x = as[i].x + MO - x;  // < 2 MO
      as[i].x += x;  // < 2 MO
    }
  }
  if (m >>= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned x = (prod * as[i + m]).x;  // < MO
        as[i + m].x = as[i].x + MO - x;  // < 3 MO
        as[i].x += x;  // < 3 MO
      }
      prod *= FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  for (; m; ) {
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i + m].x = as[i].x + MO - x;  // < 4 MO
          as[i].x += x;  // < 4 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
    if (m >>= 1) {
      Mint prod = 1U;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
          as[i + m].x = as[i].x + MO - x;  // < 3 MO
          as[i].x += x;  // < 3 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
    as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x;  // < MO
  }
}

// as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)]
void invFft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = 1;
  if (m < n >> 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
    m <<= 1;
  }
  for (; m < n >> 1; m <<= 1) {
    Mint prod = 1U;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + (m >> 1); ++i) {
        const unsigned long long y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
        as[i].x += as[i + m].x;  // < 4 MO
        as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      for (int i = i0 + (m >> 1); i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  if (m < n) {
    for (int i = 0; i < m; ++i) {
      const unsigned y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
      as[i].x += as[i + m].x;  // < 4 MO
      as[i + m].x = y;  // < 4 MO
    }
  }
  const Mint invN = Mint(n).inv();
  for (int i = 0; i < n; ++i) {
    as[i] *= invN;
  }
}

void fft(vector<Mint> &as) {
  fft(as.data(), as.size());
}
void invFft(vector<Mint> &as) {
  invFft(as.data(), as.size());
}

vector<Mint> convolve(vector<Mint> as, vector<Mint> bs) {
  if (as.empty() || bs.empty()) return {};
  const int len = as.size() + bs.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  bs.resize(n); fft(bs);
  for (int i = 0; i < n; ++i) as[i] *= bs[i];
  invFft(as);
  as.resize(len);
  return as;
}
vector<Mint> square(vector<Mint> as) {
  if (as.empty()) return {};
  const int len = as.size() + as.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  for (int i = 0; i < n; ++i) as[i] *= as[i];
  invFft(as);
  as.resize(len);
  return as;
}
////////////////////////////////////////////////////////////////////////////////

/*
  [0, n] * [0, n - m + 1]
  
  [ a[0]                     ]
  [ ...    a[0]              ]
  [ a[m-1] ...               ]
  [        a[m-1]            ]
  [               ...        ]
  [                   a[0]   ]
  [                   ...    ]
  [                   a[m-1] ]
  
  [x^j] (rev(a) b)  m - 1 <= j <= n - 1
*/
vector<Mint> middle(vector<Mint> as, vector<Mint> bs) {
  const int m = as.size();
  const int n = bs.size();
  assert(m <= n);
  int nn = 1;
  for (; nn < n; nn <<= 1) {}
  reverse(as.begin(), as.end());
  as.resize(nn, 0);
  fft(as);
  bs.resize(nn, 0);
  fft(bs);
  for (int i = 0; i < nn; ++i) {
    bs[i] *= as[i];
  }
  invFft(bs);
  bs.resize(n);
  bs.erase(bs.begin(), bs.begin() + (m - 1));
  return bs;
}

constexpr int LIM_INV = 400'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM_INV; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM_INV; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (n < 0) {
    if (k >= 0) {
      return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
    } else if (n - k >= 0) {
      return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
    } else {
      return 0;
    }
  } else {
    if (0 <= k && k <= n) {
      assert(n < LIM_INV);
      return fac[n] * invFac[k] * invFac[n - k];
    } else {
      return 0;
    }
  }
}


char S[200'010];
Substring sub;

// # occ
vector<int> wts;

// go out of block to up/left
vector<Mint> up, lf;

// gs[j] += \sum[i] path(j-i,n-1) wt^((j-i)+(n-1)) fs[i]
void opposite(Mint wt, int m, int n, vector<Mint> fs, vector<Mint> &gs) {
// cerr<<"  [opposite] "<<wt<<" "<<m<<" "<<n<<" "<<fs<<" "<<gs<<endl;
  if (m == 0) return;
  assert((int)fs.size() == m);
  reverse(fs.begin(), fs.end());
  vector<Mint> coef(m);
  {
    Mint w = wt.pow(n);
    for (int i = 0; i < m; ++i) {
      coef[i] = binom(i + (n-1), n-1) * w;
      w *= wt;
    }
  }
  const auto prod = convolve(fs, coef);
  for (int i = 0; i < m; ++i) gs[m - 1 - i] += prod[i];
// cerr<<"  [opposite] "<<wt<<" "<<m<<" "<<n<<" "<<fs<<" "<<gs<<endl;
}

// gs[j] += \sum[i] path(i,n-1-j) wt^(i+(n-1-j)+1) fs[i]
void corner(Mint wt, int m, int n, vector<Mint> fs, vector<Mint> &gs) {
  if (m == 0 || n == 0) return;
  assert((int)fs.size() == m);
  vector<Mint> coef(m + n - 1);
  {
    Mint w = wt;
    for (int i = 0; i < m + n - 1; ++i) {
      coef[i] = fac[i] * w;
      w *= wt;
    }
  }
  for (int i = 0; i < m; ++i) fs[i] *= invFac[i];
  const auto prod = middle(fs, coef);
  assert((int)prod.size() == n);
  for (int i = 0; i < n; ++i) gs[n - 1 - i] += invFac[i] * prod[i];
}

pair<vector<Mint>, vector<Mint>> rec(int i, int x0, int x1) {
  const Mint wt = wts[sub.id(i)];
  const int y0 = sub.sizeR(i, x0);
  const int y1 = (x1 < sub.sizeL(i)) ? sub.sizeR(i, x1) : 0;
// cerr<<"[rec] "<<i<<" "<<wt<<"; "<<x0<<" "<<x1<<"; "<<y0<<" "<<y1<<endl;
  pair<vector<Mint>, vector<Mint>> ret;
  ret.first.assign(x1 - x0, 0);
  ret.second.assign(y0 - y1, 0);
  if (x0 + 1 == x1) {
    Mint sum = up[sub.st[sub.id(i, x0)].par];
    for (int y = y0; --y >= y1; ) {
      sum += lf[sub.stRev[sub.idRev(i, y)].par];
      sum *= wt;
      ret.second[y - y1] = sum;
    }
    ret.first[0] = sum;
  } else {
    const int xMid = (x0 + x1) / 2;
    const int yMid = sub.sizeR(i, xMid);
    const auto res0 = rec(i, x0, xMid);
    const auto res1 = rec(i, xMid, x1);
    for (int y = y0; --y >= yMid; ) ret.second[y - y1] = res0.second[y - yMid];
    for (int x = xMid; x < x1; ++x) ret.first[x - x0] = res1.first[x - xMid];
    // rectangle
    const int dx = xMid - x0, dy = yMid - y1;
    opposite(wt, dx, dy, res0.first, ret.first);
    opposite(wt, dy, dx, res1.second, ret.second);
    corner(wt, dx, dy, res0.first, ret.second);
    corner(wt, dy, dx, res1.second, ret.first);
  }
// cerr<<"[rec] "<<i<<" "<<wt<<"; "<<x0<<" "<<x1<<"; "<<y0<<" "<<y1<<" = "<<ret<<endl;
  return ret;
}

int main() {
  prepare();
  
  for (; ~scanf("%s", S); ) {
    sub = Substring(S);
// cerr<<sub<<flush;
    wts.assign(sub.st.m, 0);
    for (int l = 0; l < sub.n; ++l) ++wts[sub.st.perm[l]];
    for (int u = sub.st.m; --u >= 1; ) wts[sub.st[u].par] += wts[u];
    up.assign(sub.st.m, 0);
    lf.assign(sub.stRev.m, 0);
    up[0] = 1;
    for (int i = 0; i < sub.size; ++i) {
      const auto res = rec(i, 0, sub.sizeL(i));
      for (int x = 0; x < sub.sizeL(i); ++x) up[sub.id(i, x)] = res.first[x];
      for (int y = 0; y < sub.sizeR(i); ++y) lf[sub.idRev(i, y)] = res.second[y];
    }
    const Mint ans = up[sub.id(sub.size - 1)];
    printf("%u\n", ans.x);
#ifdef LOCAL
const string s=S;
const int n=s.size();
if(n<=100){
 auto weight=[&](int l,int r)->int{
  int cnt=0;
  for(int i=0;i+(r-l)<=n;++i)if(s.substr(i,r-l)==s.substr(l,r-l))++cnt;
  return cnt;
 };
 vector<vector<Mint>>brt(n+1,vector<Mint>(n+1,0));
 for(int l=0;l<n;++l)brt[l][l+1]=weight(l,l+1);
 for(int l=n;--l>=0;)for(int r=l+2;r<=n;++r)brt[l][r]=weight(l,r)*(brt[l+1][r]+brt[l][r-1]);
 if(brt[0][n]!=ans){
  cerr<<sub<<flush;
  cerr<<"FAIL "<<s<<": "<<brt[0][n]<<" "<<ans<<endl;
  for(int r=n;r>0;--r){
   for(int l=0;l<r;++l)cerr<<brt[l][r]<<" ";
   cerr<<endl;
  }
 }
 assert(brt[0][n]==ans);
}
#endif
  }
  return 0;
}

详细

Test #1:

score: 5
Accepted
time: 13ms
memory: 9740kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

842068617

result:

ok 1 number(s): "842068617"

Test #2:

score: 5
Accepted
time: 17ms
memory: 9484kb

input:

zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszz...

output:

670446466

result:

ok 1 number(s): "670446466"

Test #3:

score: 5
Accepted
time: 17ms
memory: 9488kb

input:

dedgdedfdedhdedfdedgdedfdedjdedfdedgdedfdedhdedfdedgdedfdedidedfdedgdedfdedhdedfdedgdedfdedkdedfdedgdedfdedhdedfdedgdedfdedidedfdedgdedfdedhdedfdedgdedfdedjdedfdedgdedfdedhdedfdedgdedfdedidedfdedgdedfdedhdedfdedgdedfdedldedfdedgdedfdedhdedfdedgdedfdedidedfdedgdedfdedhdedfdedgdedfdedjdedfdedgdedfdedh...

output:

736895071

result:

ok 1 number(s): "736895071"

Test #4:

score: 5
Accepted
time: 17ms
memory: 9776kb

input:

zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszz...

output:

670446466

result:

ok 1 number(s): "670446466"

Test #5:

score: 5
Accepted
time: 20ms
memory: 9880kb

input:

kkhjihjjhhjhhikjjkijjjkkikjkjkjjkhjhjhhjijkhjkkkjijikhjhkkhkjhhijiijjjhkiiiikkjjhhkkjjijijkihkijihhikjkjkikikkkkkijjihjkkhiihkjkkkhkihkhhhjhjkihkhjjkikjkjhkhhjhhjiihiiiijiihjkhhihjhjjhjkiikiikhjihhkkjikijkjijjhkjijhihhihkhkjjkjjkkkhjhikkikikikkhijiiijjikiijihhkkjjjhikkjjkkkihjikihhikjijhkijkkiikhhik...

output:

500378111

result:

ok 1 number(s): "500378111"

Test #6:

score: 5
Accepted
time: 374ms
memory: 32604kb

input:

babbbbaabbabbaabaaaababaaaaababbbbbabbaaabbabbaaaaabbababbbaabaaaababbaaaaaababbbaaabbabbaaaabaaabaaababaababaabbbbababbaaaabaabbaabbaaaaabbbbababaabbabbaaaaababbabaabaabaaabaabbaaababbbabbbbbbaabbbabbababbbabaabbaaabaabababbabbbbbaaabbbaabbaaabaaababababaaababbbbbabbbbbaaabaaaaaaabaabbbabababbaabaa...

output:

276651135

result:

ok 1 number(s): "276651135"

Test #7:

score: 5
Accepted
time: 373ms
memory: 32936kb

input:

abababbbbbbababaaabbbabbabababaabaabbbaabbbabbbbbababaaaaabaabbbabbbabbaabbaabbbbbbabbaabbabbabaabbbbabbbbaabaabbbbaababbbaaabbbaabaabbbaabaaabbaaaaaabbbbbbaaaabbaaaabaabbabbbbbbaaabaababbabaabaabaabbbbbaaabbbaababbaababbbaabaabbbbbaabbbaaaabbbabaaaababbaabbababaaababbbbbbbbbbbbaabbabbabaaaaaaaaaaaa...

output:

400282964

result:

ok 1 number(s): "400282964"

Test #8:

score: 5
Accepted
time: 368ms
memory: 32716kb

input:

aaaabbbabbaabbbbbabbabbbbabbbabbaabbbaaabbaabababbababbaabaabbbbbaabbababbbbbababbaabbaaaabbbbbaababbbbbaaaaabbbbaababbbabaaaabbbbaabaaaaaaaabaabaabaabbaaaababaaaaabbbaabaaababaaaababaabbbbabaabaaaaaababbaaaabbaaabbbbaaababababbaababbaabbabbbabbbabbaabbbbabbabaababbbbbaaaaababbabaaaaaaabbbabaabaaaaa...

output:

729339954

result:

ok 1 number(s): "729339954"

Test #9:

score: 5
Accepted
time: 222ms
memory: 17660kb

input:

zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszz...

output:

787319145

result:

ok 1 number(s): "787319145"

Test #10:

score: 5
Accepted
time: 248ms
memory: 20880kb

input:

jhkhhjhkkhjikkkjiiikjikjijijjiihkkkjiiihijiiikhhkiikiijikkjkhjhhkkihkkhjikhiikkhkkkhjhiijhkihkhjkjhkjkhhkjjjjhiiikkkihhikiijikhjjjijijjhkjjihhjikkkkkijijihhjjkihikjikijjijjikihjjhiiikhhihjjjhjhhijiiijikkihkjjkjihkjjkhkijjjiihhikkhjhjkikhikhjjkkkjhhiihiiihhkhjiikjjkjjkkijkijjkikhhkjhjhhhkjikkkhjiikhh...

output:

878766990

result:

ok 1 number(s): "878766990"

Test #11:

score: 5
Accepted
time: 254ms
memory: 19540kb

input:

xyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyxxyyxxyxyxyyxxyyxxyxyyxxyxyxyyx...

output:

800644110

result:

ok 1 number(s): "800644110"

Test #12:

score: 5
Accepted
time: 353ms
memory: 19768kb

input:

xyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyyxxyxyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxyyxxyxyyxxyxyyxxyxyyxxyyxxyxyyxxy...

output:

955468834

result:

ok 1 number(s): "955468834"

Test #13:

score: 5
Accepted
time: 302ms
memory: 20688kb

input:

xgltlxgflxglnaflbelxgbgfqltsxgltlxgflxglnaflxglqfjxglflxgflxglfgrglflulnaflbelxgbgfqltsxgltlxgflxgldglnaflxglqfjxglflxgflxglfgryglflulnaflbelxgbgfqltsxgltlxgfligfqltsxgltlxgflxglnaflxglqfjxglflxaxgltlxgflxglnaflbelxgbgfqltsxgltlxgflxglnaflxglqfjxglflxgflxglfgrglflulnaflbelxgbgfqltsxgltlxgflxgldglnaf...

output:

947232801

result:

ok 1 number(s): "947232801"

Test #14:

score: 5
Accepted
time: 474ms
memory: 30508kb

input:

zszsszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszz...

output:

560729572

result:

ok 1 number(s): "560729572"

Test #15:

score: 5
Accepted
time: 551ms
memory: 30660kb

input:

ijjijiijjiijijjijiiiijjiijjijiijjiijijjiijjijiijijjijiijjiijijjijiijijjiijjijiijijjijiijjiijijjiijjijiijjiijijjijiijijjiijjijiijjiijijjiijjijiijijjijiijjiijijjiijjijiijjiijijjijiijijjiijjijiijijjijiijjiijijjijiijijjiijjijiijjiijijjiijjijiijijjijiijjiijijjijiijijjiijjijiijijjijiijjiijijjiijjijiijjiij...

output:

881489605

result:

ok 1 number(s): "881489605"

Test #16:

score: 5
Accepted
time: 448ms
memory: 29392kb

input:

khkjkjkjjkkihikjijiikjkhhkjhikkjihkijihkikiihjikhhiikhhjjkjhhijjhihhkhjkkhijjiikjjikkijkkijhhhijijiijkjijhjihhhikikhjihhjjikkijkhkjjjihjjikjihkhhikijjihjhkjhjjjhjhhhiiijikhjkjikkijhiijhkkikhjhihhikhjhjkihhkhhihhjjhhiiihjiijhjkhhkjkjikjkikikjjjhiikijijiijkijjihjjhhikiiihkihhjkjjhjjikhjkjjkhjkijhkhjjk...

output:

121066948

result:

ok 1 number(s): "121066948"

Test #17:

score: 5
Accepted
time: 353ms
memory: 33276kb

input:

ghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghghgh...

output:

394568528

result:

ok 1 number(s): "394568528"

Test #18:

score: 5
Accepted
time: 490ms
memory: 26596kb

input:

xyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxyxxyxxyxxyxyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxyxxyxxyxxyxyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxxyxyxxyxyxxyxxyxyxxyxxyxxyxyxxyxyxxyxx...

output:

380248771

result:

ok 1 number(s): "380248771"

Test #19:

score: 5
Accepted
time: 559ms
memory: 29736kb

input:

tgndlgndjrgxtgndjtgndlgxtgngunmxjgxtkdjtgndlgndjtgndlgxtgjgndlgxjgxtgndlgxtgngunmxjgxtgndjtgndlgxtgngunmxjgxtkdjtgndlgndjtgndlgxtgngundndjtgndlgxtgngunmxjgxtkdjtgndljtkdjtgndlgndjtgndlgxtgjgndxjtgndlgxtgngunyndjtgndlgxtgngunmxjgxtkdjtgndlgndjtgndlgxtgjgndlgxdlgxjgxtgndlgxtgngunmxjgxtgndjtgndlgxtgngu...

output:

379249648

result:

ok 1 number(s): "379249648"

Test #20:

score: 5
Accepted
time: 546ms
memory: 30604kb

input:

jihihiikhkkkjikjhijkijkjiiihhjhikjhkijiiihjjhjjjikkkikihjkhhkjjhijkiikkkjiijikjkkhiihikijjkhikiikkjjhkikjjjjhkhjikihiihkijkhjhkkjjhjkjkkkhjjkhkihihhhkkkjhkkjhijjhkhijhkijjjikjjkjkhikjjhkkjihkihijhjhkkkhkjjijhjjkhjiihhkkjhhjhjikikijjjkkhiijhjjihkkihikikijhkhikiijkkiiihhjkhihhkkhihiijhkhiiikiijihkijhh...

output:

113827949

result:

ok 1 number(s): "113827949"