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. 工程师技术(二):postfix基础邮件服务、postfix空客户端邮件服务、搭建mariadb数据库系统、配置一个数据库、使用数据库查询

    一.postfix基础邮件服务 目标: 本例要求在虚拟机server0上配置 postfix 基础服务,具体要求如下: 1> 监听本机的所有接口    2> 将邮件域和邮件服务主机名都改为 ...

  2. go语言中使用正则表达式

    一.代码 package main import ( "fmt" "regexp" ) func main() { text := `Hello 世界!123 ...

  3. C#网页数据采集(三)HttpWebRequest

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  4. (12)centos7 环境变量配置

    export 一个变量的设置一般只在当前环境有效,export命令可以用于传递一个或多个变量的值到任何后续脚本.export可新增.修改或删除环境变量,供后续执行的程序使用.export的效力限于该次 ...

  5. sql server 2008 数据库数据类型

    sql server 2008 数据库数据类型 一.数值型 int:整数类型,它的精度由执行机构确定.. smallint:短整数类型,它的精度由执行机构确定.. numeric(p,s):数值型,并 ...

  6. git分布式版本控制系统权威指南学习笔记(一):配置文件、简单流程和小问题

    文章目录 git配置文件简介 git config各种命令 配置级别: 用户信息 文本编辑器 差异分析工具 配置命令别名 公钥 git协同流程 简单流程 初始化版本库 提交至缓存区 查看状态 提交分支 ...

  7. \t \r \n转义字符

    t \r \n都是转义字符,空格就是单纯的空格,输入时可以输入空格 \t 的意思是 横向跳到下一制表符位置 \r 的意思是 回车 \n 的意思是回车换行 所有的转义字符和所对应的意义: 转义字符 意义 ...

  8. 剑指offer——56在排序数组中查找数字

    题目描述 统计一个数字在排序数组中出现的次数.   题解: 使用二分法找到数k然后向前找到第一个k,向后找到最后一个k,即可知道有几个k了 但一旦n个数都是k时,这个方法跟从头遍历没区别,都是O(N) ...

  9. 学习 Doug Lea 大神写的——Scalable IO in Java

    学习 Doug Lea 大神写的--Scalable IO in Java 网络服务 Web services.分布式对象等等都具有相同的处理结构 Read request Decode reques ...

  10. 2019ICPC南昌网络赛C Hello 2019

    题意:给出一个字符串,每次询问一个区间[l,r],求使得这个区间含有9102但不含有8102最少要删掉几个字符 首先我们考虑将串反转,这样就变成了含有2019但不含有2018的问题了 我们构建一个状态 ...