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". 

InputThe 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.

OutputFor 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,判断a和b按照顺序拼接是否能构成c 具体思路见代码注释,实在是感觉深搜一层层递归回去不好解释
 #include<stdio.h>
#include<iostream>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std; char a[],b[],c[];//注意一下c的长度开的大小得是ab之和
int flag,la,lb,lc;
bool book[][];//记得要标记,否则不知道是否用过,会造成重复使用 void dfs(int x,int y,int z)////传入下标
{
if(z==lc)//长度找到之后
{
flag=;
return;
}
if(flag)//已经找到了//该字符已经用过了
return;
if(book[x][y]==)//该字符已经用过了
return;
book[x][y]=;
if(x<la&&a[x]==c[z])//a的第x个字符跟c串的第z个字符相等
dfs(x+,y,z+);//加1就说明是按照顺序来的
if(y<lb&&b[y]==c[z])//b的第y个字符跟c串的第z个字符相等
dfs(x,y+,z+);
} int main()
{
int tt,t=;
scanf("%d",&tt);
while(tt--)
{
memset(book,,sizeof(book));
// memset(a,'\0',sizeof(a));
// memset(b,'\0',sizeof(b));
// memset(c,'\0',sizeof(c));
scanf("%s %s %s",a,b,c);
la=strlen(a);
lb=strlen(b);
lc=strlen(c);
flag=;
dfs(,,);
if(flag)
printf("Data set %d: yes\n",t++);
else
printf("Data set %d: no\n",t++);
}
return ;
}

HDU1501-Zipper-字符串的dfs的更多相关文章

  1. HDU1501 Zipper(DFS) 2016-07-24 15:04 65人阅读 评论(0) 收藏

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

  2. HDU 1501 Zipper(DP,DFS)

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

  3. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. HDU 1501 Zipper 字符串

    题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...

  5. hdu1501 Zipper[简单DP]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...

  6. 洛谷P1019:单词接龙(DFS)

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...

  7. FJUT2017寒假训练二题解

    A题 题意:让你找出唯一的一个四位数,满足对话时的要求. 思路:因为是4位数,可以直接从1000-9999遍历一遍,判断是否有唯一的数能满足所有条件,如果不是唯一的或者没有满足条件的数就输出Not s ...

  8. BZOJ4912 : [Sdoi2017]天才黑客

    建立新图,原图中每条边在新图中是点,点权为$w_i$,边权为两个字符串的LCP. 对字典树进行DFS,将每个点周围一圈边对应的字符串按DFS序从小到大排序. 根据后缀数组利用height数组求LCP的 ...

  9. Safecracker-HDU1015

    题意 给你大写字母的字符串,A=1,...Z=26,以及target 问你是否有v - w^2 + x^3 - y^4 + z^5 = target 有输出字典序大的那个字符串 分析 dfs code ...

  10. [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...

随机推荐

  1. 【LeetCode 1】两数之和

    描述 [题解] 用个map的话就是O(N)级别的了. [代码] class Solution { public: unordered_map<int,int> mymap; vector& ...

  2. 安全检测及分析神器—AppScan使用教程

    最近项目准备验收,所以最近在做项目验收的准备工作:我们公司规定,项目的安全检测必须通过才能进行项目验收:公司的安全部门用的检测软件就是大名鼎鼎的IBM Rational Appscan;在教由安全部门 ...

  3. php开发面试题---php面向对象详解(对象的主要三个特性)

    php开发面试题---php面向对象详解(对象的主要三个特性) 一.总结 一句话总结: 对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为. 对象的形态:当施加那些方法是对象如何响应,颜色,尺寸 ...

  4. 2.4 webpack + gulp 构建完整前端工作流

    在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp clean gu ...

  5. 7. Jmeter-逻辑控制器介绍与使用

    逻辑控制器介绍与使用 如果(if)控制器 事物控制器 循环控制器 while controller critical section controller foreach控制器 include con ...

  6. 13、testng.xml对用例进行分组

    目录如下: TestGroup.java 代码如下: package com.testng.cn; import org.testng.annotations.*; import static org ...

  7. redis-trib.rb创建集群失败

    yum安装ruby: yum install -y rubyyum install -y rubygems //安装rubygemgem install redis //安装redis的接口包gem ...

  8. iptables默认规则

    iptables默认规则 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [34:4104] -A INPUT -m ...

  9. 一键获取Android的appActvity和PackName

    大家平常写Appium自动化时,可能写脚本半小时,得有5分钟用来去看Activity,大部分都是通过adb命令的方式来获取.为了提高效率,可以把这个命令放到python里去执行,然后根据规则去筛选出自 ...

  10. 四.python注释说明

    Python第四节 Python注释 注释说明 注释分为单行注释和多行注释 单行注释以#开头 # 注释示例 > print("上面是一个注释的示例") 多行注释 多行注释可以 ...