poj3450

Corporate Identity

http://poj.org/problem?id=3450

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

题意

求多个字符串的最大公共子串。

题解

求多个字符串的最大公共子串,我们将第一个字符串和去其他字符串匹配出最大公共子串,匹配的方法是将第一个字符串的每一位都作为子串起点来查找子串的终点,通过kmp可以将时间压缩到94ms, 有个大佬的79ms。代码如下。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<cstdlib>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
int next[205];
char str[4005][205];
char res[205];
int n;

void GetNext(char *s,int len)//普通的kmp
{
	memset(next,0,sizeof(next));
	next[0]=-1;
	int i=0,j=-1;
	while(i<len)
	{
		if (j==-1||s[i]==s[j]) next[++i]=++j;
		else j=next[j];
	}
}

int GetLongestStr(char *s,int len)
{
	GetNext(s,len);
	for(int i=1;i<n;i++)//与每一个字符串比较,求最大公共子串
	{
		char *p=str[i];
		int j=0,tmp=0;
		while(*p&&j<len)//j<len保证是公共的最大字串
		{
			if(j==-1||*p==s[j]) {
				p++;
				j++;
				tmp=max(tmp,j);
			}else {
				j=next[j];
			}
		}
		len=tmp;
	}
	return len;
}
/*
为什么GetLongestStr这样就是最大的公共字串呢?
前面说了字串起点固定求终点,也就是说这里str与s比较每一次都从s[0]开始,s[0]就是固定的起点,然后查找终点,配合j<len就可以得到公共的最大的字串。
这样只需要枚举第一个字串长度就可以了。
*/
int main(void)
{
	while(~scanf("%d",&n)&&n)
	{
		getchar();
		for(int i=0;i<n;i++)
			scanf("%s",str[i]);
		int ans=0,len=strlen(str[0]),pos=0;
		for(int i=0;i<len;i++)//求以第i位为起点的最大公共子串 
		{
			if(ans>len-i) break;//理论上是可以减少用时的,但实际上并没有 
			int tmp=GetLongestStr(str[0]+i,len-i);
			if(tmp>=ans){
				if(tmp>ans){
					ans=tmp;
					pos=i;
				}else {
					bool smaller=true;
					for(int j=0;j<ans;j++)
					{
						if(str[0][pos+j]>str[0][i+j]) break; //这里为什么能这样比较呢?
						else if(str[0][pos+j]<str[0][i+j]) //因为起点是固定从i开始的,
						 { //所以这里就可以用上一次的起点pos和长度ans与这一次的起点i和tmp比较
							smaller=false;
							break;
						}
					}
					if(smaller) pos=i;
				}
			}
		}
		if(ans)
			for(int i=0;i<ans;i++)
				printf("%c",str[0][i+pos]);
		else
			printf("IDENTITY LOST");
		printf("\n");
	}
    return 0;
}

萌新一个,如有错误,欢迎指正。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值