HDU 3247 resource archiver (AC automation + state compression DP)

Source: Internet
Author: User
Resource archiver

Time Limit: 20000/10000 MS (Java/others) memory limit: 100000/100000 K (Java/Others)
Total submission (s): 899 accepted submission (s): 238

Problem descriptiongreat! Your new software is almost finished! The only thing left to do is archiving all your N resource files into a big one.
Wait a minute... You realized that it isn' t as easy as you thought. think about the virus killers. they'll find your software suspicious, if your software contains one of the M predefined virus codes. you absolutely don't want this to happen.
Technically, resource files and virus codes are merely 01 strings. you 've already convinced yourself that none of the resource strings contain a virus code, but if you make the archive arbitrarily, virus codes can still be found somewhere.
Here comes your task (formally): Design A 01 string that contains all your resources (their occurrences can overlap), but none of the virus codes. to make your software smaller in size, the string shocould be as short as possible.

 

Inputthere will be at most 10 test cases, each begins with two integers in a single line: N and M (2 <= n <= 10, 1 <= m <= 1000 ). the next n lines contain the resources, one in each line. the next M lines contain the virus codes, one in each line. the resources and virus codes are all non-empty 01 strings without spaces inside. each resource is at most 1000 characters long. the total length of all virus codes is at most 50000. the input ends with N = m = 0.

 

Outputfor each test case, print the length of shortest string.

 

Sample input2 2 1110 0111 101 1001 0 0

 

Sample output5

 

Source2009 "nit Cup" national invitational contest

 

Recommendwujianhua AC automatic machine second question... Not explained .... Let's get started.
 /* HDU 3247 resource archiver gives you n strings and M virus strings. To concatenate these n Virus strings, they can overlap, but cannot contain any of the M virus strings. Returns the minimum length of N concatenation.  */  # Include <Stdio. h> # Include <Math. h> # Include <Queue> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Const   Int Maxn = 60000 ; //  Maximum number of nodes, 50000 + 1000*10  Const   Int Maxl = ( 1 < 10  );  Const   Int Max = 2  ;  Const   Int Maxm = 11 ; Typedef  Struct  Trie_node {  Bool Virus; //  Virus?      Int  End, fail;  Int  Next [Max];} trie; trie tree [maxn];  Char STR [ 50010  ];  Int  Dis [maxn];  Int Pos [maxm];  Int  G [maxm] [maxm];  Int  DP [maxl] [maxm];  Int  Size, CNT;  Void Insert ( Char * S, Int  ID ){  Int P = 0  ;  Int I = 0 ;  While (S [I]! = '  \ 0  '  ){  If (Tree [p]. Next [s [I]- '  0  ' ] = 0  ) {Tree [ + Size]. End = 0  ; Tree [size]. fail = 0 ; Tree [size]. Virus = False  ;  For ( Int J = 0 ; J <Max; j ++ ) Tree [size]. Next [J] = 0  ; Tree [p]. Next [s [I] - '  0  ' ] = Size;} p = Tree [p]. Next [s [I]-'  0  '  ]; I ++ ;}  If (ID> = 0 ) Tree [p]. End = ( 1 < ID );  Else Tree [p]. Virus = True  ;}  Void  BFS () {queue < Int >Q;  Int  Temp, P; q. Push (  0  );  While (! Q. Empty () {temp = Q. Front (); q. Pop ();  For ( Int I = 0 ; I <Max; I ++ ){  If (Tree [temp]. Next [I]! = 0 ) {P = Tree [temp]. Next [I]; q. Push (P );  If (Temp! = 0 ) Tree [p]. Fail = Tree [tree [temp]. Fail]. Next [I]; tree [p]. End | = Tree [tree [p]. Fail]. end; tree [p]. Virus | = Tree [tree [p]. Fail]. virus ;}  Else Tree [temp]. Next [I] = Tree [tree [temp]. Fail]. Next [I] ;}}  Void PATH (Int  K) {queue < Int > Q; q. Push (Pos [k]); memset (DIS, - 1 , Sizeof  (DIS); DIS [POS [k] = 0  ;  Int  Now, P;  While (! Q. Empty () {now =Q. Front (); q. Pop ();  For ( Int I = 0 ; I <Max; I ++ ) {P = Tree [now]. Next [I];  If (DIS [p] < 0 &&! Tree [p]. Virus) {dis [p] = Dis [now] + 1  ; Q. Push (p );}}}  For (Int I = 0 ; I <CNT; I ++ ) G [k] [I] = Dis [POS [I];} inline  Int Min ( Int X, Int  Y ){  If (X < 0 | Y < 0 ) Return X> Y? X: Y;  Else  Return X> Y? Y: X ;}  Void Doit ( Int  N) {memset (DP, - 1 , Sizeof  (DP); DP [  0 ] [ 0 ] = 0  ;  For ( Int I = 0 ; I <( 1 <N); I ++ )  For ( Int J = 0 ; J <CNT; j ++ ){  If (DP [I] [J] < 0 ) Continue  ;  For ( Int K = 0 ; K <CNT; k ++){  If (G [J] [k] < 0 ) Continue  ;  Int T = I | Tree [POS [k]. end; DP [T] [k] = Min (DP [T] [K], DP [I] [J] + G [J] [k]) ;}}  Int T = ( 1 <N )- 1  ;  Int Ans =-1  ;  For ( Int I = 0 ; I <CNT; I ++ ) Ans = Min (ANS, DP [T] [I]); printf (  "  % D \ n  "  , ANS );}  Int  Main (){  //  Freopen ("in.txt", "r", stdin ); //  Freopen ("out.txt", "W", stdout );      Int  N, m;  While (Scanf ( "  % D  " , & N ,& M )){  If (N = 0 & M = 0 ) Break  ; Tree [  0 ]. End =0  ; Tree [  0 ]. Fail = 0  ; Tree [  0 ]. Virus = False  ;  For ( Int I = 0 ; I <Max; I ++) tree [ 0 ]. Next [I] = 0  ; Size = 0 ;  For ( Int I = 0 ; I <n; I ++ ) {Scanf (  "  % S  " ,& Str); insert (STR, I );}  For ( Int I = 0 ; I <m; I ++ ) {Scanf (  " % S  " ,& Str); insert (STR, - 1  );} BFS (); CNT = 1  ; POS [  0 ] = 0  ;  For ( Int I = 0 ; I <= size; I ++ ) If  (Tree [I]. End) POS [CNT ++] = I;  For ( Int I = 0 ; I <CNT; I ++ ) Path (I); doit (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.