답안 #1109441

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1109441 2024-11-06T17:34:13 Z mingga Rima (COCI17_rima) C++17
140 / 140
287 ms 136356 KB
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
// #define int long long
const int MOD = 1e9 + 7;
const int inf = 2e9;
const int N = 3e6 + 7;

struct Trie {
    struct Node {
        Node * child[26];
        int cnt;
        Node() {
            for(int i = 0; i < 26; i++) child[i] = NULL;
            cnt = 0;
        }
    };
    int ans;
    Node* root;
    Trie(){
        root = new Node();
    };
    void add(string s) {
        Node * p = root;
        for(char c: s) {
            int i = c - 'a';
            if(p->child[i] == NULL) p->child[i] = new Node();
            p = p->child[i];
        }
        p->cnt = 1;
    }
    int dfs(Node * p) {
        int ans1 = 0, ans2 = 0;
        int sus = 0;
        int cnt = 0;
        for(int i = 0; i < 26; i++) {
            if(p->child[i] == NULL) continue;
            Node * v = p->child[i];
            int x = dfs(v);
            if(v->cnt) {
                cnt++;
                if(x >= ans1) {
                    ans2 = ans1;
                    ans1 = x;
                } else if(x > ans2) {
                    ans2 = x;
                }
            }
        }
        sus = p->cnt;
        if(cnt) {
            sus += cnt - 1 + ans1;
        }
        ans = max(ans, sus);
        if(cnt > 1) {
            ans = max(ans, ans1 + ans2 + cnt - 2 + p->cnt);
        }
        return sus;
    }
} tri;

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
   // if(fopen("rima.inp", "r")) {
   //     freopen("rima.inp", "r", stdin);
   // }
    int n; cin >> n;
    for(int i = 1; i <= n; i++) {
        string s; cin >> s;
        reverse(all(s));
        tri.add(s);
    }
    tri.dfs(tri.root);
    cout << tri.ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 287 ms 136356 KB Output is correct
5 Correct 13 ms 2640 KB Output is correct
6 Correct 82 ms 78772 KB Output is correct
7 Correct 77 ms 78636 KB Output is correct
8 Correct 84 ms 78936 KB Output is correct
9 Correct 109 ms 84052 KB Output is correct
10 Correct 80 ms 78844 KB Output is correct