結果

問題 No.958 たぷりすたべる (回文)
ユーザー finefine
提出日時 2019-12-23 00:44:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 1,721 ms
コンパイル使用メモリ 167,388 KB
実行使用メモリ 6,404 KB
最終ジャッジ日時 2023-10-12 23:24:43
合計ジャッジ時間 8,469 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,352 KB
testcase_27 AC 18 ms
4,352 KB
testcase_28 AC 17 ms
4,480 KB
testcase_29 AC 17 ms
4,348 KB
testcase_30 AC 17 ms
4,352 KB
testcase_31 AC 18 ms
4,348 KB
testcase_32 AC 17 ms
4,348 KB
testcase_33 AC 18 ms
4,352 KB
testcase_34 AC 17 ms
4,348 KB
testcase_35 AC 18 ms
4,352 KB
testcase_36 AC 17 ms
4,352 KB
testcase_37 AC 19 ms
4,352 KB
testcase_38 AC 20 ms
4,348 KB
testcase_39 AC 19 ms
4,352 KB
testcase_40 AC 18 ms
4,348 KB
testcase_41 AC 19 ms
4,348 KB
testcase_42 AC 18 ms
4,348 KB
testcase_43 AC 18 ms
4,348 KB
testcase_44 AC 18 ms
4,348 KB
testcase_45 AC 19 ms
4,352 KB
testcase_46 AC 19 ms
4,352 KB
testcase_47 AC 50 ms
5,168 KB
testcase_48 AC 54 ms
6,172 KB
testcase_49 AC 55 ms
6,180 KB
testcase_50 AC 54 ms
6,152 KB
testcase_51 AC 56 ms
6,404 KB
testcase_52 AC 61 ms
6,216 KB
testcase_53 AC 57 ms
6,240 KB
testcase_54 AC 58 ms
6,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

// 各 i について「文字 i を中心とする最長の回文の半径」を記録した配列を O(|S|) で構築
// 半径: (全長+1)/2
// ex. abaaababa
//     121412321
// ※普通にやると奇数長の回文しか検出できないが、
//   "abaab"のようにダミー文字を挟むと偶数長の回文も検出可能
vector<int> manacher(const string& S) {
    vector<int> res(S.size());
    int i = 0, j = 0;
    while (i < S.size()) {
        while (i-j >= 0 && i+j < S.size() && S[i-j] == S[i+j]) ++j;
        res[i] = j;
        int k = 1;
        while (i-k >= 0 && i+k < S.size() && k+res[i-k] < j) res[i+k] = res[i-k], ++k;
        i += k; j -= k;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, K, q;
    cin >> n >> K >> q;
    string s;
    cin >> s;
    string s3 = "";
    s3 += s;
    s3 += s;
    s3 += s;

    vector<int> v = manacher(s3);

    ll len = n;
    len *= K;
    for (int i = 0; i < q; ++i) {
        ll a;
        cin >> a;
        a--;

        ll r = min(len - a, a + 1);
        int idx = a % n;
        ll r2 = min(n * 2 - idx, n + idx + 1);
        if (v[n + idx] < r2) {
            r = min(r, (ll)v[n + idx]);
        }
        cout << 2 * r - 1 << "\n";
    }
    return 0;
}
0