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. 敏捷在《PMBOK指南》知识领域中的应用

    <PMOBOK指南>知识领域 敏捷工作过程中的应用 第四章 项目整合管理 迭代和敏捷方法能够促进团队成员以相关领域专家的身份参与整合管理.团队成员自行决定计划及其组件的整合方式.在适应型环 ...

  2. python pip时openssl的错误

    也不知道看了哪个方法弄成这个样子的,也没办法,下面方法可用 https://blog.csdn.net/chr1341901410/article/details/80995451

  3. Dubbo入门到精通学习笔记(十七):FastDFS集群的安装、FastDFS集群的配置

    文章目录 FastDFS集群的安装 FastDFS 介绍(参考:http://www.oschina.net/p/fastdfs) FastDFS 上传文件交互过程: FastDFS 下载文件交互过程 ...

  4. python3 可变数据类型和不可变数据类型

    python内置有6种对象类型: Number 数值型 int 整型 不可变 float 浮点型 不可变 complex 复数 不可变 String 字符串   不可变 Tuple 元组   不可变 ...

  5. python数据结构之快速排序

    def quick_sort(nums): if not nums: return [] else: # 这里取第0个数为基点 flag = nums[0] # 小于flag 的放到左边 left = ...

  6. js 点击获取验证码后的倒数60s

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script ...

  7. 线性回归——Python代码实现

    import numpy as np def computer_error_for_give_point(w, b, points): # 计算出 观测值与计算值 之间的误差, 并累加,最后返回 平均 ...

  8. 关于VS的第一次使用

    参考链接:https://blog.csdn.net/qq_36556893/article/details/88605617

  9. 一键获取Android的appActvity和PackName

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

  10. 【转】从SOA到微服务,企业分布式应用架构在云原生时代如何重塑

    摘要: SOA 采用中心化的服务总线架构,解耦了业务逻辑和服务治理逻辑:微服务架构回归了去中心化的点对点调用方式,在提升敏捷性和可伸缩性的同时,也牺牲了业务逻辑和服务治理逻辑解耦所带来的灵活性. 为了 ...