(KMP violence) Corporate Identity--hdu--2328

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=2328Corporate Identity

Time limit:9000/3000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 698 Accepted Submission (s): 281


Problem Descriptionbeside Other services, ACM helps companies-clearly state their "corporate identity", which includes Company logos but also and other 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.

Inputthe 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.

Outputfor each task, output A, 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 input3aabbaabbabbababbbbbbbabb2xyzabc0

Sample outputabbidentity LOST is actually a violent solution to the problem, to my degree can only be violent, but I also want to write a better method Ah, next time must learn can pit, I did not give me the string of strings assigned to the initial value, resulting in my use of strcmp always wrong , I wrote a judge function to compare strings, can be the same reason, and then WA, fortunately, with the help of teammates found, the initial value of this problem do not know how many times, but I also jump to the inside, after more attention, some of the wrong can not find out, to the initial value to think about, In addition, we should pay attention to the initial

Code:

#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;#defineM 40005#defineN 210CharS[m][n];intNext[n];voidFindNext (Charb[]) {    intI=0, j=-1, blen=strlen (b); next[0] = -1;  while(i<Len) {        if(j==-1|| b[i]==B[j]) next[++i] = + +J; ElseJ=Next[j]; }}intKMP (CharA[],Charb[]) {    intI=0, j=0; intAlen=strlen (a), blen=strlen (b);    FindNext (b);  while(i<Alen) {         while(j==-1|| (A[i]==b[j] && i<alen && j<Blen)) I+ +, J + +; if(j==Blen)return 1; J=Next[j]; }    return 0;}intMain () {intN;  while(SCANF ("%d", &N), N) {intI, J, K, minlen= +, Len; CharSs[n]; memset (s),0,sizeof(s));  for(i=0; i<n; i++) {scanf ("%s", S[i]); Len=strlen (S[i]); if(len<Minlen) {Minlen=Len; memset (SS,0,sizeof(ss));            strcpy (SS, S[i]); }        }        Charb[n]="{"; intindex=0;  for(I=minlen; i>0; i--)        {             for(j=0; j<=minlen-i; J + +)            {                CharA[n]; Memset (A,0,sizeof(a)); strncpy (A, SS+J, I);  for(k=0; k<n; k++)                {                    if(KMP (S[k], a) = =0)                         Break; }                if(K==n && strcmp (A, B) <0) {Index=i;                strcpy (b, a); }                if(Index && j==minlen-i) I=-1, j= +; }        }        if(Index) printf ("%s\n", B); Elseprintf ("IDENTITY lost\n"); }    return 0;}
View Code

(KMP violence) Corporate Identity--hdu--2328

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.