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;

    }

  • 相关阅读:
    使用ADO.NET2.0提升数据交互性能 DataSet 数据表
    AD域控制器所有使用的端口明细列表
    链接数据库 远程事务的处理方式
    根据权限动态生成菜单栏和工具栏
    FTP服务器配置(cmd中ftp命令)
    该操作未能执行,因为 OLE DB 提供程序SQLOLEDB无法启动分布式事务
    ChartLet GDI+中发生一般性错误
    SQL SERVER 2000用户sa 登录失败的解决办法
    .net实例:网站发布后,在IIS中浏览提示:无法找到该页...404错误。
    Winform 关闭应用程序
  • 原文地址:https://www.cnblogs.com/Hilda/p/2617259.html
Copyright © 2011-2022 走看看