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
题意:给n个字符串,找到这n个字符串中共同的子串,如果有输出长度最大的,如果长度相同输出字典序最小的,如果找不到输出IDENTITY LOST
下面会给出两个代码,第一种的方法是找到一个字符串,然后用找到的第一个字符串再和下面的字符串进行比较
第二种不知道咋来的,是一个大神的代码,第一种的运行时间大约300ms而第二种的速度是43s(可怕)
第一种
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[4005][205],sd[205],ans[205],as[205];
int ne[205];
void getnext(int len)
{
int i=0,j=-1;
ne[0]=-1;
while(i<len)
{
if(j==-1||sd[i]==sd[j])
ne[++i]=++j;
else
j=ne[j];
}
}
int kmp(int x)
{
int len=strlen(s[x]),i=0,j=0,sum=0;
while(i<len)
{
if(j==-1||sd[j]==s[x][i])
{
i++,j++;
sum=max(j,sum);
}
else
j=ne[j];
}
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int b=0;
for(int i=0;i<n;i++)
scanf("%s",s[i]);
int len1=strlen(s[0]);
for(int i=0;i<len1;i++)
{
int maxx=0x3f3f3f3f;
strncpy(sd,s[0]+i,len1-i);//从字符串s的第i位后复制len1-i个字符到sd中
sd[len1-i]='\0';
getnext(strlen(sd));
for(int j=1;j<n;j++)
{
int a=kmp(j);
if(maxx>a)
{
maxx=a;
strncpy(ans,sd,a);
ans[a]='\0';
}
}
if(b<maxx)
{
b=maxx;
strcpy(as,ans);
}
else if(b==maxx)
{
if(strcmp(as,ans)>0)
strcpy(as,ans);
}
}
if(b)
printf("%s\n",as);
else
printf("IDENTITY LOST\n");
}
}
第二种
#include <stdio.h>
#include <string.h>
#include<algorithm>
using namespace std;
const int maxx = 4001;
const int Max = 201;
char res[Max], s[maxx][Max];
int ne[Max],n;
void getNext(char *chs, int len)
{
memset(ne, 0, sizeof(ne));
for (int i = 1, j = 0; i < len; )
{
if (chs[i] == chs[j]) ne[i++] = ++j;
else if (j > 0) j = ne[j-1];
else i++;
}
}
int getLogestPre(char *chs, int len)
{
getNext(chs, len);
for (int i = 1; i < n; i++)
{
char *p = s[i];
int j = 0, tmp = 0;
for (; *p && j < len; )
{
if (*p == chs[j])
{
p++, j++;
tmp = max(tmp, j);
}
else if (j > 0) j = ne[j-1];
else p++;
}
len = tmp;
}
return len;
}
int main()
{
while (~scanf("%d", &n) && n)
{
for (int i = 0; i < n; i++)
{
scanf("%s",s[i]);
}
int len = strlen(s[0]), ans = 0, pos = 0;
for (int i = 0; i < len; i++)
{
int tmp = getLogestPre(s[0]+i, len-i);
if (tmp >= ans)
{
if (tmp > ans)
{
ans = tmp;
pos = i;
}
else
{
bool smaller = true;
for (int t = 0; t < ans; t++)
{
if (s[0][pos+t] > s[0][i+t]) break;
else if (s[0][pos+t] < s[0][i+t])
{
smaller = false;
break;
}
}
if (smaller) pos = i;
}
}
}
if (ans)
{
for (int i = 0; i < ans; i++)
{
printf("%c",s[0][pos+i]);
}
printf("\n");
}
else
printf("IDENTITY LOST\n");
}
return 0;
}