結果

問題 No.1512 作文
ユーザー Mister
提出日時 2021-05-21 22:03:25
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,037 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 77,324 KB
最終ジャッジ日時 2025-01-21 15:18:31
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>

using namespace std;
constexpr int K = 26;

void solve() {
    int n;
    cin >> n;

    vector<vector<pair<int, int>>> graph(K);
    vector<int> lens(K, 0);

    while (n--) {
        string s;
        cin >> s;
        int m = s.length();

        bool judge = true;
        for (int i = 0; i + 1 < m; ++i) {
            if (s[i] > s[i + 1]) judge = false;
        }

        if (judge) {
            int i = s.front() - 'a',
                j = s.back() - 'a';

            if (i == j) {
                lens[i] += m;
            } else {
                graph[i].emplace_back(j, m);
            }
        }
    }

    vector<int> dp(K + 1, 0);
    for (int i = 0; i < 26; ++i) {
        dp[i] += lens[i];
        for (auto [j, m] : graph[i]) {
            dp[j] = max(dp[j], dp[i] + m);
        }
        dp[i + 1] = max(dp[i + 1], dp[i]);
    }

    cout << dp[K] << "\n";
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();

    return 0;
}
0