hdu2328 Corporate Identity

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328

题目:

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1599    Accepted Submission(s): 614


Problem 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
 

 

Source
 

 

Recommend
teddy
 

 思路:kmp+暴力枚举

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 const double eps=1e-8;
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 int nt[K];
15 char sa[4005][205],sb[205];
16 void kmp_next(char *T,int *next)
17 {
18     next[0]=0;
19     for(int i=1,j=0,len=strlen(T);i<len;i++)
20     {
21         while(j&&T[i]!=T[j]) j=next[j-1];
22         if(T[i]==T[j])  j++;
23         next[i]=j;
24     }
25 }
26 int kmp(char *S,char *T,int *next)
27 {
28     int ans=0;
29     int ls=strlen(S),lt=strlen(T);
30     for(int i=0,j=0;i<ls;i++)
31     {
32         while(j&&S[i]!=T[j]) j=next[j-1];
33         if(S[i]==T[j])  j++;
34         if(j==lt)   ans++;
35     }
36     return ans;
37 }
38 int cmp(char *sb,int si,int st,int len)
39 {
40     for(int i=0;i<len;i++)
41     if(sb[si+i]<sb[st+i])
42         return -1;
43     else if(sb[si+i]>sb[st+i])
44         return 1;
45     return 0;
46 }
47 int main(void)
48 {
49     int t,n;
50     while(scanf("%d",&n)&&n)
51     {
52         for(int i=1;i<=n;i++)
53             scanf("%s",sa[i]);
54         int len,st,se;
55         len=strlen(sa[1]);
56         st=0,se=-1;
57         for(int i=0;i<len;i++)
58         {
59             for(int j=i;j<len;j++)
60             {
61                 sb[j-i]=sa[1][j],sb[j-i+1]='\0';
62                 int ff=1;
63                 kmp_next(sb,nt);
64                 for(int k=2;k<=n&&ff;k++)
65                 if(!kmp(sa[k],sb,nt))
66                     ff=0;
67                 if(ff&&j-i>se-st)
68                     st=i,se=j;
69                 else if(ff&&j-i==se-st&&cmp(sa[1],i,st,j-i+1)<0)
70                     st=i,se=j;
71             }
72         }
73         if(se-st+1<1)
74             printf("IDENTITY LOST\n");
75         else
76         {
77             for(int i=st;i<=se;i++)
78                 printf("%c",sa[1][i]);
79             printf("\n");
80         }
81 
82     }
83     return 0;
84 }

 

posted @ 2017-04-05 17:45  weeping  阅读(165)  评论(0编辑  收藏  举报