Links: http://poj.org/problem?id=2192
Test instructions: is a given three string a,b,c, to determine whether C can be composed of the characters in AB, and this combination of character order must be a, b in the original order, can not reverse, for example: A:MNL,B:XYZ; if C is Mnxylz, it is test instructions; if C is mxnzly, does not conform to test instructions, because Z and y order are not in the order of B.
DP Solver: The definition Dp[i][j] Indicates whether the first I character in A and B before the first J character can compose the first (i+j) characters in C, if so, Mark 1, if not, Mark 0;
With this definition, we can find the state transition equation, initial state dp[0][0] = 1:
if dp[i-1][j] = = 1 && c[i+j-1] = = a[i-1]----> Dp[i][j] = 1
If dp[i][j-1] = = 1 && c[i+j-1] = = b[j-1]----> dp[i][j] = 1
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack > #include <set> #include <cmath> #include <queue> #define MAXN 1010#define Mul (x) ((x) * (x)) #define RST (n) memset (n, 0, sizeof (n)) using namespace Std;int DP[MAXN][MAXN], Cas;char A[MAXN], B[MAXN], C[maxn];int main () {int Kcas = 0; scanf ("%d", &cas); while (cas--) {scanf ("%s%s%s", A, B, C); RST (DP); int La = strlen (A), Lb = strlen (B); Dp[0][0] = 1; for (int i=0, i<=la; i++) {for (int j=0; j<=lb; J + +) {if (i > 0 && dp[i-1][j] = = 1 && c[i+j-1] = = A[i-1]) {dp[i][j] = 1; } if (J > 0 && dp[i][j-1] = = 1 && c[i+j-1] = = B[j-1]) {dp[i][j] = 1; }}} printf ("Data set%d:%s\n", ++kcas, dp[la][lb]? "Yes": "No"); } return 0;}
POJ 2192 Zipper (DP)