"Spoj" Mglar10-growing Strings

Source: Internet
Author: User

Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, asit is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings has the particularity, as they grow, they add characters to the left and/or to theright of themselves, but T Hey never lose characters, nor insert new characters in the middle. Gene and Gina has a collection of photos of some strings at di?erent times during their growth. The problem is and the collection are not annotated, so they forgot to which strings each photobelongs to. They want to put together a wall to illustrate strings growing procedures, but theyneed your help to? nd a appropriate SE Quence of photos. Each photo illustrates a string. The sequence of photos must be such so if Si comes imme-diately before si+1 in the sequence and then si+1 are a string that May has grown from Si (i.e., siappears as a consecutive substring of si+1). Also, they do not want to use repeated Pictures,so All strings in the sequence must is di?erent. Given a set of strings representing all available photos, your job was to calculate the size of thelargest sequence they CA n produce following the guidelines above. Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. strings has the particularity, as they grow, they add characters to the L EFT and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle.   gene and Gina has a collection of photos of some strings at di?erent times during their growth. the problem are That the collection are not annotated, so they forgot to which strings each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they need your help to? nd a APPROPRI Ate sequence of photos. Each photo illustrates aString. The sequence of photos must be such so if Si comes immediately before si+1 in the sequence and then Si+1 was a string that m Ay has grown from Si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures, so all strings in the sequence must is di?erent. Given a set of strings representing all available photos, your job was to calculate the size of the largest sequence T                Hey can produce following the guidelines above. --by Spoj The main idea is that some strings, from which to choose some of the final phase, requires that the next two strings in the string of strings before the substring of the maximum number of string x is the longest string (total string) of the largest scheme, and then enumeration X to max statistics method is: When marking the fail pointer, if x has a is_end tag, Then the scheme of fail (x) and FA (x) is taken to Max and +1 as the X scheme, and if there is no is_end mark on X, there should be no scheme in theory, but it is convenient for recursion orfail (x) vs. FA (x) scheme takes Max (not + 1) as a scheme without affecting the resultCode:
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Chars[1000010];structtrie{intch[ -];} data[1000010];inttot;intis_end[1000010],fail[1000010];intque[10000000];voidWorkint );voidInit ();voidBuildfail ();intMain () {intN;  while(SCANF ("%d", &n) &&N) work (n); return 0;}voidWorkintN) {    inti,j,k,len,ans=0;    Init ();  for(i=1; i<=n;i++) {scanf ("%s", s); Len=strlen (s); k=0;  for(j=0; j<len;j++){            if(!data[k].ch[s[j]-'a']) Data[k].ch[s[j]-'a']=++tot; K=data[k].ch[s[j]-'a']; } Is_end[k]=1;    } buildfail ();  for(i=0; i<=tot;i++)        if(ans<Is_end[i]) ans=Is_end[i]; printf ("%d\n", ans);}voidInit () {memset (fail,0,sizeof(fail)); memset (Is_end,0,sizeof(Is_end)); memset (data,0,sizeof(data)); Tot=0;}voidBuildfail () {intH=0, t=1, I,j,k;  while(h<t) {h++;  for(i=0;i< -; i++)            if(Data[que[h]].ch[i]) {que[++t]=Data[que[h]].ch[i]; J=Fail[que[h]];  while(1)                    if(data[j].ch[i]&&data[j].ch[i]!=Que[t]) {Fail[que[t]]=Data[j].ch[i]; Is_end[que[t]]+=is_end[que[h]]>is_end[fail[que[t]]?Is_end[que[h]]:is_end[fail[que[t]];  Break; }                    Else{                        if(!J) Break; J=Fail[j]; }                if(!fail[que[t]]) is_end[que[t]]+=Is_end[que[h]]; }    }}

P.S., the first time with Spoj, feel good;

"Spoj" Mglar10-growing Strings

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.