56分求助
查看原帖
56分求助
520171
wjy959楼主2023/10/13 19:13

AC:1 2 3 5

#include <bits/stdc++.h>
using namespace std;
const int N=3e6+6;
int n,tot,f[N],ans=1,ch[N][30];
char a[N];
int stop[N];
inline void build()
{
	int u=0,sz=strlen(a);
	for(int i=sz-1;i>=0;i--)
	{
		if(ch[u][a[i]-'a']==0)
		{
			ch[u][a[i]-'a']=++tot;
		}
		u=ch[u][a[i]-'a'];
	}
	stop[u]=1;
}
inline void dfs(int x)
{
	vector<int> b;
	b.clear();
	for(int i=0;i<=26;i++)
	{
		if(ch[x][i])
		{
			dfs(ch[x][i]);
			b.push_back(f[ch[x][i]]);
		}
	}
	if(stop[x]==0)
	{
		f[x]=0;
		return ;
	}
	if(b.size()==0)
	{
		f[x]=1;
		return ;
	}
	sort(b.begin(),b.end(),greater<int>());
	if(b.size()==1)
	{
		f[x]=stop[x]+b[0];
	}
	else
	{
		f[x]=stop[x]+b[0]+b[1]+b.size()-2;
	}
	ans=max(ans,f[x]);
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a;
		build();
	}
	dfs(0);
	cout<<ans<<"\n";
	return 0;
}
2023/10/13 19:13
加载中...