# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
960543 | LM1 | Rima (COCI17_rima) | C++14 | 365 ms | 218752 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
using namespace std;
const int N=5e5+5,M=3e6+5;
int t,n,dp[M],fix[M][26],ans=1,cn,cnt[M];
vector<int>v[M];
string s;
void add(string s){
int cur=0;
for(int i=0;i<(int)s.size();i++){
int c=s[i]-'a';
if(!fix[cur][c]){
cn++;
fix[cur][c]=cn;
v[cur].push_back(fix[cur][c]);
}
cur=fix[cur][c];
}
cnt[cur]++;
}
void dfs(int st,int p){
if(v[st].size()==0 and st>0){
dp[st]=1;
return;
}
int mx=0;
vector<int>v1;
for (int i:v[st]){
if (i==p)continue;
dfs(i,st);
mx=max(mx,dp[i]);
if(dp[i])v1.push_back(dp[i]);
}
sort(v1.begin(),v1.end());
if(v1.size()>=2)ans=max(ans,v1.back()+v1[v1.size()-2]+(int)v1.size()-2);
ans=max(ans,mx+(int)v1.size()-(!cnt[st]));
if(cnt[st])dp[st]=mx+1+v1.size()-min((int)v1.size(),(int)1);
else dp[st]=0;
}
main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s;
reverse(s.begin(),s.end());
add(s);
}
dfs(0,0);
cout<<ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |