您的位置:首页 > 其它

poj Corporate Identity 3450 (KMP&&枚举) 好题

2015-10-25 10:51 211 查看
Corporate Identity

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 5804 Accepted: 2108
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

//题意:

给你n个字符串让你找出他们中的最长公共子串。如果最长公共子串有多个,则输出字典序小的那个子串。

//看了大神的代码一早上还是有点迷糊,先贴上代码。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char res[210];
char dict[4010][210];
int p[210];
int n;
void getp(char s[],int l)
{
memset(p,0,sizeof(p));
for(int i=1,j=0;i<l;)
{
if(s[i]==s[j])
{
j++;
p[i]=j;
i++;
}
else if(j>0)
j=p[j-1];
else
i++;
}
}
int kmp(char s[],int l)
{
int i,j;
getp(s,l);
for(i=1;i<n;i++)
{
char *q=dict[i];
int j=0;
int tmp=0;
for(;*q&&j<l;)
{
if(*q==s[j])
{
q++;
j++;
tmp=max(tmp,j);
}
else if(j>0)
j=p[j-1];
else
q++;
}
l=tmp;
}
return l;
}
int main()
{
while(scanf("%d",&n),n)
{
getchar();
for(int i=0;i<n;i++)
gets(dict[i]);
int l=strlen(dict[0]);
int ans=0;
int pos=0;
for(int i=0;i<l;i++)
{
int tmp=kmp(dict[0]+i,l-i);
if(tmp>=ans)
{
if(tmp>ans)
{
ans=tmp;
pos=i;
}
else
{
bool sma=true;
for(int t=0;t<ans;t++)
{
if(dict[0][pos+t]>dict[0][i+t])
break;
else if(dict[0][pos+t]<dict[0][i+t])
{
sma=false;
break;
}
}
if(sma)
pos=i;
}
}
}
if(ans)
{
for(int i=0;i<ans;i++)
printf("%c",dict[0][pos+i]);
printf("\n");
}
else
printf("IDENTITY LOST\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: