HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper 【DFS+剪枝】
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的最后一位 ,然后再往前找,那么从前往后找也是一样的。就可以用DFS去搜,但是需要剪枝。不然TLE
AC代码
#include <bits/stdc++.h> //DFS+剪枝
using namespace std;
const int maxn = 2 * 1e2 + 5;
string a, b, c;
int len_a, len_b, len_c;
int ans;
int vis[maxn][maxn];
void dfs(int x, int y)
{
if (x + y == len_c)
{
ans = 1;
return ;
}
if (vis[x][y]) //剪枝
return ;
if (a[x] == c[x + y])
{
vis[x][y] = 1;
dfs(x + 1, y);
}
if (b[y] == c[x + y])
{
vis[x][y] = 1;
dfs(x, y + 1);
}
}
int main()
{
int t;
int k;
cin >> t;
for (k = 1; k <= t; k++)
{
cin >> a >> b >> c;
len_a = a.size(), len_b = b.size(), len_c = c.size();
printf("Data set %d: ", k);
ans = 0;
memset(vis, 0, sizeof(vis));
dfs(0, 0);
if (ans)
cout << "yes\n";
else
cout << "no\n";
}
}
HDU 1501 Zipper 【DFS+剪枝】的更多相关文章
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- hdu - 1072(dfs剪枝或bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...
- HDU 1175 连连看 (DFS+剪枝)
<题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1501 Zipper 动态规划经典
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
随机推荐
- Java 还是 C++?
Java 特别象 C++:由此很自然地会得出一个结论:C++似乎会被Java 取代.但我对这个逻辑存有一些疑问. 无论如何,C++仍有一些特性是Java 没有的.而且尽管已有大量保证,声称Java有一 ...
- mybatis 入门学习
所须要jar包:mybatis-3.x.x.jar .假设须要和spring整合,还须要增加相关的包 1:看项目文件夹 红颜色不要关心 2:依照步骤: 1:增加jar包 2:创建数据源(configu ...
- Python学习-一个简单的计时器
在实际开发中,往往想要计算一段代码执行多长时间,以下我将该功能写入到一个函数里面,仅仅要在每一个函数前面调用该函数就可以,见以下代码: #------------------------------- ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)4.3——排除任务
问题: 你想要在构建进程当中排除指定的任务. 解决方案: 可以使用-x排除单个任务.编辑task graph排除多个任务. 讨论: Gradle的构建进程包含了许多任务的执行顺序.他们中的大多数都依赖 ...
- c++ new(不断跟新)
1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...
- FZU1465
题目链接:传送门 题目大意:给你n个整数(可正可负),求有多少个连续的子序列的和==m(时限1S) 题目思路:前缀和+手写hash(map效率太慢,会超时) 具体做法是用一个数组sum,数组的第i位保 ...
- MVC 发布程序 HTTP 错误 403.14 - Forbidden 及 HTTP 错误 404.2 - Not Found
新建立的MVC项目发布程序后会浏览网站可能会有问题 这时不要去按照系统提示打开“目录浏览”,而是应该去做一些配置 具体步骤: 1:配置web.Config <system.webServer&g ...
- 爬虫入门【9】Python链接Excel操作详解-openpyxl库
Openpyx是一个用于读写Excel2010各种xlsx/xlsm/xltx/xltm文件的python库. 现在大多数用的都是office2010了,如果之前之前版本的可以使用xlrd读,xlwt ...
- hdu 1513 Palindrome【LCS滚动数组】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1513 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Spoken English Practice(Look, That cute guy is checking me out. come on, give me a break, he's just looking around.)
绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 下划线_为浊化 口语蜕变(2017/7/6) 英 ...