博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1501 Zipper【DP】
阅读量:6141 次
发布时间:2019-06-21

本文共 2423 字,大约阅读时间需要 8 分钟。

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
3cat tree tcraetecat tree catrteecat tree cttaree
 

 

Sample Output
Data set 1: yesData set 2: yesData 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;

}

转载于:https://www.cnblogs.com/Hilda/archive/2012/07/31/2617259.html

你可能感兴趣的文章
Silverlight 如何手动打包xap
查看>>
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
阿里云企业邮箱 在Foxmail 7.0上POP3/IMAP协议设置方法
查看>>
Javascript一些小细节
查看>>
canvas学习总结
查看>>
Javascript的if判断
查看>>
spring cloud gateway 源码解析(3)记录请求参数及返回的json
查看>>
阿里云ECS数据盘格式化与挂载图文教程
查看>>
Flexbox响应式网页布局 - W3Schools视频02
查看>>
【手牵手】搭建前端组件库(二)
查看>>
怎么给视频添加音频或配乐
查看>>