답안 #631104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631104 2022-08-17T17:07:45 Z inksamurai Rima (COCI17_rima) C++17
140 / 140
283 ms 111052 KB
#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);

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 283 ms 111052 KB Output is correct
5 Correct 17 ms 828 KB Output is correct
6 Correct 80 ms 56052 KB Output is correct
7 Correct 88 ms 56036 KB Output is correct
8 Correct 80 ms 55956 KB Output is correct
9 Correct 104 ms 56100 KB Output is correct
10 Correct 81 ms 56052 KB Output is correct