Corporate Identity
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total submissions: 7268 |
|
accepted: 2546 |
Description
Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but AL So is 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 don't want to change their existing logos and trademarks completely, because their customers are used to the old one S. Therefore, ACM would only change existing trademarks instead of creating new ones.
After several other proposals, it is decided to take all existing trademarks and find the longest common sequence of Lett ERS is contained in all of them. This sequence is graphically emphasized to form a new logo. Then, the old trademarks may still is 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'll be composed only from lowercase letters, the length of each trademark'll be in least 1 and at most C Haracters.
After the last trademark, the next task begins. The last task was 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 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
The longest and least dictionary-ordered substring in the given string (Strstr can be easily water. Write KMP is purely my personal Shi)
#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--)//substring length {for (int i=0; I<=strlen (a[0))-flag; i++)//select substring
{ 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;
}