3450

/*
KMP来做532ms
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <strstream>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)

#define FF(i,a)    for(int i=0;i<(a);i+++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)
#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}


// code begin
#define MAXN 4001
#define MAXM 202

char dc[MAXN][MAXM];
int Len[MAXN];
int N;
int mins;
int minst;

char P[MAXM];
int Ps;
int Ts;
int next[MAXM];

char ans[MAXM];

void CalNext()
{
	next[0] = 0;
	next[1] = 0;
	int j;
	FORi(2,Ps+1,1)
	{
		j = next[i-1];
		while(true)
		{
			if(P[j]==P[i-1])
			{
				next[i] = j+1;
				break;
			}
			if(j==0)
			{
				next[i] = 0;
				break;
			}
			j = next[j];
		}
	}
}

bool KMP(int cur)
{
	Ts = Len[cur];
	int i=0;
	int j=0;
	while(true)
	{
		if(j==Ts) break;

		if(P[i]==dc[cur][j])
		{
			i++;
			j++;
			if(i==Ps)
			{
				return true;
			}
		}
		else if(i>0)
		{
			i=next[i];
		}
		else
			j++;
	}
	return false;
}

bool Isok()
{
	FORi(0,N,1)
	{
		if(i!=mins)
		{
			if(!KMP(i))
			{
				return false;
			}
		}
	}
	return true;
}

int main()
{
	read;
	write;
	while(scanf("%d",&N)!=-1)
	{
		if(N==0) break;
		minst = MAXM;
		FORi(0,N,1)
		{
			scanf("%s",dc[i]);
			Len[i] = strlen(dc[i]);
			if(Len[i]<minst)
			{
				minst = Len[i];
				mins = i;
			}
		}
		
		//找到了最短的子串 mins;
		for(int i=0;i<MAXM;i++)
			ans[i] = 'z';
		ans[MAXM] = 0;
		bool f=false;
		for(int len=Len[mins];len>0;len--)
		{
			Ps = len;
			for(int i=0;i+len<=Len[mins];i++)
			{
				strncpy(P,dc[mins]+i,len);
				P[len] = 0;
				CalNext();
				
				//然后和其余的串比较
				if(Isok())
				{
					if(strcmp(P,ans)<0)
					{
						strcpy(ans,P);
						f=true;
					}
				}
			}
			if(f) break;
		}
		if(f) printf("%s\n",ans);
		else printf("IDENTITY LOST\n");
	}
	return 0;
}

posted @ 2011-03-25 14:16  AC2012  阅读(146)  评论(0编辑  收藏  举报