結果

問題 No.2234 palindromer
ユーザー CoCo_Japan_panCoCo_Japan_pan
提出日時 2023-03-03 22:41:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,958 bytes
コンパイル時間 3,643 ms
コンパイル使用メモリ 205,908 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 02:52:09
合計ジャッジ時間 3,430 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// 提出時にassertはオフ
#ifndef DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif
#endif

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define ALL(x) (x).begin(), (x).end()
template <class T> using vec = vector<T>;

bool isPalindrome(const string &S) {
    int size = S.size();
    for(int i = 0; i < size / 2; i++){
        if(S[i] != S[size - 1 - i]) return false;
    }
    return true;
}

vec<int> Manacher(const string &S){
    int size = S.size();
    int i = 0, j = 0;
    vec<int> R(size);
    while(i < size) {
        while(i - j >= 0 && i + j < 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;
    }
    return R;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string S;
    cin >> S;
    if(isPalindrome(S)){
        cout << S << "\n";
        return 0;
    }
    // $を挟む
    string S_dummy;
    S_dummy += S[0];
    for(int i = 1; i < (int)S.size(); i++){
        S_dummy += '$';
        S_dummy += S[i];
    }
    // cout << S_dummy << "\n";
    vec<int> manac = Manacher(S_dummy);
    /*
    for(int num : manac){
        cout << num << " ";
    }
    cout << "\n";
    */
    int size_dummy = S_dummy.size();
    int centerIndex = size_dummy - 1;
    for(int i = size_dummy / 2 + 1; i < size_dummy - 1; i++){
        if(manac[i] == size_dummy - i){
            centerIndex = i;
            break;
        }
    }
    if(centerIndex % 2 == 0) {
        centerIndex /= 2;
        string ans = S.substr(0, centerIndex);
        string ans_rev = ans;
        reverse_copy(ALL(ans), ans_rev.begin());
        cout << ans + S[centerIndex] + ans_rev << "\n";
        return 0;
    }
    centerIndex /= 2;
    string ans = S.substr(0, centerIndex + 1);
    string ans_rev = ans;
    reverse_copy(ALL(ans), ans_rev.begin());
    cout << ans + ans_rev << "\n";
}
0