Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5707
Test instructions
Give you three strings S1, S2, S3, let you judge whether S3 is exactly composed of string S1 and S2, S1 as S3 substring, S2 also for S3 substring, can be discontinuous.
Ideas:
Set DP[I][J] Indicates whether the first i + J bit of the string S3 can be composed of the first I bit of the string S1 and the first J bit of the string S2. DP[I][J] = 1 means yes, dp[i][j] = 0 means no.
DP[I][J] can be transferred by DP[I-1][J] and dp[i][j-1]. If DP[I][J] is transferred from Dp[i-1][j, then it should be satisfied:
DP[I-1][J] is a legal state (that is, dp[i-1][j] = 1) and the first character of the S1 must be equal to the first i + J character of S3.
If DP[I][J] is transferred by Dp[i][j-1], then it should be satisfied:
DP[I][J-1] is a legal state (that is, dp[i][j-1] = 1) and the J-character of S2 must be equal to the first i + J character of S3.
The state transfer equation can be obtained in the synthesis:
i = = 0 && J = = 0:dp[i][j] = 1; Obviously an empty string can be made up of two empty strings, which is a legitimate state. In addition to the other States, the initialization should be 0.
I! = 0 && J = = 0:dp[i][j] = dp[i-1][j] && s1[i-1] = = S3[i + j-1]//string subscript starting from zero
i = = 0 && J! = 0:dp[i][j] = dp[i][j-1] && s2[j-1] = = S3[i + j-1]
I! = 0 && J! = 0:dp[i][j] = ( dp[i-1][j] && s1[i-1] = = S3[i + j-1]) | | ( dp[i][j] = dp[i][j-1] && s2[j-1] = s3[i + j-1] )//both satisfy one
In addition, the topic also requires S3 must be composed of S1 and S2, in addition to meet dp[Len (S1) [Len (S2)] = = 1, len (S1) + len (S2) = = Len (S3) should also be established.
Code:
1#include <iostream>2#include <cstring>3 4 using namespacestd;5typedefLong LongLL;6 Const intMAXN = -;7 intDP[MAXN +3][MAXN +3];8 9 intMain () {TenIos_base::sync_with_stdio (0); Cin.tie (0); One stringS1, S2, S3; A while(Cin >> s1 >> S2 >>S3) { -dp[0][0] =1;//Dp[0][0] is a legal state -UnsignedintI, J; the for(i =0; I <= s1.length (); i++) { - for(j =0; J <= S2.length (); J + +) { - if(i && j) Dp[i][j] =0;//initialization of other states other than dp[0][0] - if(i) dp[i][j] = dp[i-1][J] & (S1[i-1] = = S3[i + J-1]); //Because it is not 0 or 1, it can be bitwise and + if(j) Dp[i][j] |=dp[i][j-1] & (S2[j-1] = = S3[i + J-1]); //"|=" is due to the fact that two transfer states satisfy one - } + } Acout << (Dp[i-1][j-1] && i + J-2= = S3.length ())?"Yes":"No") <<Endl; at } - return 0; -}
HDU 5707 Combine String (Dynamic planning)