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. Jetty修改默认端口

    1.webserver: Jetty2.version:   7.6.5, 8.1.53.operation: 修改默认端口3.1 修改Jetty目录下的/etc/jetty.xml 文件中的[por ...

  2. spark(1.1) mllib 源码分析(三)-朴素贝叶斯

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/4042467.html 本文主要以mllib 1.1版本为基础,分析朴素贝叶斯的基本原理与源码 一.基本原 ...

  3. VC++ 给你的代码强制加一个硬断点

    类似与Javascript的 debugger; Hard code a debugger breakpoint If you need to insert a hard breakpoint in ...

  4. Laravel5.1 分页展示

    Laravel为我们提供了一套分页的逻辑,我们无需自己实现分页逻辑,只需要执行几个简单的方法就能实现漂亮的分页. 1 simplePaginate 这是一种只显示上一页下一页的样式分页,我们来看看怎么 ...

  5. Oracle起步---创建临时表空间/表空间/创建用户/授权

    1. 安装: 百度一下你就知道 2. sqlplus登录/sqlplus命令登录 在安装Oracle时,你需要记住设置的“全局数据库名”(默认为orcl) 和 口令,在以两种方式登录时: 用户名: s ...

  6. Python中xlrd和xlwt模块使用方法 (python对excel文件的操作)

    本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 安装xlrd和xlwt模块 xlrd和xlwt模块不是 ...

  7. 《转》最受欢迎的ASP.NET的CMS下载

    1. Umbraco 项目地址 | 下载 Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使用mssql进行存储数据. 使用Umbraco ,设计师能创造出有效的XHTML标 ...

  8. 每一行最后添加文字python脚本

    比较简单的在pycharm上跑的脚本 #_*_coding:utf-8_*_ #普通版 file = open("oldfile.txt","r",newlin ...

  9. 1.1_php基础语法

    一,变量与常量: 二,php中的运算符(字符串拼接): 三,php数组. <!DOCTYPE html> <html> <head> <meta charse ...

  10. ZOJ 2676 Network Wars[01分数规划]

    ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special J ...