結果

問題 No.2234 palindromer
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-08-27 01:38:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,986 bytes
コンパイル時間 1,934 ms
コンパイル使用メモリ 201,928 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-27 01:38:42
合計ジャッジ時間 2,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename T>
vector<int> z_algorithm(T &S){
    vector<int> Z(S.size());
    Z[0] = S.size();
    int i=1, j=0;
    while(i < S.size()){
        while(i+j < S.size() && S[j] == S[i+j]) j++;
        Z[i] = j;

        if (j == 0){
            i++;
            continue;
        }
        int k=1;
        while(k < j && k + Z[k] < j){
            Z[i+k] = Z[k];
            k++;
        }
        i += k;
        j -= k;
    }

    return Z;
}

//M[2n]...n文字目を中心とする最長回文
//M[2n+1]...n文字目とn+1文字目を中心とする最長回文
template <typename T>
vector<int> manacher(T &s){
    string S="";
    for (auto x : s){
        S += x;
        S += '$';
    }
    S.pop_back();
    vector<int> R(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++;
        R[i] = j;
        int k=1;
        while(i-k >= 0 && k + R[i-k] < j){
            R[i+k] = R[i-k];
            k++;
        }
        i += k;
        j -= k;
    }

    for (int i=0; i<S.size(); i++){
        if (i % 2 == R[i] % 2) R[i]--;
    }

    return R;
}

int main(){

    string S, T;
    cin >> S;
    int N=S.size(), mx=0, r;
    vector<int> R = manacher(S);
    for (int i=2*N-2; i>=N-1; i--) mx = max(mx, R[i]);
    for (int i=2*N-2; i>=N-1; i--){
        if (mx == R[i]){
            //奇数長
            if (i % 2 == 0){
                r = (R[i]-1)/2;
                i /= 2;
                T = S.substr(0, i-r);
                reverse(T.begin(), T.end());
                cout << S << T << endl;
                return 0;
            }
            //偶数長
            else{
                r = R[i]/2;
                i /= 2;
                T = S.substr(0, i-r+1);
                reverse(T.begin(), T.end());
                cout << S << T << endl;
                return 0;
            }
        }
    }

    return 0;
}
0