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+剪枝】的更多相关文章

  1. hdu 1501 Zipper dfs

    题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...

  2. (step4.3.5)hdu 1501(Zipper——DFS)

    题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...

  3. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  4. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  5. hdu 1501 Zipper

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...

  6. hdu - 1072(dfs剪枝或bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1072 思路:深搜每一个节点,并且进行剪枝,记录每一步上一次的s1,s2:如果之前走过的时间小于这一次, ...

  7. HDU 1175 连连看 (DFS+剪枝)

    <题目链接> 题目大意:在一个棋盘上给定一个起点和终点,判断这两点是否能通过连线连起来,规定这个连线不能穿过其它的棋子,并且连线转弯不能超过2次. 解题分析:就是DFS从起点开始搜索,只不 ...

  8. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. 【实验二】Spring框架笔记——NamedParameterJdbcTemplate与具名参数

    在经典的 JDBC 用法中, SQL 参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定. 在 Spring JDBC 框架中, 绑定 ...

  2. 第二百一十五节,jQuery EasyUI,DateBox(日期输入框)组件

    jQuery EasyUI,DateBox(日期输入框)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 DateBox(日期输入框)组件的使 ...

  3. 框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口

    框架一般用作Java应用程序的窗口,而Applet是Java小程序的窗口. 与Frame不同,Applet是在网页中显示的,也可以通过添加Panel进行组件布局. package TomAwt; im ...

  4. 【翻译自mos文章】使用asm来部署 超大数据库(10TB到PB 范围)--针对oracle 10G

    使用asm来部署 超大数据库(10TB到PB 范围) 參考原文: Deployment of very large databases (10TB to PB range) with Automati ...

  5. js计算百分比

    function GetPercent(num, total) { /// <summary> /// 求百分比 /// </summary> /// <param na ...

  6. windows 下安装securecrt 绿色版

  7. Volley使用详细说明

    前一篇粗略的介绍了一下Volley,并在最后附上了一段使用代码,这一篇详细的介绍一下Volley的使用.前面也说了Volley主要获取JSON对象和图片加载,这里也分为两部分介绍. 1.获取JSON对 ...

  8. [MongoDB]学习笔记--User管理

    1. 创建一个超级用户 use admin db.createUser( { user: "adminUserName", pwd: "userPassword" ...

  9. Less-mixin判断(守卫)一

    mixin卫士--判断 类似于JavaScript的if/else example: .test(@a) when (@a>10){//当大于10 font-size:18px; } .test ...

  10. 常用的mysql语句

    为了方便学习mysql,把接触到的sql收集一下,忘记的时候可以查询一下. 连接mysql数据库: mysql -u 用户名 -p 输入密码. 创建数据库: create database 数据库名; ...