Hdu_4295
When F [I] [J] [k] is used to represent the I-th character of S, the set of the selected string is K (in binary format ), the length of J is overwritten from the current character.
Here we will only discuss the minimum overwrite length, which is similar to the maximum overwrite length. For the convenience of DP, we use the table flushing method. First, set f [I] [0] [0] to 0, and then for any f [I] [J] [K], there are actually two kinds of decisions, one is that the current position does not put anything over to the next character, then there is f [I + 1] [J-1] [k] = STD:: min (F [I + 1] [J-1] [K], F [I] [J] [k]), another is to put the string t from the current position, the length of the string T may affect the status J, that is, the length that has been overwritten from the current character, if the status of J after the string T is set to NJ, and the value of F is NF, then f [I] [NJ] [k | 1 <t] = STD :: min (F [I] [NJ] [k | 1 <t], NF ).
To make it easy to know whether the current position can contain a string T, you need to pre-process the string A, B, C, and D from each position of S, here, we can also use KMP for brute force attacks. The complexity of this Part has little impact on the overall complexity.
# Include <stdio. h> # Include <String . H> # Include <Algorithm> # Define # Maxl 4110 # Define INF 0x3f3f3f # Define INF 0xc3c3c3c3 Char S [maxl], B [ 4 ] [ 70 ]; Int N, P [ 70 ], H [ 4 ] [Maxl], Len [ 4 ], F [maxl] [70 ] [ 16 ]; Void KMP ( Int K ){ Int I, J; P [ 1 ] = 0 ; For (I = 2 , J = 0 ; I <= Len [k]; I ++ ){ While (J & B [k] [J + 1 ]! = B [k] [I]) J = P [J]; If (B [k] [J + 1 ] = B [k] [I]) ++ J; P [I] = J;} memset (H [K], 0 , Sizeof (H [k] [ 0 ]) * (N + 1 )); For (I =1 , J = 0 ; I <= N; I ++ ){ While (J & B [k] [J + 1 ]! = S [I]) J = P [J]; If (B [k] [J + 1 ] = S [I]) ++ J; If (J = Len [k]) H [k] [I-len [k] + 1 ] = 1 , J = P [J] ;}} Void Init (){ Int I, J, K; n = Strlen (S + 1 ); For (I = 0 ; I < 4 ; I ++ ) Scanf ( " % S " , B [I] + 1 ), Len [I] = strlen (B [I] + 1 ), KMP (I );} Void DP ( Int C, Const Int & (& Fun )( Const Int &, Const Int & )){ Int I, J, K, NJ, NF, ANS = C? INF: INF; memset (F, c ? 0x3f : 0xc3 , Sizeof (F [ 0 ]) * (N + 1 )); For (I = 1 ; I <= N; I ++ ) {F [I] [ 0 ] [ 0 ] = 0 ; For (J = 0 ; J <= 64 ; J ++){ For (K = 0 ; K < 15 ; K ++ ) If (F [I] [J] [k]! = (C? INF: INF )){ Int Nj = j> 0 ? J- 1 : 0 ; F [I + 1 ] [NJ] [k] = fun (F [I + 1 ] [NJ] [K], F [I] [J] [k]); For ( Int T = 0 ; T < 4 ; T ++ ) If (K & 1 <T) = 0 && H [T] [I]) {NJ =STD: max (J, Len [T]); F [I] [NJ] [K | 1 <T] = fun (F [I] [NJ] [k | 1 <T], F [I] [J] [k] + NJ- J) ;}} ans = Fun (ANS, F [I] [J] [ 15 ]) ;}} Printf ( " % D " , ANS );} Void Solve () {dp ( 1 , STD: min), printf ( " " ), DP ( 0 , STD: max), printf ( " \ N " );} Int Main (){ While (Scanf ( " % S " , S + 1 ) = 1 ) {Init (); solve ();} Return 0 ;}