HDU-2328 modify ate identity

Source: Internet
Author: User

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 with 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: Find the longest Public String Of all strings. Train of Thought: enumerate the strings of the first string, and then match and search
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 4010; const int n = 210; int N, next [maxn]; char words [maxn] [N]; int CMP (const void * str1, const void * str2) {return strlen (char *) str1) -strlen (char *) str2);} void getnext (char * Str) {int M = strlen (STR); next [0] = next [1] = 0; for (INT I = 1; I <m; I ++) {Int J = next [I]; while (J & STR [I]! = STR [J]) J = next [J]; next [I + 1] = STR [I] = STR [J]? J + 1: 0 ;}} int KMP (char * Text, char * Str) {getnext (STR); int n = strlen (text ); int M = strlen (STR); Int J = 0; For (INT I = 0; I <n; I ++) {While (J & text [I]! = STR [J]) J = next [J]; If (STR [J] = text [I]) J ++; If (j = m) return 1;} return 0;} int main () {While (scanf ("% d", & N )! = EOF & N) {for (INT I = 0; I <n; I ++) scanf ("% s", words [I]); qsort (words, n, sizeof (words [0]), CMP); char ans [maxn], TMP [maxn]; int Len = strlen (words [0]); int max = 0; for (INT I = 0; I <Len; I ++) {memset (TMP, '\ 0', sizeof (TMP); int Pos = 0; for (Int J = I; j <Len; j ++) {TMP [POS ++] = words [0] [J]; getnext (TMP ); int flag = 1; for (int K = 1; k <n; k ++) if (KMP (words [K], TMP) = 0) {flag = 0; break;} If (flag & max <POS) {max = Pos; memcpy (ANS, TMP, sizeof (TMP ));} else if (flag & max = POS & strcmp (ANS, TMP)> 0) memcpy (ANS, TMP, sizeof (TMP) ;}} if (max) printf ("% s \ n", ANS); else printf ("identity lost \ n");} return 0 ;}


HDU-2328 modify ate identity

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.