题目链接:HDU2328
Corporate Identity
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 968 Accepted Submission(s): 386
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
题目分析:枚举出第一个串的所有子串,然后挨个匹配。找到相同大小的子串就比较两串的大小,返回小串。
不过写完查题解貌似可以用后缀数组做,先留个坑等学完后缀数组再来写一遍。
//
// main.cpp
// HDU2328(1
//
// Created by teddywang on 16/5/16.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[5000][210],t[210],p[210],maxs[210];
int nexts[300],n;
void getnext(char *t)
{
int i=0,j=-1;
nexts[0]=-1;
int len=strlen(t);
while(i<len)
{
if(j==-1||t[i]==t[j])
{
if(t[++i]==t[++j])
nexts[i]=nexts[j];
else nexts[i]=j;
}
else j=nexts[j];
}
}
int kmp(char *s,char *t)
{
int i=0,j=0;
int lens=strlen(s),lent=strlen(t);
while(i<lens&&j<lent)
{
if(j==-1||s[i]==t[j])
{
++i;++j;
if(j==lent) return 1;
}
else j=nexts[j];
}
return 0;
}
int main()
{
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
scanf("%s",s[i]);
int len=strlen(s[0]);
int maxn=0;
for(int i=0;i<len;i++)
{
int k=0;
for(int j=i;j<len;j++)
{
t[k++]=s[0][j];
t[k]='\0';
getnext(t);
int flag=0;
for(int l=1;l<n;l++)
{
memset(p,0,sizeof(p));
strcpy(p,s[l]);
if(kmp(p,t)==0)
{
flag=1;
break;
}
}
if(flag==1)
break;
else if(k>maxn)
{
strcpy(maxs,t);
maxn=k;
}
else if(k==maxn)
{
for(int l=0;l<maxn;l++)
{
if(t[l]<maxs[l])
{
strcpy(maxs,t);
break;
}
else if(t[l]>maxs[l])
break;
}
}
}
}
if(maxn) printf("%s\n",maxs);
else printf("IDENTITY LOST\n");
}
}