POJ 3450 Corporate Identity to find the longest common substring of all characters

Source: Internet
Author: User

Description

Beside other services, ACM helps companies-clearly state their ' corporate identity ', which includes company logos but AL So and signs, like trademarks. One of such companies is Internet Building Masters (IBM), which have recently asked ACM for a to help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers is 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 and each containing one trademark. Trademarks'll is composed only from lowercase letters, the length of each trademark would be at 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 is several strings of the same length, print the one and 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

Source

CTU Open 2007

Test instructions is long, in fact, the longest common substring of a set of strings.

The KMP can be used to find a string in an O (N) time with the longest prefix substring of another string in the feature accelerator.

#include <stdio.h> #include <string.h>const int max_n = 4001;const int max_l = 201;char samestr[max_l], Dict[ma  X_n][max_l];int nxtbl[max_l];int len[max_n];template<typename T1, TypeName t2>inline bool Equ (T1 t1, T2 T2) {return (T1) T1 = = (T1) T2; }void getnexttble (int *t, char *s, int len) {t[0] = 0;for (int i = 1, j = 0; i < Len;) J Record the subscript of the last good prefix {if (s[i] = = S[j]) t[i++] = ++j;//++j equals how many good prefixes else if (J > 0) j = t[j-1];//get subscript of next contrasting character else t[i++] = 0; }}int Getlongestpre (Char *chs, char *s, int len1, int len2) {int Len3 = 0;int i = 0, j = 0;for (; i < len2 && J < Len1; {if (equ (S[i], chs[j])) {i++, j++;if (J > Len3) Len3 = j;} else if (J > 0) j = nxtbl[j-1];else i++;} return LEN3;} BOOL Smaller (char *s1, char *s2, int L)//lexicographically smaller{for (int i = 0; i < L; i++) {if (S1[i] < s2[i]) re Turn True;else if (S1[i] > S2[i]) return false;} return false;} int main () {int n;while (~scanf ("%d\n", &n) && N) {for (int i = 0; i < n; i+ +) {gets (Dict[i]); Len[i] = strlen (Dict[i]);} Char *p = Dict[0];char *pres = Null;int L = 0;for (; len[0]; len[0]--, p++) {getnexttble (NXTBL, p, len[0]); int tmp = len[0] ; for (int i = 1; i < N && tmp; i++) {tmp = Getlongestpre (P, Dict[i], TMP, Len[i]);} if (tmp > L | | equ (TMP, L) && smaller (p, pRes, L)) {L = Tmp;pres = P;}} if (L) {for (int i = 0; i < L; i++) Putchar (pres[i]);p utchar (' \ n ');} Else puts ("IDENTITY LOST");} return 0;}




POJ 3450 Corporate Identity to find the longest common substring of all characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.