类似于3080 稍作改动,即可AC。。
#include<iostream>
using namespace std;
int next[205];
char str[205];
char ans[205];
char s[5050][205];
int h;
int le;
void get_next(char *T,int len)
{
int j=0,k=-1;
next[0]=-1;
while(j<len)
{
if(k==-1||T[j]==T[k])
{
next[j+1]=k+1;
j++;k++;
}
else
k=next[k];
}
//for(int i=0;i<len;i++)
//printf("%d ",next[i]);
}
bool find(char *str)
{
int len=strlen(str);
int i=0,j=0;
while(i<=le&&j<len)
{
if(j==-1||s[h][i]==str[j])
{
j++;i++;
}
else
j=next[j];
}
if(j==len) return true;
else return false;
}
int main()
{
int k,l,n;
while(scanf("%d",&n)!=EOF&&n)
{
for(int i=0;i<n;i++)
scanf("%s",s[i]);
le=strlen(s[0]);
int t_len=0;
strcpy(ans,"\0");
for(int i=1;i<=le;i++)
{
for(int j=0;j<=le-i;j++)
{
for(k=j,l=0;k<i+j;k++,l++)
str[l]=s[0][k];
str[l]='\0';
get_next(str,i);
//for(int m=0;m<i;m++)
//printf("%d ",next[m]);
//system("pause");
for(h=1;h<n;h++)
if(!find(str)) break;
if(h==n)
{
if(t_len<i)
{
t_len=i;
strcpy(ans,str);
}
else if(t_len==i)
{
if(strcmp(ans,str)>0)
{
strcpy(ans,str);
}
}
}
//else
//printf("No\n");
}
}
if(ans[0]!='\0')
printf("%s\n",ans);
else
printf("IDENTITY LOST\n");
}
}