您的位置:首页 > 其它

poj3450之kmp入门

2013-07-08 21:06 465 查看
Corporate Identity

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 4087Accepted: 1547
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


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=4000+10;
char s[MAX][210],temp[210],temp2[210];
int next[210];

void Strcpy(char *a,char *b,int S,int T){
	int k=0;
	for(int i=S;i<=T;++i)a[k++]=b[i];
	a[k]='\0';
}

void get_next(char *a){
	int i=-1,j=0,len=strlen(a);
	next[0]=-1;
	while(j<len){
		if(i == -1 || a[i] == a[j]){
			if(a[++i] == a[++j])next[j]=next[i];
			else next[j]=i;
		}else i=next[i];
	}
	return;
}

bool KMP(char *a,char *b){
	int lena=strlen(a),lenb=strlen(b);
	if(lena>lenb)return false;
	get_next(a);
	int i=0,j=0;
	while(i<lena && j<lenb){
		if(i == -1 || a[i] == b[j])++i,++j;
		else i=next[i];
	}
	if(i>=lena)return true;
	return false;
}

int main(){
	int n,k;
	while(cin>>n,n){
		for(int i=0;i<n;++i)cin>>s[i];
		int len=strlen(s[0]),flag=0;
		for(int i=len;i>0;--i){
			for(int j=0;j+i-1<len;++j){//取长度为i的字符串并且从j开始 
				Strcpy(temp,s[0],j,j+i-1);
				for(k=1;k<n;++k){
					if(!KMP(temp,s[k]))break;
				}
				if(k == n){
					if(flag){if(strcmp(temp2,temp)>0)strcpy(temp2,temp);}
					else strcpy(temp2,temp);
					flag=true; 
				}
			}
			if(flag)break;
		}
		if(!flag)cout<<"IDENTITY LOST"<<endl;
		else cout<<temp2<<endl;
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: