Submission #245391

# Submission time Handle Problem Language Result Execution time Memory
245391 2020-07-06T09:07:54 Z Vladikus004 Rima (COCI17_rima) C++14
140 / 140
327 ms 150880 KB
#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

const int N = 500000 + 3;
int n;
string s[N];

struct node{
    int ed, dp;
    node *next[26];
    node(){
        ed = dp = 0;
        for (int i = 0; i < 26; i++) next[i] = nullptr;
    }
};

node *root = new node();

void add(string &s){
    node *cur = root;
    for (auto u: s){
        if (cur->next[u - 'a'] == nullptr)
            cur->next[u - 'a'] = new node;
        cur = cur->next[u - 'a'];
    }
    cur->ed++;
}

int ans = 0;

void dfs(node *cur){
    pii mx = {0, 0};
    int sum = 0;
    for (int i = 0; i < 26; i++){
        if (cur->next[i] == nullptr) continue;
        dfs(cur->next[i]);
        sum += cur->next[i]->ed;
        cur->dp = max(cur->dp, cur->next[i]->dp);
        mx = max(mx, {mx.first, cur->next[i]->dp});
        mx = max(mx, {cur->next[i]->dp, mx.first});
    }
    if (cur->ed) {
        cur->dp += 1;
        cur->dp += max(0, sum - 1);
    }else cur->dp = 0;
    ans = max(ans, mx.first + mx.second + cur->ed + max(0, sum - 2));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif // LOCAL
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
        reverse(all(s[i]));
        add(s[i]);
    }
    dfs(root);
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 16128 KB Output is correct
2 Correct 16 ms 16128 KB Output is correct
3 Correct 13 ms 16000 KB Output is correct
4 Correct 327 ms 150880 KB Output is correct
5 Correct 32 ms 20256 KB Output is correct
6 Correct 93 ms 107020 KB Output is correct
7 Correct 92 ms 106780 KB Output is correct
8 Correct 99 ms 106580 KB Output is correct
9 Correct 108 ms 112468 KB Output is correct
10 Correct 97 ms 106496 KB Output is correct