Corporate Identity

来源:互联网 发布:怎么签署淘宝空间协议 编辑:程序博客网 时间:2024/06/05 22:36

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
 

这个问题就是叫你求出最长的公共字符串,如果求出两个字符串长度长度相等,则按照字典序排序,输出字典序较小的字服串


主要做法就是枚举出第一串字符串所有的子字符串,然后kmp匹配后面的每一串字符串,就可以找到串最长的公共字符串,如果遇到两串字符串长度相等

那么就字典序排序,把字典序较小的留下来


代码如下

#include <cstdio>#include <cstdlib>#include<cstring>#include <algorithm>using namespace std;char str [20000][20000];int next1[20000];char temp[20000];char sum[20000];void getnext(char *s1){    int j=0,k=-1;    int len=strlen(s1);    next1[0]=-1;    while(j<len){        if(k==-1||s1[j]==s1[k]){            ++j;            ++k;            if(s1[j]!=s1[k]) next1[j]=k;            else next1[j]=next1 [k];        }        else k=next1[k];    }    return ;}bool kmp(char *s1,char *s2){    int len1=strlen(s2);    int len2=strlen(s1);    getnext(s1);    int i=0,j=0;    while(i<len1){        if(j==-1||s1[j]==s2[i]){            ++i;            ++j;        }        else j=next1[j];        if(j==len2)        return true;    }    return false;}int main(){    int n;    int i,j,k;    while(scanf("%d",&n)==1&&n){        for( i=0;i<n;i++)            scanf("%s",str[i]);            int len=strlen(str[0]);            memset(sum,'\0',sizeof(sum));            for(i=0;i<len;i++){                int ans=0;                for(j=i;j<len;j++){                    temp[ans]=str[0][j];                    ans++;                    temp[ans]='\0';//注意这里一定要加上,否则出现越界等情况                    int flag=1;                    for(k=1;k<n;k++){                        if(!kmp(temp,str[k])){                            flag=0;                            break;                        }                    }                    if(flag){                        if(strlen(temp)>strlen(sum)){                            strcpy(sum,temp);                        }                        else if(strlen(temp)==strlen(sum)&&strcmp(temp,sum)<0)                            strcpy(sum,temp);                    }                }            }            if(strlen(sum)>0) printf("%s\n",sum);            else printf("IDENTITY LOST\n");    }    return 0;}


0 0
原创粉丝点击