Submission #701360

# Submission time Handle Problem Language Result Execution time Memory
701360 2023-02-21T04:08:32 Z fdnfksd Rima (COCI17_rima) C++14
140 / 140
285 ms 160084 KB
#include<bits/stdc++.h>
#define TASKNAME "bieudo"
#define pb push_back
#define pli pair<int,int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=5e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct TrieNode
{
    ll val;
    TrieNode* child[26];
    ll ct;
    TrieNode()
    {
        val=0;
        ct=0;
        for(int i=0;i<26;i++) child[i]=nullptr;
    }
};
TrieNode *root=new TrieNode();
void add(string s)
{
    auto p=root;
    ll n=s.size()-1;
    for(int i=1;i<=n;i++)
    {
        if(p->child[s[i]-'a']==nullptr)
        {
            p->child[s[i]-'a']=new TrieNode();
        }
        p=p->child[s[i]-'a'];
    }
    p->ct++;
}
ll ans=0;
void dfs(TrieNode* u)
{
    vector<ll> vec;
    for(int i=0;i<26;i++)
    {
        if(u->child[i]!=nullptr)
        {
            dfs(u->child[i]);
            auto v=u->child[i];
            if(v->ct>0)
            {
                vec.pb(v->val);
            }
        }
    }
    ll bonus=u->ct;
    sort(vec.begin(),vec.end(),greater<int>());
    if(vec.size()==0) u->val=bonus;
    else
    {
        u->val=bonus+vec.size()-1+vec[0];
    }
    ans=max(ans,u->val);
    if(vec.size()>=2)
    {
        ans=max(ans,bonus+(ll)vec.size()-2+vec[0]+vec[1]);
    }
}
ll n;
string s[maxN];
void solve()
{
    cin >> n;
    //ll ans=0;
    for(int i=1;i<=n;i++)
    {
        cin >> s[i];
        reverse(s[i].begin(),s[i].end());
        s[i]=' '+s[i];
        add(s[i]);
    }
    dfs(root);
    cout << ans;
}
int main()
{
    fastio
    //freopen(TASKNAME".INP","r",stdin);
    //freopen(TASKNAME".OUT","w",stdout);
    solve();
}

Compilation message

rima.cpp:9:14: warning: overflow in conversion from 'double' to 'll' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
    9 | const ll inf=1e18;
      |              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15956 KB Output is correct
2 Correct 8 ms 15956 KB Output is correct
3 Correct 8 ms 15956 KB Output is correct
4 Correct 285 ms 160084 KB Output is correct
5 Correct 22 ms 19556 KB Output is correct
6 Correct 76 ms 111148 KB Output is correct
7 Correct 70 ms 110872 KB Output is correct
8 Correct 69 ms 110816 KB Output is correct
9 Correct 91 ms 116816 KB Output is correct
10 Correct 73 ms 110880 KB Output is correct