結果

問題 No.3000 Optimal Run Length Encoding
ユーザー 👑 hos.lyrichos.lyric
提出日時 2024-12-25 01:07:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 15,537 bytes
コンパイル時間 4,074 ms
コンパイル使用メモリ 177,656 KB
実行使用メモリ 816,404 KB
最終ジャッジ日時 2024-12-25 01:10:51
合計ジャッジ時間 168,725 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 316 ms
5,248 KB
testcase_02 AC 322 ms
5,248 KB
testcase_03 AC 301 ms
5,248 KB
testcase_04 AC 309 ms
5,248 KB
testcase_05 AC 286 ms
5,248 KB
testcase_06 AC 284 ms
5,248 KB
testcase_07 AC 317 ms
5,248 KB
testcase_08 AC 305 ms
5,248 KB
testcase_09 AC 280 ms
5,248 KB
testcase_10 AC 267 ms
5,248 KB
testcase_11 AC 292 ms
5,248 KB
testcase_12 AC 289 ms
5,248 KB
testcase_13 AC 293 ms
5,248 KB
testcase_14 AC 266 ms
5,248 KB
testcase_15 AC 264 ms
5,248 KB
testcase_16 AC 275 ms
5,248 KB
testcase_17 AC 250 ms
5,248 KB
testcase_18 AC 178 ms
5,248 KB
testcase_19 AC 434 ms
5,248 KB
testcase_20 AC 420 ms
5,248 KB
testcase_21 AC 423 ms
5,248 KB
testcase_22 AC 439 ms
5,248 KB
testcase_23 AC 451 ms
5,248 KB
testcase_24 AC 423 ms
5,248 KB
testcase_25 AC 420 ms
5,248 KB
testcase_26 AC 429 ms
5,248 KB
testcase_27 AC 374 ms
5,248 KB
testcase_28 AC 436 ms
5,248 KB
testcase_29 AC 420 ms
5,248 KB
testcase_30 AC 445 ms
5,248 KB
testcase_31 AC 426 ms
5,248 KB
testcase_32 AC 365 ms
5,248 KB
testcase_33 AC 367 ms
7,032 KB
testcase_34 AC 497 ms
37,648 KB
testcase_35 AC 433 ms
92,944 KB
testcase_36 AC 431 ms
93,044 KB
testcase_37 AC 428 ms
92,908 KB
testcase_38 AC 426 ms
92,912 KB
testcase_39 AC 433 ms
92,908 KB
testcase_40 AC 418 ms
92,900 KB
testcase_41 AC 406 ms
92,784 KB
testcase_42 AC 450 ms
93,040 KB
testcase_43 AC 419 ms
92,920 KB
testcase_44 AC 414 ms
92,912 KB
testcase_45 AC 3,802 ms
370,424 KB
testcase_46 AC 2,996 ms
232,308 KB
testcase_47 AC 3,549 ms
337,900 KB
testcase_48 AC 3,126 ms
272,300 KB
testcase_49 AC 3,311 ms
292,048 KB
testcase_50 AC 3,221 ms
263,760 KB
testcase_51 AC 3,596 ms
318,044 KB
testcase_52 AC 3,355 ms
282,472 KB
testcase_53 AC 3,700 ms
352,164 KB
testcase_54 AC 3,086 ms
250,528 KB
testcase_55 AC 3,787 ms
370,416 KB
testcase_56 AC 3,423 ms
293,016 KB
testcase_57 AC 3,204 ms
286,560 KB
testcase_58 AC 2,909 ms
233,852 KB
testcase_59 AC 3,541 ms
334,620 KB
testcase_60 AC 3,844 ms
370,424 KB
testcase_61 AC 3,520 ms
318,048 KB
testcase_62 AC 3,112 ms
246,804 KB
testcase_63 AC 3,201 ms
289,084 KB
testcase_64 AC 2,955 ms
232,192 KB
testcase_65 AC 2,280 ms
365,000 KB
testcase_66 AC 1,498 ms
220,732 KB
testcase_67 AC 1,847 ms
281,256 KB
testcase_68 AC 1,502 ms
213,500 KB
testcase_69 AC 2,183 ms
323,740 KB
testcase_70 AC 291 ms
103,968 KB
testcase_71 AC 455 ms
94,148 KB
testcase_72 AC 715 ms
174,380 KB
testcase_73 AC 434 ms
98,684 KB
testcase_74 AC 847 ms
173,468 KB
testcase_75 AC 459 ms
93,644 KB
testcase_76 AC 882 ms
177,180 KB
testcase_77 AC 363 ms
93,260 KB
testcase_78 AC 746 ms
183,880 KB
testcase_79 AC 380 ms
93,652 KB
testcase_80 AC 570 ms
144,268 KB
testcase_81 AC 34 ms
5,248 KB
testcase_82 AC 420 ms
103,972 KB
testcase_83 AC 395 ms
104,100 KB
testcase_84 AC 403 ms
103,972 KB
testcase_85 AC 404 ms
103,976 KB
testcase_86 AC 382 ms
103,936 KB
testcase_87 AC 433 ms
103,976 KB
testcase_88 AC 407 ms
103,844 KB
testcase_89 AC 430 ms
103,972 KB
testcase_90 AC 389 ms
103,976 KB
testcase_91 AC 383 ms
103,848 KB
testcase_92 AC 403 ms
103,840 KB
testcase_93 AC 420 ms
103,796 KB
testcase_94 AC 427 ms
103,748 KB
testcase_95 AC 404 ms
103,768 KB
testcase_96 AC 410 ms
104,076 KB
testcase_97 AC 136 ms
42,876 KB
testcase_98 AC 198 ms
64,416 KB
testcase_99 AC 127 ms
35,976 KB
testcase_100 AC 124 ms
39,256 KB
testcase_101 AC 161 ms
44,972 KB
testcase_102 MLE -
testcase_103 MLE -
testcase_104 MLE -
testcase_105 AC 1,385 ms
262,192 KB
testcase_106 MLE -
testcase_107 AC 729 ms
161,372 KB
testcase_108 MLE -
testcase_109 AC 1,386 ms
257,420 KB
testcase_110 AC 1,173 ms
236,096 KB
testcase_111 MLE -
testcase_112 AC 745 ms
162,056 KB
testcase_113 AC 773 ms
161,920 KB
testcase_114 AC 779 ms
161,800 KB
testcase_115 AC 740 ms
161,656 KB
testcase_116 AC 835 ms
165,164 KB
testcase_117 AC 767 ms
163,912 KB
testcase_118 AC 729 ms
161,388 KB
testcase_119 AC 790 ms
165,036 KB
testcase_120 AC 768 ms
163,048 KB
testcase_121 AC 733 ms
164,108 KB
testcase_122 AC 777 ms
162,516 KB
testcase_123 AC 752 ms
163,960 KB
testcase_124 AC 776 ms
163,568 KB
testcase_125 AC 709 ms
154,904 KB
testcase_126 AC 655 ms
150,384 KB
testcase_127 AC 780 ms
163,612 KB
testcase_128 AC 721 ms
156,092 KB
testcase_129 AC 696 ms
153,704 KB
testcase_130 AC 745 ms
160,992 KB
testcase_131 AC 694 ms
153,980 KB
testcase_132 AC 312 ms
103,976 KB
testcase_133 AC 345 ms
103,972 KB
testcase_134 AC 327 ms
103,976 KB
testcase_135 AC 345 ms
104,096 KB
testcase_136 AC 387 ms
103,844 KB
testcase_137 AC 356 ms
104,104 KB
testcase_138 AC 349 ms
103,840 KB
testcase_139 AC 397 ms
103,968 KB
testcase_140 AC 348 ms
103,844 KB
testcase_141 AC 354 ms
104,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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)]);
  }
};
////////////////////////////////////////////////////////////////////////////////

// https://codeforces.com/blog/entry/106725
// Lyndon
//   * smaller than non-trivial suffix
//   * smaller than other cyclic shift
// Lyndon factorization (Lyndon factors in non-incr. order):
//   * longest Lyndon prefix greedily
//   * min suffix greedily (removing min suffix does not change the suffix order)

// pqs[v] = (p, q): Lyndon factorization of as[0, v) ends with a[v - p, v)^q
//   String: string, vector<int>, vector<long long>
//   O(n) time
//
// min_u as[u, v) bs:
//   for (int u = v; ; ) {
//     // candidate u
//     if (v == 0) break;
//     const int p = pqs[u].first, q = pqs[u].second;
//     const int uu = u - q * p;
//     if (!(p >= v - u && sa.lcp(uu, u) >= v - u)) break;
//     // (v - u) is at least doubled
//     u = uu;
//   }
// }
template <class String>
vector<pair<int, int>> lyndonSuffix(const String &as) {
  const int n = as.size();
  vector<pair<int, int>> pqs(n + 1);
  pqs[0] = make_pair(0, 0);
  for (int u = 0; u < n; ) {
    for (int p = 1, q = 1, r = 0, v = u + 1; ; ++v) {
      // as[u, v) = as[u, u + p)^q as[u, u + r)
      // as[u, u + p): Lyndon
      pqs[v] = (r != 0) ? pqs[u + r] : make_pair(p, q);
      if (v == n || as[v - p] > as[v]) {
        u = v - r;
        break;
      } else if (as[v - p] < as[v]) {
        p = v + 1 - u; q = 1; r = 0;
      } else {
        if (++r == p) { ++q; r = 0; }
      }
    }
  }
  return pqs;
}

// as[u, vs[u]): longest Lyndon prefix of as[u, n)
//   String: string, vector<int>, vector<long long>
//   O(n) time
template <class String>
vector<int> lyndonPrefix(const String &as, const SuffixArray &sa) {
  const int n = as.size();
  // top: larger suffix
  int stackLen = 0;
  vector<int> stack(n + 1);
  vector<int> vs(n);
  for (int u = 0; u <= n; ++u) {
    for (; stackLen > 0 && sa.su[stack[stackLen - 1]] > sa.su[u]; --stackLen) {
      vs[stack[stackLen - 1]] = u;
    }
    stack[stackLen++] = u;
  }
  return vs;
}
template <class String>
vector<int> lyndonPrefix(const String &as) {
  return lyndonPrefix(as, SuffixArray(as, /*rmq=*/false));
}

// lyndonPrefix for invert(as), using suffix array of as
template <class String>
vector<int> lyndonPrefixInverted(const String &as, const SuffixArray &sa) {
  assert(sa.rmq);
  const int n = as.size();
  // top: larger suffix
  int stackLen = 0;
  vector<int> stack(n + 1);
  vector<int> vs(n);
  for (int u = 0; u <= n; ++u) {
    for (; stackLen > 0; --stackLen) {
      const int uu = stack[stackLen - 1];
      const int l = sa.lcp(uu, u);
      if (u + l < n && as[uu + l] > as[u + l]) break;
      vs[uu] = u;
    }
    stack[stackLen++] = u;
  }
  return vs;
}

// (p, [u, v)): run <=>
//   * p: min period of as[u, v)
//   * v - u >= 2 p
//   * [u, v): maximal
// \sum 1 <= n
// \sum (v - u) / p <= 3 n
// \sum (v - u - 2 p + 1) \in O(n log n)  (TODO: proof)

// Returns runs (p, [u, v)) in lex. order.
//   String: string, vector<int>, vector<long long>
//   O(n log n) time, (<= 6 n + 2) SuffixArray::lcp calls
template <class String>
vector<pair<int, pair<int, int>>> repetitions(const String &as, const SuffixArray &sa) {
  assert(sa.rmq);
  const int n = as.size();
  if (n == 0) return {};
  String asRev = as;
  std::reverse(asRev.begin(), asRev.end());
  const SuffixArray saRev(asRev, /*rmq=*/true);
  const vector<int> vs = lyndonPrefix(as, sa);
  const vector<int> vsInverted = lyndonPrefixInverted(as, sa);
  vector<pair<int, pair<int, int>>> runs;
  for (int u = 0; u < n; ++u) {
    // from longest lyndon prefix of as[u, n) or invert(as)[u, n)
    {
      const int v = vs[u];
      const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v);
      if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv));
    }
    if (vs[u] != vsInverted[u]) {
      const int v = vsInverted[u];
      const int p = v - u, uu = u - saRev.lcp(n - u, n - v), vv = v + sa.lcp(u, v);
      if (vv - uu >= 2 * p) runs.emplace_back(p, make_pair(uu, vv));
    }
  }
  // radix sort
  const int runsLen = runs.size();
  auto runsWork = runs;
  vector<int> pt(n + 1, 0);
  for (int i = 0; i < runsLen; ++i) ++pt[runs[i].second.first];
  for (int u = 0; u < n - 1; ++u) pt[u + 1] += pt[u];
  for (int i = runsLen; --i >= 0; ) runsWork[--pt[runs[i].second.first]] = runs[i];
  memset(pt.data() + 1, 0, n * sizeof(int));
  for (int i = 0; i < runsLen; ++i) ++pt[runsWork[i].first];
  for (int p = 1; p < n; ++p) pt[p + 1] += pt[p];
  for (int i = runsLen; --i >= 0; ) runs[--pt[runsWork[i].first]] = runsWork[i];
  runs.erase(std::unique(runs.begin(), runs.end()), runs.end());
  return runs;
}
template <class String>
vector<pair<int, pair<int, int>>> repetitions(const String &as) {
  return repetitions(as, SuffixArray(as, /*rmq=*/true));
}

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


constexpr int INF = 1001001001;
constexpr int TEN[7] = {
  1,
  10,
  100,
  1000,
  10000,
  100000,
  1000000,
};

int N;
char S[500'010];

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%s", S);
    N = strlen(S);
    
    const auto runs = repetitions(string(S));
// cerr<<"runs = "<<runs<<endl;
    const int len = runs.size();
    vector<vector<int>> headss[6];
    vector<vector<vector<pair<int, int>>>> quess[6];
    for (int e = 0; e < 6; ++e) {
      headss[e].assign(len, {});
      quess[e].assign(len, {});
    }
    for (int i = 0; i < len; ++i) {
      const int p = runs[i].first;
      const int u = runs[i].second.first;
      const int v = runs[i].second.second;
      const int lim = (v - u) / p;
      for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) {
        headss[e][i].assign(p, 0);
        quess[e][i].assign(p, {});
      }
    }
    
    vector<vector<int>> addssL(N + 1), remssL(N + 1);
    vector<vector<int>> addssR(N + 1), remssR(N + 1);
    for (int i = 0; i < len; ++i) {
      const int p = runs[i].first;
      const int u = runs[i].second.first;
      const int v = runs[i].second.second;
      addssL[u].push_back(i);
      remssL[v - p].push_back(i);
      addssR[u + p].push_back(i);
      remssR[v].push_back(i);
    }
    
    pair<int, int> norun(INF, -1);
    vector<int> dp(N + 1, INF);
    vector<pair<int, int>> prv(N + 1, make_pair(-1, -1));
    set<int> onL, onR;
    dp[0] = 0;
    for (int x = 0; x <= N; ++x) {
      if (x) {
        // get
        for (const int i : addssR[x]) onR.insert(i);
        for (const int i : onR) {
          const int p = runs[i].first;
          const int u = runs[i].second.first;
          const int v = runs[i].second.second;
          const int lim = (v - u) / p;
          for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) {
            int &head = headss[e][i][x % p];
            auto &que = quess[e][i][x % p];
            for (; head < (int)que.size() && que[head].second + (TEN[e + 1] - 1) * p < x; ++head) {}
            if (head < (int)que.size()) {
// cerr<<COLOR("94")<<"get "<<x<<" "<<i<<" "<<que[head]<<COLOR()<<endl;
              if (chmin(dp[x], que[head].first)) {
                prv[x] = make_pair(i, que[head].second);
              }
            }
          }
        }
        for (const int i : remssR[x]) {
          onR.erase(i);
          const int p = runs[i].first;
          const int u = runs[i].second.first;
          const int v = runs[i].second.second;
          const int lim = (v - u) / p;
          for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) {
            quess[e][i].clear();
            quess[e][i].shrink_to_fit();
          }
        }
        if (chmin(dp[x], norun.first + x)) {
          prv[x] = make_pair(-1, norun.second);
        }
      }
      // push
      {
        for (const int i : addssL[x]) onL.insert(i);
        for (const int i : onL) {
          const int p = runs[i].first;
          const int u = runs[i].second.first;
          const int v = runs[i].second.second;
          const int lim = (v - u) / p;
          for (int e = 0, ten = 1; ten <= lim; ++e, ten *= 10) {
            auto &head = headss[e][i][x % p];
            auto &que = quess[e][i][x % p];
            const pair<int, int> val(dp[x] + p + (e + 1), x);
            for (; head < (int)que.size() && que.back() >= val; que.pop_back()) {}
// cerr<<COLOR("91")<<"push "<<x<<" "<<i<<" "<<val<<COLOR()<<endl;
            que.push_back(val);
          }
        }
        for (const int i : remssL[x]) onL.erase(i);
        chmin(norun, make_pair(dp[x] - x + 1, x));
      }
    }
// cerr<<"dp = "<<dp<<endl;
// cerr<<"prv = "<<prv<<endl;
    
    vector<string> ops;
    for (int x = N; x > 0; ) {
      const int i = prv[x].first;
      const int y = prv[x].second;
      const int p = (~i) ? runs[i].first : (x - y);
      ops.push_back(string(S + y, S + (y + p)) + to_string((x - y) / p));
      x = y;
    }
    reverse(ops.begin(), ops.end());
    string ans;
    for (const auto &op : ops) ans += op;
    puts(ans.c_str());
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}
0