結果
問題 | No.3000 Optimal Run Length Encoding |
ユーザー | 👑 hos.lyric |
提出日時 | 2024-12-25 00:52:30 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 14,947 bytes |
コンパイル時間 | 3,931 ms |
コンパイル使用メモリ | 179,532 KB |
実行使用メモリ | 818,036 KB |
最終ジャッジ日時 | 2024-12-25 00:57:09 |
合計ジャッジ時間 | 200,121 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1,821 ms
5,248 KB |
testcase_02 | AC | 1,879 ms
5,248 KB |
testcase_03 | AC | 1,635 ms
5,248 KB |
testcase_04 | AC | 1,661 ms
5,248 KB |
testcase_05 | AC | 1,212 ms
5,248 KB |
testcase_06 | AC | 1,327 ms
5,248 KB |
testcase_07 | AC | 1,665 ms
5,248 KB |
testcase_08 | AC | 1,727 ms
5,248 KB |
testcase_09 | AC | 1,207 ms
5,248 KB |
testcase_10 | AC | 1,028 ms
5,248 KB |
testcase_11 | AC | 1,289 ms
5,248 KB |
testcase_12 | AC | 1,425 ms
5,248 KB |
testcase_13 | AC | 1,514 ms
5,248 KB |
testcase_14 | AC | 956 ms
5,248 KB |
testcase_15 | AC | 961 ms
5,248 KB |
testcase_16 | AC | 1,293 ms
5,248 KB |
testcase_17 | AC | 877 ms
5,248 KB |
testcase_18 | AC | 617 ms
5,248 KB |
testcase_19 | AC | 2,598 ms
5,248 KB |
testcase_20 | AC | 2,611 ms
5,248 KB |
testcase_21 | AC | 2,727 ms
5,248 KB |
testcase_22 | AC | 2,682 ms
5,248 KB |
testcase_23 | AC | 2,662 ms
5,248 KB |
testcase_24 | AC | 2,650 ms
5,248 KB |
testcase_25 | AC | 2,668 ms
5,248 KB |
testcase_26 | AC | 2,598 ms
5,248 KB |
testcase_27 | AC | 2,292 ms
5,248 KB |
testcase_28 | AC | 2,274 ms
5,248 KB |
testcase_29 | AC | 2,199 ms
5,248 KB |
testcase_30 | AC | 2,255 ms
5,248 KB |
testcase_31 | AC | 2,222 ms
5,248 KB |
testcase_32 | AC | 1,739 ms
5,248 KB |
testcase_33 | AC | 475 ms
7,672 KB |
testcase_34 | AC | 450 ms
41,124 KB |
testcase_35 | AC | 311 ms
92,908 KB |
testcase_36 | AC | 281 ms
93,024 KB |
testcase_37 | AC | 281 ms
92,900 KB |
testcase_38 | AC | 301 ms
93,028 KB |
testcase_39 | AC | 307 ms
93,024 KB |
testcase_40 | AC | 276 ms
92,896 KB |
testcase_41 | AC | 301 ms
92,916 KB |
testcase_42 | AC | 288 ms
92,896 KB |
testcase_43 | AC | 295 ms
92,788 KB |
testcase_44 | AC | 272 ms
92,896 KB |
testcase_45 | MLE | - |
testcase_46 | MLE | - |
testcase_47 | MLE | - |
testcase_48 | MLE | - |
testcase_49 | MLE | - |
testcase_50 | MLE | - |
testcase_51 | MLE | - |
testcase_52 | MLE | - |
testcase_53 | MLE | - |
testcase_54 | MLE | - |
testcase_55 | MLE | - |
testcase_56 | MLE | - |
testcase_57 | MLE | - |
testcase_58 | MLE | - |
testcase_59 | MLE | - |
testcase_60 | MLE | - |
testcase_61 | MLE | - |
testcase_62 | MLE | - |
testcase_63 | MLE | - |
testcase_64 | MLE | - |
testcase_65 | MLE | - |
testcase_66 | MLE | - |
testcase_67 | MLE | - |
testcase_68 | MLE | - |
testcase_69 | MLE | - |
testcase_70 | AC | 193 ms
103,968 KB |
testcase_71 | AC | 323 ms
96,716 KB |
testcase_72 | AC | 645 ms
171,896 KB |
testcase_73 | AC | 264 ms
93,740 KB |
testcase_74 | AC | 736 ms
168,728 KB |
testcase_75 | AC | 290 ms
93,644 KB |
testcase_76 | AC | 697 ms
172,376 KB |
testcase_77 | AC | 255 ms
93,260 KB |
testcase_78 | AC | 656 ms
182,188 KB |
testcase_79 | AC | 242 ms
93,928 KB |
testcase_80 | AC | 455 ms
134,284 KB |
testcase_81 | AC | 140 ms
5,248 KB |
testcase_82 | AC | 283 ms
104,032 KB |
testcase_83 | AC | 302 ms
103,976 KB |
testcase_84 | AC | 297 ms
104,100 KB |
testcase_85 | AC | 290 ms
103,932 KB |
testcase_86 | AC | 276 ms
103,976 KB |
testcase_87 | AC | 309 ms
103,972 KB |
testcase_88 | AC | 293 ms
104,016 KB |
testcase_89 | AC | 303 ms
103,972 KB |
testcase_90 | AC | 267 ms
103,980 KB |
testcase_91 | AC | 303 ms
103,976 KB |
testcase_92 | AC | 304 ms
103,716 KB |
testcase_93 | AC | 297 ms
103,672 KB |
testcase_94 | AC | 283 ms
103,752 KB |
testcase_95 | AC | 324 ms
103,768 KB |
testcase_96 | AC | 293 ms
104,212 KB |
testcase_97 | AC | 103 ms
43,004 KB |
testcase_98 | AC | 140 ms
56,484 KB |
testcase_99 | AC | 91 ms
40,020 KB |
testcase_100 | AC | 93 ms
39,332 KB |
testcase_101 | AC | 142 ms
47,268 KB |
testcase_102 | MLE | - |
testcase_103 | MLE | - |
testcase_104 | MLE | - |
testcase_105 | MLE | - |
testcase_106 | MLE | - |
testcase_107 | AC | 1,378 ms
351,064 KB |
testcase_108 | MLE | - |
testcase_109 | MLE | - |
testcase_110 | MLE | - |
testcase_111 | MLE | - |
testcase_112 | AC | 672 ms
183,016 KB |
testcase_113 | AC | 642 ms
182,916 KB |
testcase_114 | AC | 634 ms
183,012 KB |
testcase_115 | AC | 676 ms
182,904 KB |
testcase_116 | AC | 672 ms
186,800 KB |
testcase_117 | AC | 664 ms
184,828 KB |
testcase_118 | AC | 775 ms
182,452 KB |
testcase_119 | AC | 638 ms
186,920 KB |
testcase_120 | AC | 653 ms
184,940 KB |
testcase_121 | AC | 652 ms
185,604 KB |
testcase_122 | AC | 653 ms
184,536 KB |
testcase_123 | AC | 647 ms
185,716 KB |
testcase_124 | AC | 701 ms
185,204 KB |
testcase_125 | AC | 619 ms
174,624 KB |
testcase_126 | AC | 586 ms
170,612 KB |
testcase_127 | AC | 670 ms
184,604 KB |
testcase_128 | AC | 631 ms
175,808 KB |
testcase_129 | AC | 602 ms
173,448 KB |
testcase_130 | AC | 608 ms
182,148 KB |
testcase_131 | AC | 636 ms
173,816 KB |
testcase_132 | AC | 239 ms
103,844 KB |
testcase_133 | AC | 242 ms
103,976 KB |
testcase_134 | AC | 242 ms
104,088 KB |
testcase_135 | AC | 297 ms
103,976 KB |
testcase_136 | AC | 259 ms
103,972 KB |
testcase_137 | AC | 262 ms
104,088 KB |
testcase_138 | AC | 246 ms
103,876 KB |
testcase_139 | AC | 282 ms
104,104 KB |
testcase_140 | AC | 265 ms
103,972 KB |
testcase_141 | AC | 262 ms
104,084 KB |
ソースコード
#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<vector<int>>> headss(len); vector<vector<vector<vector<pair<int, int>>>>> quess(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; headss[i].assign(p, {}); quess[i].assign(p, {}); for (int r = 0; r < p; ++r) { for (int ten = 1; ten <= lim; ten *= 10) { headss[i][r].emplace_back(); quess[i][r].emplace_back(); } } } 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; for (int e = 0; e < (int)quess[i][x % p].size(); ++e) { int &head = headss[i][x % p][e]; auto &que = quess[i][x % p][e]; 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); 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; for (int e = 0; e < (int)quess[i][x % p].size(); ++e) { auto &head = headss[i][x % p][e]; auto &que = quess[i][x % p][e]; 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; }