# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
631102 | inksamurai | Rima (COCI17_rima) | C++17 | 298 ms | 111136 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>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct N{
int nxt[26];
};
signed main(){
_3phCa4T;
int n;
cin>>n;
vec(N) trie;
trie.pb({});
vi usd={0};
rep(i,n){
string s;
cin>>s;
reverse(s.begin(), s.end());
// print(s);
int rt=0;
rep(j,sz(s)){
int x=s[j]-'a';
if(!trie[rt].nxt[x]){
trie[rt].nxt[x]=sz(trie);
trie.pb({});
// print(trie[rt].nxt[x]);
usd.pb(0);
}
rt=trie[rt].nxt[x];
}
usd[rt]=1;
// print(rt);
}
vi dp(sz(trie));
int ans=0;
auto dfs=[&](auto self,int v)->void{
int cnt=0;
pii p={0,0};
rep(x,26){
int u=trie[v].nxt[x];
if(u){
self(self,u);
if(dp[u]>p.fi){
p.se=p.fi;
p.fi=dp[u];
}else if(dp[u]>p.se){
p.se=dp[u];
}
cnt+=usd[u];
}
}
// print(v,p.fi,p.se,usd[v]);
ans=max(ans,p.fi+p.se+cnt-min(cnt,2)+usd[v]);
if(usd[v]){
dp[v]=1+p.fi+cnt-min(cnt,1);
}
};
dfs(dfs,0);
print(ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |