# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
696427 | mousey | Rima (COCI17_rima) | C++14 | 272 ms | 117068 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#define int long long
#define vi vector <int>
#define vv vector <vi>
#define pi pair <int, int>
#define vip vector <pi>
#define pb push_back
#define f first
#define s second
#define nl cout<<"\n"
using namespace std;
const int mod=1e9+7;
const double eps=1e-9;
const int N=5e5;
int n;
struct Node{
int next[26]={};
bool end=false;
};
struct Trie{
vector <Node> tree;
vi dp;
int cur=1, ans=0;
Trie()
{
tree.emplace_back();
dp.emplace_back();
}
void add(string s)
{
int crr=0;
for(int j = s.size()-1; j >= 0; j--)
{
char i=s[j];
if(tree[crr].next[i-'a']) crr=tree[crr].next[i-'a'];
else tree.emplace_back(), dp.emplace_back(), tree[crr].next[i-'a']=cur, crr=cur, cur++;
}
tree[crr].end=true;
}
void dfs(int u)
{
int sum=0;
vi v;
for(int i = 0; i < 26; i++)
{
if(tree[u].next[i])
{
dfs(tree[u].next[i]);
if(tree[tree[u].next[i]].end)
{
v.pb(dp[tree[u].next[i]]);
dp[u]=max(dp[u], dp[tree[u].next[i]]);
sum++;
}
}
}
if(sum>0) dp[u]+=sum-1;
if(tree[u].end) dp[u]++;
if(tree[u].end || u==0)
{
if(v.size()>1)
{
sort(v.begin(), v.end());
if(u==0) ans=max(ans, v[v.size()-1]+v[v.size()-2]+sum-2);
else ans=max(ans, v[v.size()-1]+v[v.size()-2]+1+sum-2);
}
}
ans=max(ans, dp[u]);
}
};
Trie T;
void input()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
T.add(s);
}
}
void solve()
{
T.dfs(0);
cout << T.ans;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// freopen("math.inp", "r", stdin);
// freopen("math.out", "w", stdout);
// cin >> t;
for(int i = 1; i <= t; i++)
{
input();
solve();
nl;
}
}
/*
6
a
b
c
d
e
f
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |