답안 #1031918

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031918 2024-07-23T08:42:36 Z Whisper Rima (COCI17_rima) C++17
140 / 140
236 ms 162752 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
int n;
#define All(x...) (x).begin(), (x).end()

struct Node{
    Node* child[26];
    int last, dp;
    Node(){
        REP(i, 26) child[i] = nullptr;
        last = 0, dp = 0;
    }
};
int numNode = 0;
Node *root = new Node();
void Add(string &s){
    Node *p = root;
    for (char c : s){
        int nxt = (c - 'a');
        if (p -> child[nxt] == nullptr){
            p -> child[nxt] = new Node();
        }
        p = p -> child[nxt];
    }
    p -> last = 1;
}
int ans = 0;
void dfs(Node *p){
    REP(c, 26) if(p -> child[c] != nullptr){
        dfs(p -> child[c]);
    }
    int cnt = 0;
    int mx_first = 0, mx_second = 0;
    REP(c, 26){
        if (p -> child[c] != nullptr){
            if (p -> child[c] -> last){
                    ++cnt;
                if (p -> child[c] -> dp > mx_first) swap(mx_first, mx_second), mx_first = p -> child[c] -> dp;
                else maximize(mx_second, p -> child[c] -> dp);
            }
        }
    }
    p -> dp = mx_first + cnt;
    maximize(ans, p -> dp + mx_second + p -> last);
}
void process(void){
    cin >> n;
    vector<string> s(n);
    for (string&i : s) cin >> i;
    for (string&i : s) reverse(All(i)), Add(i);
    dfs(root);
    cout << ans;
}

signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
}



# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 236 ms 162752 KB Output is correct
5 Correct 12 ms 6908 KB Output is correct
6 Correct 59 ms 86764 KB Output is correct
7 Correct 64 ms 86584 KB Output is correct
8 Correct 75 ms 86088 KB Output is correct
9 Correct 72 ms 95264 KB Output is correct
10 Correct 60 ms 86160 KB Output is correct