結果

問題 No.464 PPAP
ユーザー tubo28tubo28
提出日時 2017-02-11 11:08:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 168,604 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-28 12:48:52
合計ジャッジ時間 2,868 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 47 ms
4,380 KB
testcase_11 AC 22 ms
4,380 KB
testcase_12 AC 34 ms
4,380 KB
testcase_13 AC 29 ms
4,376 KB
testcase_14 AC 28 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 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,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 28 ms
4,376 KB
testcase_24 AC 28 ms
4,380 KB
testcase_25 AC 29 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

std::vector<int> manachar(const std::string &s) {
    int n = s.size();
    std::vector<int> rad(n);
    int c = 0;
    for (int r = 0; r < n; ++r) {
        int l = c - (r - c);
        if (r + rad[l] < c + rad[c]) {
            rad[r] = rad[l];
        } else {
            int rr = c + rad[c];
            int rl = r - (rr - r);
            while (rl >= 0 && rr < n && s[rl] == s[rr])
                ++rr, --rl;
            rad[r] = rr - r;
            c = r;
        }
    }
    return rad;
}

std::string insert_dollar(const std::string &s){
    int n = s.size();
    std::string res(n*2 + 1, '$');
    for(int i = 0; i < n; ++i){
        res[i*2 + 1] = s[i];
    }
    return res;
}

// [l, r)
bool is_palindrome(int l, int r, const std::vector<int> &rad){
    l = l*2 + 1;
    r = (r-1)*2 + 1;
    int c = (l + r) / 2;
    return r < c + rad[c];
}

using namespace std;

int main(){
    string s;
    while(cin >> s){
        int n = s.size();
        auto rad = manachar(insert_dollar(s));
        int pp[5010] = {};
        for(int i = 1; i < n; ++i){
            for(int j = i + 1; j <= n; ++j){
                if(is_palindrome(0, i, rad) && is_palindrome(i, j, rad)){
                    ++pp[j];
                }
            }
        }
        long long ans = 0;
        for(int i = 2; i < n; ++i){
            for(int j = i + 1; j < n; ++j){
                ans += (long long)pp[i] * is_palindrome(j, n, rad);
            }
        }
        cout << ans << endl;
    }
}
0