Corporate Identity
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1917 Accepted Submission(s): 734
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.
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.
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
求n个串里面的最长公共子串,长度相同的,按字典序从小到大输出
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n;
char s[4005][205];
int nex[205];
char c[205];
void get_nex(char s[])
{
int k=-1;
int j=0;
nex[0]=-1;
int len=strlen(s);
while(j<len-1)
{
if(k==-1||s[j]==s[k])
{
j++;
k++;
nex[j]=k;
}
else
k=nex[k];
}
}
bool KMP(char s[],char t[])
{
int i=0,j=0;
int len1=strlen(s);
int len2=strlen(t);
while(i<len1&&j<len2)
{
if(j==-1||s[i]==t[j])
{
i++;
j++;
}
else
j=nex[j];
}
if(j==len2)
return true;
return false;
}
int main()
{
while(~scanf("%d",&n))
{
if(n==0) return 0;
int now=0;
int tmp=205;
for(int i=1; i<=n; i++)
{
scanf("%s",s[i]);
if(tmp>strlen(s[i]))
{
tmp=strlen(s[i]);
now=i;
}
}
char ans[205]="";
int flag=0;
for(int len=tmp; len>=1; len--)//这两个for循环的作用:枚举最短的字符串的所有子串
{
for(int i=0; i<=tmp-len; i++)
{
strncpy(c,s[now]+i,len);
c[len]='\0';
get_nex(c);
int j=1;
for(j=1; j<=n; j++)//遍历所有字符串(除自己以外),并判断是否是它的母串
{//只要存在一个不是它的母串,则肯定不成立,直接跳出
if(j==now) continue;
if(!KMP(s[j],c))
{
break;
}
}
if(j==n+1)
{
flag=1;
if(strcmp(c,ans)<0||!strlen(ans))
strncpy(ans,c,strlen(c));
}
}
if(flag) break;
}
if(flag==0)
printf("IDENTITY LOST\n");
else
printf("%s\n",ans);
}
return 0;
}