hdu2328(KMP+枚举)

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 186    Accepted Submission(s): 89


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
 

Recommend
teddy
 
本题要求多个字符串的最长公共子串。
求最长公共子串是个常见的算法。但本题所求的是多个字符串的,排除常见的两个字符串的DP做法。
可以用后缀数组,但我刚刚接触后缀数组,不能灵活运用。
尝试用KMP+枚举,不料没有超时。
找到最短的字符串,枚举该字符串的子串,然后分别与其他字符串做KMP,找到长度最长且在长度相同的情况下字典序最小的公共子串。
 
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN=200+10;
char str[4000+10][MAXN];
int next[MAXN];

//KMP算法中计算next[]数组
void getNext(char *p)
{
    int j,k,len=strlen(p);
    j=0;
    k=-1;
    next[0]=-1;
    while(j<len)
    {
        if(k==-1||p[j]==p[k])
        {
            next[++j]=++k;
        }
        else k=next[k];
    }
}


//返回模式串T在主串S中首次出现的位置
//返回的位置是从0开始的,若没有找到返回-1。
int KMP_Index(char *W,char *T)
{
    int i=0, j=0,wlen=strlen(W),tlen=strlen(T);
    getNext(W);
    while(i<tlen&&j<wlen)
    {
        if(j==-1||T[i]==W[j])
        {
            i++; j++;
        }
        else
            j=next[j];
    }
    if(j==wlen)
        return i-wlen;
    else
        return -1;
}


int main()
{
	int n,i,j,k,Minlen,Minid;
	char tmp[MAXN],ans[MAXN];
	bool flag;
	while(~scanf("%d",&n),n)
	{
		scanf("%s",str[0]);
		Minlen=strlen(str[0]);
		flag=false;
		for(i=1;i<n;i++)
		{
			scanf("%s",str[i]);
			Minid=0;
			if(strlen(str[i])<Minlen)
			{
				Minid=i;Minlen=strlen(str[i]);
			}
		}
	//	printf("str[Minid]=%s\n",str[Minid]);

		ans[0]=0;
		for(i=Minlen;i>=0;i--)
		{
			for(j=0;j<=Minlen-i;j++)
			{
				strncpy(tmp,str[Minid]+j,i);
				tmp[i]=0;
			//	printf("tmp=%s\n",tmp);
				
				for(k=0;k<n;k++)
				{
					if(k!=Minid)
					{
						if(KMP_Index(tmp,str[k])==-1)
							break;
					}
				}
				
				if(k==n)
				{
					if(strlen(ans)<i)
						strcpy(ans,tmp);
					else if(strlen(ans)==i&&strcmp(ans,tmp)>0)
						strcpy(ans,tmp);
					flag=true;
				}
			}
			//if(flag)break;
		}
	if(ans[0]==0)
		printf("IDENTITY LOST\n");
	else printf("%s\n",ans);
	}
	return  0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值