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. Using a long as ArrayList index in java

    http://stackoverflow.com/questions/459643/using-a-long-as-arraylist-index-in-java http://bbs.csdn.ne ...

  2. HTML表单页面的运用

    本章目标:掌握表单基本结构<form> 掌握各种表单元素 能理解post和get两种提交方式的区别 本章重点:掌握各种表单元素 本章难点:post和get两种提交方式的区别 一.    H ...

  3. js阻止事件冒泡和标签默认行为

    ////阻止事件冒泡函数和 // 阻止默认浏览器动作(W3C) 要一起使用效果好<a href="/Scripts/newfiber_js_lib/images/1.jpg" ...

  4. 将Centos的yum源更换为国内的阿里云(163)源

    阿里云是最近新出的一个镜像源.得益于阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ CentOS系统更换软 ...

  5. 转(解决GLIBC_2.x找不到的编译问题)

    Linux/CentOS 升级C基本运行库CLIBC的注意事项(当想解决GLIBC_2.x找不到的编译问题) 分类: 开发环境 Linux2014-09-24 10:32 8933人阅读 评论(5)  ...

  6. linux时间格式化

    echo `date +'[%Y-%m-%d %H:%M:%S]'`

  7. 170221、浅谈mysql的SQL的四种连接

    例子:   -------------------------------------------------  a表     id   name     b表     id   job   pare ...

  8. JDK源码分析之concurrent包(四) -- CyclicBarrier与CountDownLatch

    上一篇我们主要通过ExecutorCompletionService与FutureTask类的源码,对Future模型体系的原理做了了解,本篇开始解读concurrent包中的工具类的源码.首先来看两 ...

  9. Create a Group Policy Central Store

    一.How to create a Group Policy Central Store You have downloaded or created your own Group Policy Ad ...

  10. Numba makes Python code fast

    Numba: A High Performance Python Compiler http://numba.pydata.org/ 一行代码让python的运行速度提高100倍,你信吗?-聚能聊-云 ...