admin管理员组文章数量:1615702
输出最长公共子串
Corporate Identity
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 795 Accepted Submission(s): 318
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.
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
Source CTU Open Contest 2007
Recommend teddy | We have carefully selected several similar problems for you: 2324 2326 2327 2329 2331
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source£ºhdu 2328 Corporate Identity
* @type: data_structrue KMP
* @author: wust_ysk
* @blog: http://blog.csdn/yskyskyer123
* @email: 2530094312@qq
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 4000 ;
//const int maxV=12 ;
string N[maxn+5];
string M;
string S;
int nex[200+5];
int len[maxn+5];
int len_S;
int len_M,n;
int dp[maxn][200+5];
void getnex()
{
nex[0]=nex[1]=0;
for(int i=1;i<len_M;i++)
{
int p=nex[i];
while(p&&M[p]!=M[i]) p=nex[p];
nex[i+1]= M[p]==M[i]? p+1:0;
}
}
void KMP(int ind,int delta)
{
int p=0;
for(int i=0;i<len[ind];i++)
{
while(p&&M[p]!=N[ind][i]) p=nex[p];
if(M[p]==N[ind][i])
{
dp[ind][delta+p]=max(dp[ind][delta+p],p+1 );
p++;
}
if(p==len_M) p=nex[p];
}
}
void work()
{
memset(dp,0,sizeof dp);
for(int st=0;st<len_S;st++)
{
M=S.substr(st,len_S);
len_M=len_S-st;
getnex();
for(int i=1;i<=n;i++)
{
KMP( i,st );
}
}
int maxi=0;string ans="";
memset(dp[0],0x3f,sizeof dp[0]);
for(int j=0;j<len_S;j++)
{
for(int i=1;i<=n;i++)
{
dp[0][j]=min(dp[0][j],dp[i][j] );
}
if(dp[0][j]>maxi)
{
maxi=dp[0][j];
ans="";
for(int k=j-maxi+1;k<=j;k++)
{
ans+=S[k];
}
}
else if(dp[0][j]==maxi)
{
string t="";
for(int k=j-maxi+1;k<=j;k++)
{
t+=S[k];
}
ans=min(ans,t);
}
}
if(ans=="") puts("IDENTITY LOST");
else cout<<ans<<endl;
}
int main()
{
while(cin>>n&&n)
{
cin>>S;
len_S=S.length();
n--;
for(int i=1;i<=n;i++)
{
cin>>N[i];
len[i]=N[i].length();
}
work();
}
return 0;
}
版权声明:本文标题:hdu 2328Corporate Identity 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1728724606a1170592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论