結果

問題 No.2234 palindromer
ユーザー TsReaperTsReaper
提出日時 2023-03-03 22:04:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,184 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 167,328 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 02:09:00
合計ジャッジ時間 2,369 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 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 #

#include <bits/stdc++.h>
#define MAXN 100
#define P 131
#define MOD ((int) 1e9 + 7)
using namespace std;

int n;
char s[MAXN + 10];

long long p[MAXN + 10], f[MAXN + 10], g[MAXN + 10];

long long gao(int L, int R) {
    if (L <= R) {
        int len = R - L + 1;
        return (f[R] - f[L - 1] * p[len] % MOD + MOD) % MOD;
    } else {
        int len = L - R + 1;
        return (g[R] - g[L + 1] * p[len] % MOD + MOD) % MOD;
    }
}

int main() {
    scanf("%s", s + 1); n = strlen(s + 1);
    p[0] = 1;
    for (int i = 1; i <= n; i++) p[i] = p[i - 1] * P % MOD;
    for (int i = 1; i <= n; i++) f[i] = (f[i - 1] * P + s[i]) % MOD;
    for (int i = n; i > 0; i--) g[i] = (g[i + 1] * P + s[i]) % MOD;

    int ans1 = n + 1;
    for (int i = (n + 1) / 2 + 1; i <= n; i++) if (gao(n, i) == gao(i - (n - i + 1), i - 1)) { ans1 = i; break; }
    int len1 = n - 2 * (n + 1 - ans1);

    int ans2 = n;
    for (int i = n / 2 + 1; i < n; i++) if (gao(n, i + 1) == gao(i - (n - i), i - 1)) { ans2 = i; break; }
    int len2 = n - 2 * (n - ans2) - 1;
    
    for (int i = 1; i <= n; i++) printf("%c", s[i]);
    for (int i = min(len1, len2); i > 0; i--) printf("%c", s[i]);
    return 0;
}
0