답안 #520677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520677 2022-01-30T18:02:20 Z HaYoungJoon Rima (COCI17_rima) C++14
140 / 140
326 ms 160548 KB
#include <bits/stdc++.h>

using namespace std;

const int maxsize = 3*1e5;
int trie[3000001][26], timer = 0;
int dp[3000001], n, res = 0;
string a[500001];
vector<int> adj[3000001];

void addString(string s) {
    int cur = 0;
    for (char c: s) {
        int val = c - 'a';
        if (!trie[cur][val]) {
            adj[cur].emplace_back(++timer);
            trie[cur][val] = timer;
        }
        cur = trie[cur][val];
    }
    dp[cur] = 1;
}

void DFS(int u) {
    int maxnhanh1 = 0, maxnhanh2 = 0;
    for (int v: adj[u]) {
        DFS(v);
        if (dp[v] > maxnhanh1) {
            maxnhanh2 = maxnhanh1;
            maxnhanh1 = dp[v];
        } else maxnhanh2 = max(maxnhanh2,dp[v]);
    }
    int total = 0;
    for (int v: adj[u]) total += (dp[v] > 0);
    if (maxnhanh1) total += maxnhanh1 - 1;
    if (maxnhanh2) total += maxnhanh2 - 1;
    res = max(res,total + dp[u]);
    if (dp[u] && maxnhanh1) {
        for (int v: adj[u]) dp[u] += (dp[v] > 0);
        dp[u] += maxnhanh1 - 1;
        res = max(dp[u],res);
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        reverse(a[i].begin(),a[i].end());
    }
    for (int i = 1; i <= n; i++) addString(a[i]);

    DFS(0);
    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 86468 KB Output is correct
2 Correct 40 ms 86468 KB Output is correct
3 Correct 40 ms 86340 KB Output is correct
4 Correct 326 ms 160548 KB Output is correct
5 Correct 61 ms 89860 KB Output is correct
6 Correct 94 ms 150656 KB Output is correct
7 Correct 87 ms 150572 KB Output is correct
8 Correct 86 ms 150408 KB Output is correct
9 Correct 110 ms 154756 KB Output is correct
10 Correct 85 ms 150480 KB Output is correct