結果

問題 No.1512 作文
ユーザー TomorrowNextTomorrowNext
提出日時 2021-05-21 21:48:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,478 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 177,012 KB
実行使用メモリ 43,880 KB
最終ジャッジ日時 2024-04-18 15:04:20
合計ジャッジ時間 3,735 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

bool isGood(const string& s) {
    for (int i = 1; i < s.size(); ++i) {
        if (s[i - 1] > s[i]) {
            return false;
        }
    }
    return true;
}

int dp(int curr, char last, const vector<pair<pair<char, char>, int>>& S, vector<vector<int>>& memo) {
    if (curr == S.size()) {
        return 0;
    }
    if (memo[curr][last - 'a'] >= 0) {
        return memo[curr][last - 'a'];
    }

    int ret = 0;
    if (last <= S[curr].first.second) {
        int take = dp(curr + 1, S[curr].first.first, S, memo) + S[curr].second;
        int skip = dp(curr + 1, last, S, memo);
        ret = max(take, skip);
    }
    else {
        ret = dp(curr + 1, last, S, memo);
    }
    memo[curr][last - 'a'] = ret;
    return ret;
}

void Main() {
    int N;
    cin >> N;
    vector<pair<pair<char, char>, int>> S;
    for (int i = 0; i < N; ++i) {
        string s;
        cin >> s;
        if (isGood(s)) {
            S.push_back(make_pair(make_pair(s[s.size() - 1], s[0]), s.size()));
        }
    }
    sort(S.begin(), S.end());

    if (S.size() == 0) {
        cout << 0 << endl;
        return;
    }
    vector<vector<int>> memo(S.size() + 10, vector<int>(26, -1));
    int ans = dp(0, 'a', S, memo);
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}
0