結果

問題 No.464 PPAP
ユーザー mamekinmamekin
提出日時 2017-06-11 19:48:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 2,062 bytes
コンパイル時間 1,000 ms
コンパイル使用メモリ 107,664 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 21:15:18
合計ジャッジ時間 2,128 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 1 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 12 ms
4,348 KB
testcase_11 AC 4 ms
4,348 KB
testcase_12 AC 6 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class ManacherAlgorithm
{
private:
    static void solve(const string& s, vector<int>& len)
    {
        int n = s.size();
        int i = 0;
        int j = 0;
        len.resize(n);

        while (i < n) {
            while(i-j >= 0 && i+j < n && s[i-j] == s[i+j])
                ++ j;
            len[i] = j;

            int k = 1;
            while(i-k >= 0 && i+k < n && k+len[i-k] < j){
                len[i+k] = len[i-k];
                ++ k;
            }
            i += k;
            j -= k;
        }
    }
public:
    static void getPalindromeLen(const string& s, vector<int>& len)
    {
        int n = s.size();
        string t(2*n-1, '$');
        for(int i=0; i<n; ++i)
            t[2*i] = s[i];

        vector<int> oddLen, evenLen;
        solve(s, oddLen);
        solve(t, evenLen);

        len.assign(2*n-1, 0);
        for(int i=0; i<n; ++i)
            len[2*i] = oddLen[i] * 2 - 1;
        for(int i=0; i<n-1; ++i){
            if(evenLen[2*i+1] > 1)
                len[2*i+1] = evenLen[2*i+1];
        }
    }
};

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

    vector<int> len;
    ManacherAlgorithm::getPalindromeLen(s, len);

    vector<int> cnt(n, 0);
    for(int a=1; a<n; ++a){
        if(len[a-1] < a)
            continue;
        for(int b=1; a+b<n; ++b){
            if(len[2*a+b-1] >= b)
                ++ cnt[a+b];
        }
    }

    int tmp = 0;
    long long ans = 0;
    for(int a=1; a<n; ++a){
        if(len[2*n-1-a] >= a)
            ++ tmp;
        ans += tmp * (long long)cnt[n-1-a];
    }
    cout << ans << endl;

    return 0;
}
0