zoukankan      html  css  js  c++  java
  • HDU 1501 Zipper【DP】

    Problem Description
    Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

    For example, consider forming "tcraete" from "cat" and "tree":

    String A: cat
    String B: tree
    String C: tcraete


    As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":

    String A: cat
    String B: tree
    String C: catrtee


    Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
     
    Input
    The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

    For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

     
    Output
    For each data set, print:

    Data set n: yes

    if the third string can be formed from the first two, or

    Data set n: no

    if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
     
    Sample Input
    3
    cat tree tcraete
    cat tree catrtee
    cat tree cttaree
     
    Sample Output
    Data set 1: yes
    Data set 2: yes
    Data set 3: no

    思路

    题意是说:判断强两个字符串是否能由第三个拆成。Dp[i][j]的值只有2个,0,(表示串3不能拆成1的i之前2的j之前的串)即判断dp[len(1)][len(2)]是0还是1即可。

    dp[i][j]的值可由两个方向而来,即串3的那个字母与串1相等且dp[i-1][j]==1则dp[i][j]==1;串三的字母与串2相等且dp[i][j-1]==1则dp[i][j]==1

    (解题报告写的不是很好,忏悔一下)

    源码

    #include <iostream>

    #define MAX 210

    using namespace std;

    char a[MAX], b[MAX], c[2*MAX];

    int dp[MAX][MAX];

    int main()

    {

        int i, j, t, cas;

        scanf("%d", &cas);

        for(t = 1; t <= cas; t++)

        {

            scanf("%s%s%s", a, b, c);

            memset(dp, 0, sizeof(dp));

            dp[0][0] = 1;

            int la, lb;

            la = strlen(a);

            lb = strlen(b);

            for(i = 0; i <= la; i++)

                  {

                for(j = 0; j <= lb; j++)

                      {

                    if(i > 0 && a[i-1] == c[i+j-1] && dp[i-1][j])

                        dp[i][j] = 1;

                    if(j > 0 && b[j-1] == c[i+j-1] && dp[i][j-1])

                        dp[i][j] = 1;   

                }

            }

            printf("Data set %d: ", t);

            if(dp[la][lb])

                printf("yes\n");

            else

                printf("no\n");

        }

    return 0;

    }

  • 相关阅读:
    linux中配置celery定时任务
    django2 将request.body 中的json字符串转换成字典
    在postman中各种填写参数的区别
    requests.Request 中参数data与json的区别
    Java中使用OpenSSL生成的RSA公私钥进行数据加解密
    openssl生成RSA密钥证书
    WKWebViewJavascriptBridge
    LeetCode实现 strStr()Swift
    LeetCode删除排序数组中的重复项Swift
    LeetCode合并两个有序链表Swift
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617259.html
Copyright © 2011-2022 走看看