HDOJ 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

题意

给出三串字符串 a, b, c 判断 字符串C 中的每个字符 是不是分别从a, b 字符串中 顺序取出的。也就是说 从c字符串中 按顺序 能不能取出 a ,b 字符串 可以不连续 但顺序不能变

思路

我们可以从最后一个字符来判断 ,若要满足题目要求,字符串c的最后一个字符肯定是字符串a或者字符串b中的最后一个 如果满足 就往前推 如果不满足 就break 掉

所以 其实是一个动态规划的过程 我们从前往后看 也是一样的

我们用dp[i][j]来标记,i 代表取a串的前i个字符, j 代表取 b串的前j 个字符

我们可以发现 dp[i][j] 的状态 其实是由 dp[i - 1][j] || dp[i][j - 1] 转移过来的

如果 dp[i - 1][j] 的状态是 1 的话 那么 只需要满足 a[i - 1] == c[i + j - 1] 为什么要减1 是因为 字符串从0开始计数

如果满足 那么 dp[i][j] 的状态也是1

同理 或者满足 dp[i][j - 1] 的状态是1 并且满足 b[j - 1] == c[i + j - 1] 那么 dp[i][j] 的状态是1

上面两种条件 只需要满足一个 就可以转移过来了

否则 dp[i][j] 的状态就是0

AC代码

#include <bits/stdc++.h>   //简单DP
using namespace std;
const int maxn = 2 * 1e2 + 5;
int dp[maxn][maxn];
int main()
{
int t;
cin >> t;
int i, j, k;
for (k = 1; k <= t; k++)
{
string s[3];
int len[3];
for (i = 0; i < 3; i++)
{
cin >> s[i];
len[i] = s[i].size();
}
memset(dp, 0, sizeof(dp));
for (i = 0; i < len[0]; i++)
{
if (s[0][i] == s[2][i])
dp[i + 1][0] = 1;
else //如果不BREAK 存在BUG eg: ca t dat 输出 yes
break;
}
for (i = 0; i < len[1]; i++)
{
if (s[1][i] == s[2][i])
dp[0][i + 1] = 1;
else
break;
}
for (i = 1; i <= len[0]; i++)
{
for (j = 1; j <= len[1]; j++)
{
if (dp[i - 1][j] && s[0][i - 1] == s[2][i + j - 1])
dp[i][j] = 1;
if (dp[i][j - 1] && s[1][j - 1] == s[2][i + j - 1])
dp[i][j] = 1;
}
}
printf("Data set %d: ", k);
if (dp[len[0]][len[1]])
cout << "yes\n";
else
cout << "no\n";
}
}

HDOJ 1501 Zipper 【简单DP】的更多相关文章

  1. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  2. hdu1501 Zipper[简单DP]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...

  3. hdu 1501 Zipper(DP)

    题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...

  4. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  6. codeforces Gym 100500H A. Potion of Immortality 简单DP

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  7. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  8. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  9. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

随机推荐

  1. TP 框架 ajax[利用异步提交表单]

    //[] $(function () { $("#send-btn" ).click(function (){ //接受表单的值 var username=$('input[nam ...

  2. 网络协议之rtp---rtp 传输视频及加密

    http://read.pudn.com/downloads170/sourcecode/windows/788977/es%20ParkertTS/ESToTS.cpp__.htm http://w ...

  3. 【BZOJ】3479: [Usaco2014 Mar]Watering the Fields(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3479 这个还用说吗.... #include <cstdio> #include < ...

  4. 一起talk C栗子吧(第一百二十七回:C语言实例--查看main函数的參数)

    各位看官们,大家好,上一回中咱们说的是static关键字的样例,这一回咱们说的样例是:查看main函数的參数.闲话休提,言归正转.让我们一起talk C栗子吧! 看官们.我们在第五十七回中介绍过mai ...

  5. Plist文件介绍

    开发IOS遇到数据,这里专门做frame sprite数据说明 plist plist是property list的缩写.plist中包括一些命名值和使用Core  Foundation类型创建的值的 ...

  6. 160805、oracle查询:取出每组中的第一条记录

    在Java 9发布之前,我们来分享一些Java 8开发技巧 [以下为译文] 在使用JAVA 8进行开发多年后,结合个人使用IntelliJ IDEA的心得,我总结了以下几个JAVA8技巧供大家参考. ...

  7. Python全栈day26-27(面向对象进阶)

    参考 http://www.cnblogs.com/linhaifeng/articles/6204014.html 1,什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访 ...

  8. mysql出现) Notice: Trying to get property of non-object in E:\p错误的 原因

    在mysql中,每个命令之间都要留一点空格 如果是这样, $query = "select * from books where " .$searchtype. "lik ...

  9. CCCallFunc, CCCallFuncN, CCCallFuncND 三者的区别

    今天学习过程中,自己敲了一个例子,结果在执行Action的时候出现了错误.经排查发现是CCCallFunc使用的问题,应该使用CCCallFuncN,然后搜了下他们的区别,才知道,是因为有一个参数的问 ...

  10. 解决ubuntu 无法挂载移动硬盘问题 unknown filesystem type 'exfat'

    Ubuntu 13.10 或以上 安装exfat-fuse: sudo apt-get install exfat-fuse Ubuntu 13.04 或以下 sudo apt-add-reposit ...