Submission #285759

# Submission time Handle Problem Language Result Execution time Memory
285759 2020-08-29T14:44:27 Z HynDuf Rima (COCI17_rima) C++14
140 / 140
358 ms 134832 KB
#include <bits/stdc++.h>

#define task "R"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5e5 + 2;
int n, ans;
struct Node
{
    Node *child[26];
    int ans;
    bool leaf;
    Node() {ans = 0; leaf = 0; rep(i, 0, 25) child[i] = nullptr;}
};
Node *root = new Node();
void add(const string &t)
{
    Node *u = root;
    rep(i, 0, SZ(t) - 1)
    {
        if (u->child[t[i] - 'a'] == nullptr) u->child[t[i] - 'a'] = new Node();
        u = u->child[t[i] - 'a'];
    }
    u->leaf = 1;
}
void dfs(Node *u)
{
    int cnt = 0, mx = 0, mx1 = 0;
    rep(i, 0, 25) cnt += (u->child[i] != nullptr && (u->child[i])->leaf);
    cnt += u->leaf;
    rep(i, 0, 25) if (u->child[i] != nullptr)
    {
        Node *v = u->child[i];
        dfs(v);
        if (v->leaf)
        {
            if (v->ans > mx) mx1 = mx, mx = v->ans;
            else if (v->ans > mx1) mx1 = v->ans;
        }
    }
    if (u->leaf) u->ans = cnt + mx - (mx != 0);
    ans = max({ans, u->ans, mx + mx1 + cnt - 2});
}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n;
    rep(i, 1, n)
    {
        string s;
        cin >> s;
        reverse(all(s));
        add(s);
    }
    dfs(root);
    cout << ans;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 358 ms 134832 KB Output is correct
5 Correct 16 ms 1024 KB Output is correct
6 Correct 99 ms 90196 KB Output is correct
7 Correct 96 ms 90100 KB Output is correct
8 Correct 96 ms 89900 KB Output is correct
9 Correct 122 ms 93292 KB Output is correct
10 Correct 96 ms 89960 KB Output is correct