hdu 2328 Corporate Identity
2016-01-26 12:57
295 查看
输出最长公共子串
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.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#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;
}
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.net/yskyskyer123 * @email: 2530094312@qq.com
*===========================================*/
#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;
}
相关文章推荐
- 验证邮箱的正则
- bat 自动执行 SVN 命令
- 动态更新约束问题(SnapKit 的使用)
- AngularJS系列:4、HTML编译器
- for,foreach,iterator的用法和区别
- Spring中PropertyPlaceholderConfigurer的使用
- How does a relational database work
- Java基础知识点2:hashCode()方法
- AFNetworking判断网络状态
- 高性能计算知识汇总
- CodeForces 617A Elephant
- exchange 2010连接器解释
- [Latex] natbib package to cite references
- HVR安装配置步骤
- MaterialRefreshLayout实现下拉刷新上拉加载更多
- 理解SVN中trunk,branches,tags
- canvas绘制简单图形
- hdu 2502 月之数
- Android实现定时执行某个任务
- 基于nginx tomcat redis分布式web应用的session共享配置