結果

問題 No.464 PPAP
ユーザー pekempeypekempey
提出日時 2016-12-15 23:44:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 1,306 bytes
コンパイル時間 1,492 ms
コンパイル使用メモリ 167,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 08:03:49
合計ジャッジ時間 3,434 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 97 ms
4,380 KB
testcase_11 AC 62 ms
4,376 KB
testcase_12 AC 97 ms
4,380 KB
testcase_13 AC 97 ms
4,380 KB
testcase_14 AC 98 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 102 ms
4,380 KB
testcase_24 AC 142 ms
4,380 KB
testcase_25 AC 159 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    string s;
    cin >> s;
    int n = s.size();

    const int mod = 1e9 + 7;
    srand(time(NULL));
    int64_t base = rand() + 2;

    vector<int64_t> pw(5050);
    pw[0] = 1;
    for (int i = 1; i < 5050; i++) {
        pw[i] = base * pw[i - 1] % mod;
    }

    vector<int64_t> P(n + 2);
    for (int i = 0; i < n; i++) {
        P[i + 1] = 1;
        for (int j = 0; j <= i; j++) {
            if (s[j] != s[i - j]) {
                P[i + 1] = 0;
            }
        }
    }

    vector<int64_t> PP(n + 2);
    for (int i = n - 1; i >= 0; i--) {
        int64_t a = 0;
        int64_t b = 0;
        for (int j = 0; i + j < n; j++) {
            a = a * base + s[i + j];
            b = b + s[i + j] * pw[j];
            a %= mod;
            b %= mod;
            if (a == b) {
                PP[i + j + 2] += P[i];
            }
        }
    }

    for (int i = 0; i < n; i++) {
        PP[i + 1] += PP[i];
    }

    int64_t ans = 0;
    for (int i = n - 1; i >= 0; i--) {
        bool pal = true;
        for (int j = 0; j <= n - 1 - i; j++) {
            if (s[i + j] != s[n - 1 - j]) {
                pal = false;
            }
        }
        if (pal) {
            ans += PP[i];
        }
    }

    cout << ans << endl;
}
0