Poj 3450 modify ate identity (brute force enumeration + KMP)

Source: Internet
Author: User
Tags first string
Corporate Identity
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:4152   Accepted:1569

Description

Beside other services, ACM helps companies to clearly state their "login ate identity", which has des 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
Their new identity. IBM do not want to change their existing logos and trademarks completely, because their MERs 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
Question:Evaluate the longest consecutive Public String of N strings, and output the identity lost if it does not exist. I remember that I used DP to write such a question. Solution:Enumerate the length of the first string from large to small to find the next array, and then match all the strings later with the first one. The question requires that the minimum length of the Lexicographic Order be the longest.
Address: Modify ate identity
Finally, question a has been entangled for a day or two. ACCode:
# Include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; int next [205], N; char A [4005] [205]; int lena0; // length of the mode string // my idea is to use the first string as the mode string, then, calculate the next matching void getnext (int sta, int Len) {int I, j; Len = sta + Len; // The relative position is changed to the absolute position: Next [sta] = STA, next [sta + 1] = sta; for (I = sta + 1; I <Len; I ++) {J = next [I]; while (J! = Sta & A [0] [I]! = A [0] [J]) J = next [J]; if (a [0] [I] = A [0] [J]) next [I + 1] = J + 1; else next [I + 1] = sta ;}} int KMP (int sta, int Len) {int I, J, K; len = sta + Len; For (k = 1; k <n; k ++) // The substring following the substring matches with the substring of the first substring {int flag = 0; j = sta; for (I = 0; I <strlen (A [k]); I ++) {While (J! = Sta & A [k] [I]! = A [0] [J]) J = next [J]; if (a [k] [I] = A [0] [J]) J ++; if (j = Len) {flag = 1; break ;}} if (flag = 0) // if there is no match, 0 return 0;} is returned ;} return 1;} int main () {int I, j, k; while (scanf ("% d", & N) {If (n = 0) break; for (I = 0; I <n; I ++) scanf ("% s", a [I]); lena0 = strlen (A [0]); int flag = 0; // determine whether char res [205] = ""; for (I = lena0; I> = 1; I --) exists --) // from the length lena0 start to enumerate down {int flag1 = 0; // whether the length of I exists for (j = 0; j <= lena0-i; j ++) // start position of mode string decomposition {getnext (J, I); If (KMP (J, I) {If (strlen (RES) = 0 | strncmp (Res, a [0] + J, I)> 0) {// If the matching string is null or the lexicographically ordered string is left behind, the strncpy (Res, A [0] + J, I); Res [I] = '\ 0';} flag1 = 1 ;}} if (flag1) // If the length is I {printf ("% s \ n", Res); flag = 1; break ;}} if (flag = 0) printf ("identity lost \ n");} return 0 ;}

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.