admin管理员组

文章数量:1616032

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3263    Accepted Submission(s): 1210


 

Problem Description

Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence.

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST

Source

CTU Open Contest 2007

问题链接:HDU2328 Corporate Identity

问题描述:给定字符串集合,输出它们的最长公共子串(如果有多个则输出字典序小的)

解题思路:枚举第一个字符串的各个子串,然后查看是否是其他所有字符串的子串即可,使用find函数

AC的C++程序:

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

const int N=4005;
string s[N];

int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;i++)
		  cin>>s[i];
		//枚举s[0]的每个子串 按长度递减顺序
		string ans;
		bool flag=false; 
		for(int l=s[0].length();l>0&&!flag;l--)//子串长度
		{
		   for(int i=0;i+l<=s[0].length();i++)//子串起始位置 
		   {
		   		string s1=s[0].substr(i,l);
				bool flag2=true;//进行查找
				//查看其他各个子串中是否含有s1
				for(int j=1;j<n;j++)
				  if(s[j].find(s1,0)==-1)
				  {
				  	flag2=false;
				  	break;
				  }
				if(flag2)
				{
					if(!flag||ans>s1)
					  ans=s1;
					flag=true;//找到答案 
				} 
			}
		}
		if(flag)
		  cout<<ans<<endl;
		else
		  cout<<"IDENTITY LOST"<<endl;
	}
	return 0;
 } 

 

本文标签: CorporateKMPIDENTITY