HDOJ 1501 Zipper 【简单DP】
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】的更多相关文章
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdu1501 Zipper[简单DP]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...
- hdu 1501 Zipper(DP)
题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- 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 ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
随机推荐
- jQuery常用技巧
1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用 ...
- 第一百六十节,封装库--JavaScript,ajax注册表单到数据库
封装库--JavaScript,ajax注册表单到数据库 效果图 前台js var biaodan = $().xu_lie_biao_dan($('form').sh_jd()); //序列化获取表 ...
- WinCC7.3 Win764位系统安装教程
WinCC7.3 Win764位安装教程 (1)将ISO文件解压缩. (2)编辑Setup.ini文件 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...
- vi 的使用,很详细
来源:鸟哥的Linux私房菜 网址:http://vbird.dic.ksu.edu.tw/linux_basic/0310vi_2.php
- lstrcpyn
看代码时遇到的一些不会用的函数,记录下来. 1. lstrcpyn LPTSTR lstrcpyn( __out LPTSTR lpString1, __in LPCTSTR lpString2, _ ...
- (转)Unity笔记之编辑器(CurveField、DoubleField、EnumMaskField、EnumPopup) ... ...
1. CurveField创建的是一个类型为AnimationCurve的曲线变量,看代码: [code]csharpcode: using UnityEngine; using System.Col ...
- Xcode The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
使用Xcode SVN 出现问题 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 解决方法: 打开终端 然后输 ...
- java各个版本垃圾收集器?
1.7G1
- 面试题思考:IOC的优缺点
先讲重点 面试时怎么答: 先把IOC的概念说出来 依赖注入和控制反转 所谓的依赖注入是甲方开放接口,在它需要的时候,能够将乙方传递进来(注入):所谓的控制反转,甲乙双方不相互依赖,交易活动的进行不依 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)5.2——使用Android Testing Support Library进行测试
问题: 你想要测试app的Android组件. 解决方案: 使用新的测试类实现JUnit风格的测试. 讨论: 测试像activities,services等的Android组件,需要将app部署到连接 ...