2782: [HNOI2006] Shortest female string

Source: Internet
Author: User

2782: [HNOI2006] Shortest mother string Time limit:1 Sec Memory limit:128 MB
Submit:3 Solved:2
[Submit] [Status] [Web Board] Description given n strings (S1,s2, ", SN), requires finding a shortest string t, so that n strings (S1,s2,", SN) are substrings of T. The first line of input is a positive integer n (n<=12) that represents the number of strings given. The following n lines, each with a string of all uppercase letters. The length of each string does not exceed 50. Output has only one row, which is the shortest string found for T. In the shortest possible case, if more than one string satisfies the requirement, the first in the dictionary order must be output. Sample Input
Sample Output
HINT Source



First of all, my thinking is very stupid, isf[i][J][k] F[I][J][K] indicates a length ofII, the node on the automaton is JJ, it is impossible to include a substring with a state of KK. But this complexity is directly exploding.

But we can makef [i ][j ]F[i][j] indicates that the node on the automaton is iI, which contains the Shortest length when the status of the substring is J J.

So we turned into the Benquan are 1 of the single source shortest, BFS on the line.

If the BFS is carried out in alphabetical order from small to large, the answer to the smallest dictionary order is obtained.

(a bit of card memory)

#include <iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;Const intinf=1e9;structnode{intfail,ch[ -],val; voidClear () {fail=val=0; memset (CH,0,sizeof(CH)); }}tr[605];intsz;intbin[ -],n;voidInsertChar*s,intval) {    intu=0, len=strlen (s);  for(intI=0; i<len;i++){        intc=s[i]-'A'; if(!Tr[u].ch[c]) {Tr[u].ch[c]=++sz;        Tr[sz].clear (); } u=Tr[u].ch[c]; } tr[u].val|=bin[val];}voidbuild () {Queue<int>q;q.push (0);  while(!Q.empty ()) {        intu=Q.front (); Q.pop ();  for(intI=0;i< -; i++){            if(Tr[u].ch[i]) {intv=Tr[u].ch[i]; if(u) tr[v].fail=Tr[tr[u].fail].ch[i];            Q.push (v); }Elsetr[u].ch[i]=Tr[tr[u].fail].ch[i]; }    }}intfromu[605][4100],froms[605][4100];BOOLvis[605][4100];voidPrintintUints) {    if(!u)return;    Print (Fromu[u][s],froms[u][s]);  for(intI=0;i< -; i++){        intv=tr[fromu[u][s]].ch[i],t=Froms[u][s];  for(intp=v;p;p=tr[p].fail) T|=Tr[p].val; if(v==u&&t==s) {Putchar ('A'+i);  Break; }    }}voidBFs () {Queue<int>q1,q2; Q1.push (0); Q2.push (0); vis[0][0]=1;  while(!Q1.empty ()) {        intU=q1.front (), s=Q2.front (); Q1.pop (); Q2.pop ();  for(intI=0;i< -; i++){            intv=tr[u].ch[i],t=R;  for(intp=v;p;p=tr[p].fail) T|=Tr[p].val; if(!Vis[v][t]) {Vis[v][t]=1;                Q1.push (v); Q2.push (t); Fromu[v][t]=u; Froms[v][t]=s; if(t==bin[n+1]-1) {print (v,t); return; }            }        }    }}Chars[ -];intMain () {scanf ("%d",&N); Tr[sz=0].clear (); bin[1]=1;  for(intI=2;i< -; i++) bin[i]=bin[i-1]<<1;  for(intI=1; i<=n;i++) {scanf ("%s", s);    Insert (s,i);    } build ();    BFS (); return 0;}
View Code

2782: [HNOI2006] Shortest female string

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.