結果

問題 No.464 PPAP
ユーザー pekempey
提出日時 2016-12-16 15:36:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 1,496 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 170,640 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-30 10:24:06
合計ジャッジ時間 2,595 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Manacher {
private:
    const string &s;
    vector<int> rad;

public:
    Manacher(const string &s) : s(s), rad(s.size() * 2 - 1) {
        const int n = rad.size();
        int i = 0;
        int j = 0;
        while (i < n) {
            while (i - j >= 0 && i + j < n && get(i - j) == get(i + j)) {
                j++;
            }
            rad[i] = j;
            int k = 1;
            while (i - k >= 0 && i + k < n && k + rad[i - k] < j) {
                rad[i + k] = rad[i - k];
                k++;
            }
            i += k;
            j -= k;
        }
    }

    // [l,r)
    bool isPalindrome(int l, int r) {
        return rad[l + r - 1] >= r - l;
    }

private:
    char get(int k) {
        return k % 2 == 0 ? s[k / 2] : '$';
    }
};

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

    vector<int64_t> P(n + 1);
    for (int i = 1; i < n; i++) {
        if (mana.isPalindrome(0, i)) {
            P[i]++;
        }
    }
    vector<int64_t> PP(n + 1);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (mana.isPalindrome(i, j)) {
                PP[j + 1] += P[i];
            }
        }
    }
    for (int i = 0; i < n; i++) {
        PP[i + 1] += PP[i];
    }
    int64_t PPAP = 0;
    for (int i = 0; i < n; i++) {
        if (mana.isPalindrome(i, n)) {
            PPAP += PP[i];
        }
    }
    cout << PPAP << endl;
}
0