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 <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;
string s[100005];
unordered_map<string,int> 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<<s[i].size()<<endl;
			return 0;
		}
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值