結果
問題 | No.1018 suffixsuffixsuffix |
ユーザー | りあん |
提出日時 | 2020-04-03 22:19:56 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 171 ms / 2,000 ms |
コード長 | 4,082 bytes |
コンパイル時間 | 2,596 ms |
コンパイル使用メモリ | 211,192 KB |
実行使用メモリ | 7,532 KB |
最終ジャッジ日時 | 2024-07-03 03:53:11 |
合計ジャッジ時間 | 7,607 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 70 ms
6,024 KB |
testcase_10 | AC | 69 ms
5,920 KB |
testcase_11 | AC | 73 ms
6,216 KB |
testcase_12 | AC | 68 ms
5,988 KB |
testcase_13 | AC | 67 ms
5,908 KB |
testcase_14 | AC | 155 ms
7,212 KB |
testcase_15 | AC | 151 ms
7,184 KB |
testcase_16 | AC | 169 ms
7,408 KB |
testcase_17 | AC | 148 ms
7,344 KB |
testcase_18 | AC | 150 ms
7,304 KB |
testcase_19 | AC | 16 ms
5,376 KB |
testcase_20 | AC | 17 ms
5,376 KB |
testcase_21 | AC | 17 ms
5,376 KB |
testcase_22 | AC | 17 ms
5,376 KB |
testcase_23 | AC | 17 ms
5,376 KB |
testcase_24 | AC | 156 ms
7,072 KB |
testcase_25 | AC | 171 ms
7,432 KB |
testcase_26 | AC | 156 ms
7,040 KB |
testcase_27 | AC | 154 ms
7,316 KB |
testcase_28 | AC | 153 ms
7,040 KB |
testcase_29 | AC | 22 ms
5,376 KB |
testcase_30 | AC | 23 ms
5,376 KB |
testcase_31 | AC | 21 ms
5,376 KB |
testcase_32 | AC | 24 ms
5,376 KB |
testcase_33 | AC | 22 ms
5,376 KB |
testcase_34 | AC | 2 ms
5,376 KB |
testcase_35 | AC | 19 ms
5,376 KB |
testcase_36 | AC | 22 ms
5,376 KB |
testcase_37 | AC | 165 ms
7,532 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; using P = pair<int, int>; const int M = 1000000007; class suffix_array { // compare (rank[i], rank[i + k]) and (rank[j], rank[j + k]) static bool compare_sa(int n, const vector<int>& rank, int i, int j, int k) { if (rank[i] != rank[j]) return rank[i] < rank[j]; int ri = i + k <= n ? rank[i + k] : -1; int rj = j + k <= n ? rank[j + k] : -1; return ri < rj; } public: static vector<int> construct_sa(const string& s) { int n = s.size(); vector<int> sa(n + 1), rank(n + 1); for (int i = 0; i <= n; ++i) { sa[i] = i; rank[i] = i < n ? s[i] : -1; } for (int k = 1; k <= n; k <<= 1) { sort(sa.begin(), sa.end(), [&n, &k, &rank](const int& a, const int& b){ return compare_sa(n, rank, a, b, k); }); vector<int> tmp(n + 1); for (int i = 1; i <= n; ++i) tmp[sa[i]] = tmp[sa[i - 1]] + compare_sa(n, rank, sa[i - 1], sa[i], k); for (int i = 0; i <= n; ++i) rank[i] = tmp[i]; } return sa; } static vector<int> construct_lcp(const string& s, const vector<int>& sa) { int n = s.length(); vector<int> rank(n + 1), lcp(n + 1); for (int i = 0; i <= n; ++i) rank[sa[i]] = i; int h = 0; for (int i = 0; i < n; ++i) { if (h > 0) --h; for (int j = sa[rank[i] - 1]; j + h < n && i + h < n; ++h) if (s[j + h] != s[i + h]) break; lcp[rank[i] - 1] = h; } return lcp; } }; class segtree { private: int n, s, t; vector<int> tr; const int ex = M; int q(int k, int l, int r) { return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k] : min(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r)); } public: segtree(int m) { n = 1; while (n < m) n <<= 1; tr.clear(); tr.resize((n << 1) - 1, ex); } void update(int j, const int& x) { int i = j + n - 1; tr[i] = x; while (i > 0) { i = (i - 1) >> 1; tr[i] = min(tr[i << 1 | 1], tr[(i + 1) << 1]); } } // [s, t) int run(int _s, int _t) { s = _s; t = _t; return q(0, 0, n); } }; vector<int> kmp(const string& s) { int n = s.length(); vector<int> res(n + 1); res[0] = -1; int j = -1; for (int i = 0; i < n; ++i) { while (j >= 0 && s[i] != s[j]) j = res[j]; j++; res[i + 1] = j; } return res; } vector<long long> solve(const string& s, long long m, const vector<long long>& qs) { int n = s.length(); int q = qs.size(); int l = n - kmp(s)[n]; if (l > 0 && l < n && n % l == 0) { return solve(s.substr(0, l), m * n / l, qs); } vector<long long> res(q); if (m == 1) { vector<int> sa = suffix_array::construct_sa(s); for (int i = 0; i < q; ++i) { res[i] = sa[qs[i]] + 1; } } else { string t = s + s; vector<int> sa = suffix_array::construct_sa(t); long long cnt = 0; int qi = 0; for (int i = 1; i <= n * 2; ++i) { if (sa[i] >= n) { if (qi < q && cnt + 1 == qs[qi]) { res[qi] = (m - 2) * n + sa[i] + 1; ++qi; } ++cnt; } else { while (qi < q && cnt + m - 1 >= qs[qi]) { res[qi] = (m - (qs[qi] - cnt + 1)) * n + sa[i] + 1; ++qi; } cnt += m - 1; } } } return res; } int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; long long m; string s; cin >> n >> m >> q >> s; vector<long long> qs(q); for (int i = 0; i < q; ++i) { cin >> qs[i]; } vector<long long> res = solve(s, m, qs); for (int i = 0; i < q; ++i) { cout << res[i] << (i < q - 1 ? ' ' : '\n'); } return 0; }