Submission #96833

# Submission time Handle Problem Language Result Execution time Memory
96833 2019-02-12T12:57:19 Z tieunhi Rima (COCI17_rima) C++14
140 / 140
845 ms 134904 KB
#include <bits/stdc++.h>
#define FOR(i, u, v) for (int i = u; i <= v; i++)
#define FORD(i, v, u) for (int i = v; i >= u; i--)
#define ll long long
#define pii pair<int, int>
#define PB push_back
#define mp make_pair
#define F first
#define S second
#define N 3000005
#define BASE 37
#define mod 1000000007
#define mid (r + l)/2

using namespace std;

int n, dp[N], res;

struct Trie
{
    Trie *child[26];
    int cnt, dp;
    Trie()
    {
        FOR(i, 0, 25) child[i] = NULL;
        cnt = 0, dp = 0;
    }
}t;

void DFS(Trie *u)
{
    int f1 = 0, f2 = 0;
    int add = 0;
    FOR(c, 0, 25)
    {
        if (u->child[c] == NULL) continue;
        Trie *v = u->child[c];
        DFS(v);
        add += v->cnt;
        if (v->dp >= f1) f2 = f1, f1 = v->dp;
        else if (v->dp > f2) f2 = v->dp;

        u->dp = max(u->dp, v->dp);
    }
    if (u->cnt) u->dp += 1 + max(0, add-1);
    else u->dp = 0;
    res = max(res, f1 + f2 + u->cnt + max(0, add-2));
}
int main()
{
    ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
    //freopen("INP.TXT", "r", stdin);
    //freopen("OUT.TXT", "w", stdout);
    cin >> n;
    FOR(i, 1, n)
    {
        string s; cin >> s;
        Trie *p = &t;
    	FORD(i, s.size()-1, 0)
    	{
        	int c = s[i] - 'a';
        	if (p->child[c] == NULL) p->child[c] = new Trie();
        	p = p->child[c];
    	}
    	p->cnt++;
    }
    DFS(&t);
    cout <<res;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 845 ms 134904 KB Output is correct
5 Correct 34 ms 1144 KB Output is correct
6 Correct 110 ms 90184 KB Output is correct
7 Correct 105 ms 90220 KB Output is correct
8 Correct 130 ms 89888 KB Output is correct
9 Correct 130 ms 93216 KB Output is correct
10 Correct 115 ms 89928 KB Output is correct