POJ 2192 Zipper (DP)

Source: Internet
Author: User

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)

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.