結果

問題 No.958 たぷりすたべる (回文)
ユーザー 👑 hitonanodehitonanode
提出日時 2019-12-21 00:11:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,893 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 169,196 KB
実行使用メモリ 6,320 KB
最終ジャッジ日時 2023-09-25 05:45:56
合計ジャッジ時間 5,881 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T> bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }


// Manacher's Algorithm: radius of palindromes
// Input: std::string of length N
// Output: std::vector<int> of size N
// Complexity: O(N)
// Sample:
// - `sakanakanandaka` -> [1, 1, 2, 1, 4, 1, 4, 1, 2, 2, 1, 1, 1, 2, 1]
// Reference: <https://snuke.hatenablog.com/entry/2014/12/02/235837>
std::vector<int> manacher(std::string S)
{
    std::vector<int> res(S.length());
    int i = 0, j = 0;
    while (i < (int)S.size()) {
        while (i - j >= 0 and i + j < (int)S.size() and S[i - j] == S[i + j]) j++;
        res[i] = j;
        int k = 1;
        while (i - k >= 0 and i + k < (int)S.size() and k + res[i - k] < j) res[i + k] = res[i - k], k++;
        i += k, j -= k;
    }
    return res;
}


int main()
{
    lint N, K, Q;
    cin >> N >> K >> Q;
    string S;
    cin >> S;
    if (K <= 5)
    {
        string T;
        while (K--) T += S;
        vector<int> ret = manacher(T);
        while (Q--)
        {
            lint a;
            cin >> a;
            printf("%d\n", ret[a - 1] * 2 - 1);
        }
        return 0;
    }
    vector<int> man3 = manacher(S + S + S);
    lint LEN = K * N;
    while (Q--)
    {
        lint a;
        cin >> a;
        a--;
        lint ret;
        if (a < N)
        {
            ret = man3[a];
        }
        else if (a >= LEN - N)
        {
            ret = man3[a % N + N * 2];
        }
        else
        {
            ret = man3[a % N + N];
            if (ret >= N)
            {
                ret = a + 1;
                mmin(ret, LEN - a);
            }
        }
        printf("%lld\n", ret * 2 - 1);
    }
}
0