結果

問題 No.464 PPAP
ユーザー face4face4
提出日時 2018-11-12 17:56:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 1,061 bytes
コンパイル時間 629 ms
コンパイル使用メモリ 72,776 KB
実行使用メモリ 113,536 KB
最終ジャッジ日時 2023-08-25 02:49:18
合計ジャッジ時間 3,747 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,424 KB
testcase_01 AC 2 ms
5,444 KB
testcase_02 AC 2 ms
5,500 KB
testcase_03 AC 2 ms
5,756 KB
testcase_04 AC 3 ms
8,196 KB
testcase_05 AC 3 ms
7,912 KB
testcase_06 AC 3 ms
7,860 KB
testcase_07 AC 13 ms
20,304 KB
testcase_08 AC 12 ms
19,556 KB
testcase_09 AC 8 ms
17,876 KB
testcase_10 AC 347 ms
113,536 KB
testcase_11 AC 72 ms
57,112 KB
testcase_12 AC 223 ms
70,636 KB
testcase_13 AC 85 ms
52,332 KB
testcase_14 AC 287 ms
93,444 KB
testcase_15 AC 2 ms
5,476 KB
testcase_16 AC 3 ms
5,524 KB
testcase_17 AC 3 ms
7,620 KB
testcase_18 AC 2 ms
5,484 KB
testcase_19 AC 3 ms
7,900 KB
testcase_20 AC 3 ms
7,852 KB
testcase_21 AC 3 ms
7,656 KB
testcase_22 AC 3 ms
7,656 KB
testcase_23 AC 89 ms
52,236 KB
testcase_24 AC 167 ms
52,508 KB
testcase_25 AC 168 ms
52,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

string s;
bool visit[5000][5000] = {}, memo[5000][5000] = {};

bool isPalindrome(int i, int j){
    if(visit[i][j]) return memo[i][j];

    visit[i][j] = true;
    
    if(i + 1 == j){
        return memo[i][j] = (s[i]==s[j]);
    }else{
        bool b = (s[i] == s[j]) && isPalindrome(i+1, j-1);
        return memo[i][j] = b;
    }
}

int main(){
    cin >> s;
    
    int n = s.length();
    vector<int> v[n], aft(n, 0);

    for(int i = 0; i < n; i++)  visit[i][i] = memo[i][i] = true;

    for(int i = 0; i < n; i++){
        for(int j = i; j < n-2; j++){
            if(isPalindrome(i,j))   v[i].push_back(j);
        }
    }

    for(int i = 0; i < n; i++){
        if(isPalindrome(i, n-1))    aft[i] = 1;
    }

    for(int i = n-2; i >= 0; i--)   aft[i] += aft[i+1];

    // 最悪計算量はO(n^2)なので間に合うはず...?
    long long ans = 0;
    for(int i : v[0]){
        for(int j : v[i+1]){
            ans += aft[j+2];
        }
    }

    cout << ans << endl;

    return 0;
}
0