結果

問題 No.1512 作文
ユーザー Manuel1024Manuel1024
提出日時 2021-05-21 22:31:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,916 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 78,880 KB
実行使用メモリ 9,704 KB
最終ジャッジ日時 2024-04-18 16:04:11
合計ジャッジ時間 2,198 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 6 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 6 ms
5,376 KB
testcase_36 AC 6 ms
5,376 KB
testcase_37 AC 4 ms
5,376 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 25 ms
9,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<vector<int>> goodstring(26, vector<int>(26, 0));
vector<vector<int>> dp(27, vector<int>(27, -1));

int f(int i, int j){
    if(dp[i][j] != -1) return dp[i][j];
    dp[i][j] = goodstring[i][j];
    for(int k = i; k <= j; k++){
        for(int kk = k; kk <= j; kk++){
            if(f(i, k) > 0 && f(kk, j) > 0){
                if((i == k && k == kk)) dp[i][j] = max(dp[i][j], max(f(i, k), f(kk, j)));
                else if((j == kk && k == kk)) dp[i][j] = max(dp[i][j], max(f(i, k), f(kk, j)));
                else dp[i][j] = max(dp[i][j], f(i, k) + f(kk, j));
            }
        }
    }
    //cerr << i << " " << j << " = " << dp[i][j] << endl;
    return dp[i][j];
}

int main(){
    int n;
    cin >> n;
    vector<string> s(n);
    for(auto &p: s) cin >> p;
    
    
    for(int ii = 0; ii < n; ii++){
        auto &p = s[ii];
        bool isgood = true;
        for(int i = 1; i < p.length(); i++){
            if(p[i-1] > p[i]) isgood = false;
        }
        if(isgood){
            int start = *p.begin()-'a';
            int end = *p.rbegin()-'a';
            if(start == end) goodstring[start][end] += p.length();
            if(start != end) goodstring[start][end] = max(goodstring[start][end], (int)p.length());
        }
    }

    for(int i = 0; i < 26; i++){
        dp[i][i] = goodstring[i][i];
        for(int j = i+1; j < 26; j++){
            if(goodstring[i][j]) goodstring[i][j] += goodstring[i][i];
        }
        for(int j = i-1; j >= 0; j--){
            if(goodstring[j][i]) goodstring[j][i] += goodstring[i][i];
        }
    }

    int ans = 0;
    for(int i = 0; i < 26; i++){
        for(int j = 0; j < 26; j++){
            ans = max(ans, f(i, j));
            //cerr << f(i, j) << " ";
        }
        //cerr << endl;
    }

    cout << ans << endl;
    return 0;
}
0