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. CI框架整合微信公共平台接口

    #CI框架控制器 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /*** CI框架整合微信 ...

  2. java算法-数学之美一

    巧用数学的思想来解决程序算法问题,这样的代码如诗般优美.通过数学思想来看问题,也能将程序简单化.“斐波那契数列”对于java程序员来说一定不陌生.当然这个问题的解决方案也有很多.用一个例子说明数学思想 ...

  3. js 代码优化 (写的可以)

    Javascript是一门非常灵活的语言,我们可以随心所欲的书写各种风格的代码,不同风格的代码也必然也会导致执行效率的差异,开发过程中零零散散地接触到许多提高代码性能的方法,整理一下平时比较常见并且容 ...

  4. vim 命令大全(写的非常全和好)http://blog.csdn.net/woshixiaosimao/article/details/54312738

    下文转自链接.由原作者陈皓译自<Learn ViM Progressively>,非常不错. ——————————正文开始—————————— 你想以最快的速度学习人类史上最好的文本编辑器 ...

  5. 用Java自定义一个定时器

    1.先定义一个监听类: import java.util.Date; import java.util.Timer; import javax.servlet.ServletContextEvent; ...

  6. $().each() 与 $.each()区别,以及 jquery ajax 应用

    在jquery 中我们可以选择$().each() 与 $.each() 进行迭代对象和数组 $(items).each(function(){ //item })   , 而后者则 $.each(i ...

  7. 项目分析(GS,NET,NGP关系)

    看了两天,这三者之间的关系好像是这样的,因为GS和net在同一台机器上,所以用共享内存通信,毕竟共享内存通信是最快的进程间通信的方式,而NGP是属于客户端的,Net是属于服务器的,他与Net进程是基于 ...

  8. Codeforces Round #372 (Div. 1) B. Complete The Graph

    题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S ...

  9. PMPBOK 进度管理

    项目进度计划提供详尽的计划,说明项目如何以及何时交付项目范围中定义的产品.服务和成功,是一种用于沟通和管理相关方期望的工具,为绩效报告提供了依据. 进度计划方法:关键路径发或敏捷方法.创建项目经度模型 ...

  10. Apache-ActiveMQ transport XmlMessage

    前言 本文原创,转载请说明出处! Apache-ActiveMQ:是Apache提供的一款开源的消息传送服务,不论是安装还是使用都非常简单实用,可以作为消息缓存管理器来使用(个人观点),看看Activ ...