結果
問題 | No.515 典型LCP |
ユーザー | pekempey |
提出日時 | 2017-05-06 02:16:47 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,327 bytes |
コンパイル時間 | 1,267 ms |
コンパイル使用メモリ | 87,032 KB |
実行使用メモリ | 98,408 KB |
最終ジャッジ日時 | 2024-09-14 10:17:55 |
合計ジャッジ時間 | 7,204 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 709 ms
95,724 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 305 ms
98,356 KB |
testcase_06 | AC | 320 ms
92,204 KB |
testcase_07 | AC | 337 ms
95,436 KB |
testcase_08 | AC | 335 ms
98,408 KB |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 298 ms
95,576 KB |
testcase_16 | AC | 303 ms
94,372 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <string> class SuffixDataStructure { public: // a[i]>=0 SuffixDataStructure(const std::vector<int> &a) : a(a) { sa = sufarray(a); buildLcp(); buildRmq(); } int getLcp(int i, int j) { if (i == j) { return a.size() - std::max(i, j); } i = isa[i]; j = isa[j]; if (i > j) { std::swap(i, j); } int k = lg[j - i]; return std::min(rmq[k][i], rmq[k][j - (1 << k)]); } private: const std::vector<int> &a; std::vector<int> sa; std::vector<int> isa; std::vector<int> lcp; std::vector<int> lg; std::vector<std::vector<int>> rmq; std::vector<int> sufarray(std::vector<int> a) { const int TYPE_S = -1; const int TYPE_L = -2; const int n = a.size(); if (n == 1) { return std::vector<int>(1); } std::vector<int> type(n); type.back() = TYPE_S; for (int i = n - 2; i >= 0; i--) { if (a[i] < a[i + 1]) { type[i] = TYPE_S; } else if (a[i] > a[i + 1]) { type[i] = TYPE_L; } else { type[i] = type[i + 1]; } } std::vector<int> lmsID; std::vector<std::vector<int>> lms; int prev = n; for (int i = n - 1; i >= 1; i--) { if (type[i - 1] == TYPE_L && type[i] == TYPE_S) { type[i] = lmsID.size(); lmsID.push_back(i); lms.emplace_back(std::vector<int>(a.begin() + i, a.begin() + prev)); prev = i; } } auto inducedSort = [&](const std::vector<int> &a, const std::vector<int> &ordSS, const std::vector<int> &type) { const int n = a.size(); const int maxi = *std::max_element(a.begin(), a.end()); std::vector<int> sa(n, -1), L(maxi + 2), S(maxi + 2); for (int i = 0; i < n; i++) { L[a[i] + 1]++; } for (int i = 0; i < L.size() - 1; i++) { L[i + 1] += L[i]; S[i] = L[i + 1]; } for (int i = ordSS.size() - 1; i >= 0; i--) { int j = ordSS[i]; sa[--S[a[j]]] = j; } for (int i = 0; i < L.size() - 1; i++) { S[i] = L[i + 1]; } for (int i = 0; i < n; i++) { const int j = sa[i] - 1; if (j >= 0 && type[j] == TYPE_L) { sa[L[a[j]]++] = j; } } for (int i = n - 1; i >= 0; i--) { const int j = sa[i] - 1; if (j >= 0 && (type[j] == TYPE_S || type[j] >= 0)) { sa[--S[a[j]]] = j; } } return sa; }; const int SS = lmsID.size(); std::vector<int> ordLms; for (int i : inducedSort(a, lmsID, type)) { if (type[i] >= 0) { ordLms.push_back(i); } } std::reverse(lmsID.begin(), lmsID.end()); std::vector<int> rankLms(SS); for (int i = 1; i < SS; i++) { const int x = type[ordLms[i - 1]]; const int y = type[ordLms[i]]; rankLms[i] = rankLms[i - 1] + (lms[x] < lms[y]); } for (int i = 0; i < SS; i++) { type[ordLms[i]] = rankLms[i]; } std::vector<int> lmsA; for (int i : lmsID) { lmsA.push_back(type[i]); } std::vector<int> ordSS; for (int i : sufarray(lmsA)) { ordSS.push_back(lmsID[i]); } return inducedSort(a, ordSS, type); } void buildLcp() { const int n = a.size() - 1; isa.resize(n + 1); lcp.assign(n + 1, 0); for (int i = 0; i <= n; i++) { isa[sa[i]] = i; } int k = 0; for (int i = 0; i < n; i++) { int j = sa[isa[i] - 1]; k = std::max(0, k - 1); while (i + k < n && j + k < n && a[i + k] == a[j + k]) { k++; } lcp[isa[i] - 1] = k; } } void buildRmq() { const int n = lcp.size(); lg.resize(n + 1); for (int i = 2; i <= n; i++) { lg[i] = lg[i / 2] + 1; } const int m = lg[n]; rmq.assign(m + 1, std::vector<int>(n)); for (int i = 0; i < n; i++) { rmq[0][i] = lcp[i]; } for (int i = 0; i < m; i++) { for (int j = 0; j + (1 << i) < n; j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } } }; int main() { int n; std::cin >> n; std::vector<int> start(n); std::vector<int> t; for (int i = 0; i < n; i++) { start[i] = t.size(); static char buf[800001]; scanf("%s", buf); for (int j = 0; buf[j] != 0; j++) { t.push_back(buf[j]); } } t.push_back(0); SuffixDataStructure suf(t); int m; long long x, d; std::cin >> m >> x >> d; long long ans = 0; while (m--) { int i = x / (n - 1); int j = x % (n - 1); if (i > j) { std::swap(i, j); } else { j = j + 1; } x = (x + d) % (1LL * n * (n - 1)); ans += suf.getLcp(start[i], start[j]); } std::cout << ans << std::endl; }