Submission #631104

#TimeUsernameProblemLanguageResultExecution timeMemory
631104inksamuraiRima (COCI17_rima)C++17
140 / 140
283 ms111052 KiB
#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()); 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({}); usd.pb(0); } rt=trie[rt].nxt[x]; } usd[rt]=1; } 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]; } } 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 timeMemoryGrader output
Fetching results...