The 2022 ICPC Asia Xian Regional Contest--G. Perfect Word

You are given nn strings and required to find the length of the longest perfect word.

A string t is called a perfect word, if and only if every non-empty substring of t appears in the given strings.

A string s is called a substring of t if and only if it can be obtained by removing several (possibly zero) characters from the beginning or end of t.

Input

The first line contains a single integer n (1≤n≤10^5).

Each of the next nn lines contains a string consisting of lowercase English letters.

It is guaranteed that the total length of the given strings is no more than 10^5.

Output

Output an integer, representing the length of the longest perfect word.

Example

input

4
a
t
b
ab

output

2

题意:找出最大长度的一个字符串,他的字串全部出现过

解析:我们输入时候先用map记录每个字符出现,然后暴力n个字符串的每个字串,因为题目中说n个字符串总长度最大10的5次,枚举长度为1,2,3....n的字串,操作次数n+n/2+n/3+n/4+....+1,是调和级数,复杂度是O(N)的,因此没有问题,暴力即可

#include 
#include 
#include 
#include 
#include 
using namespace std;
string s[100005];
unordered_map mp;
bool cmp(string a,string b)//因为要求最大长度,按长度从大到小排序
{
	return a.size()>b.size();
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		mp[s[i]]=1;
	} 
	sort(s+1,s+n+1,cmp);
	for(int i=1;i<=n;i++)
	{
		int l=s[i].size(),flag=1;//flag记录该字符串是否满足条件
		for(int j=1;j<=l;j++)//枚举长度为j的字串
		{
			for(int k=0;k<=l-j;k++)
			{
				string a=s[i].substr(k,j);//字串
				if(!mp[a])//如果没出现,不满足条件
				{
					flag=0;
					break;
				}
			}
			if(!flag) break;
		}
		if(flag)
		{
			cout<

你可能感兴趣的:(算法,c++,c语言)