POJ

来源:互联网 发布:饥荒联机版网络红色 编辑:程序博客网 时间:2024/06/05 19:41
Corporate Identity
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 7268 Accepted: 2546

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

3aabbaabbabbababbbbbbbabb2xyzabc0

Sample Output

abbIDENTITY LOST
求所给字串中 最长且字典序最小的 子串(strstr可以轻松水过。。写KMP纯属我个人石乐志)
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;#define min(a,b) (a>b?b:a)char a[4008][208],b[208];int kmp_next[208];void KMP_next(){    int i=0,j=-1;    kmp_next[0]=-1;    while(b[i])        if(j==-1||b[i]==b[j])            kmp_next[++i]=++j;        else            j=kmp_next[j];}bool KMP(int k){    int i=0,j=0,m=strlen(b);    while(a[k][i])    {        if(j==-1||a[k][i]==b[j])            ++i,++j;        else            j=kmp_next[j];        if(j==m)            return true;    }    return false;}int main(){    int n;    while(cin>>n&&n!=0)    {        int vju=1,flag=200000;        for(int i=0; i<n; i++)        {            scanf("%s",a[i]);            flag=min(flag,strlen(a[i]));        }        char ans[208]= {""};        for(; flag&&vju; flag--) //子串长度        {            for(int i=0; i<=strlen(a[0])-flag; i++) //选取子串            {                strcpy(b,a[0]+i);                b[flag]=0;                KMP_next();                int j;                for(j=1; j<n; j++)                    if(!KMP(j))                        break;                if(j==n)                {                    if(vju||strcmp(b,ans)<0)                        strcpy(ans,b);                    vju=0;                }            }            if(!vju)                break;        }        if(strlen(ans))            cout<<ans<<endl;        else            cout<<"IDENTITY LOST"<<endl;    }    return 0;}


原创粉丝点击