HDU1501-Zipper-字符串的dfs
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的更多相关文章
- 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 ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- hdu1501 Zipper
Zipper Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1501 Zipper 字符串
题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...
- hdu1501 Zipper[简单DP]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...
- 洛谷P1019:单词接龙(DFS)
题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙" ...
- FJUT2017寒假训练二题解
A题 题意:让你找出唯一的一个四位数,满足对话时的要求. 思路:因为是4位数,可以直接从1000-9999遍历一遍,判断是否有唯一的数能满足所有条件,如果不是唯一的或者没有满足条件的数就输出Not s ...
- BZOJ4912 : [Sdoi2017]天才黑客
建立新图,原图中每条边在新图中是点,点权为$w_i$,边权为两个字符串的LCP. 对字典树进行DFS,将每个点周围一圈边对应的字符串按DFS序从小到大排序. 根据后缀数组利用height数组求LCP的 ...
- Safecracker-HDU1015
题意 给你大写字母的字符串,A=1,...Z=26,以及target 问你是否有v - w^2 + x^3 - y^4 + z^5 = target 有输出字典序大的那个字符串 分析 dfs code ...
- [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...
随机推荐
- PHP ftp_chdir() 函数
定义和用法 ftp_chdir() 函数改变 FTP 服务器上的当前目录为指定目录. 如果成功,该函数返回 TRUE.如果失败,则返回 FALSE 和一个警告. 语法 ftp_chdir(ftp_co ...
- bzoj1066题解
[解题思路] 考虑拆点,把每根石柱拆成两个点,具体可以理解为石柱底部和石柱顶部,能爬到石柱顶部的蜥蜴只有有限只,而且蜥蜴只有爬到了石柱顶部才能跳到其他石柱的底部. 这样,考虑如下建图: 将每个有蜥蜴的 ...
- 搭建本地 8.8 W 乌云漏洞库
这个是关于两个图片马的帖子. https://zhuanlan.zhihu.com/p/27486144 这个是开源地址: https://github.com/m0l1ce/wooyunallbug ...
- json格式化在线工具推荐
现在系统对接基本都采用json格式的报文,杂乱无章的json让人看起来头大,这里推荐一款在线格式化json的工具, 工具地址: http://www.matools.com/json 这个在线Json ...
- 51nod-1366 贫富差距——并查集
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1366 #include <iostream> # ...
- 二叉树总结及部分Lintcode题目分析 1
1. 遍历问题 Preorder / Inorder / Postorder preorder: root left right inorder: left root right postorder: ...
- 剑指offer第二版面试题8:用两个栈实现队列(JAVA版)
题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deletedHead,分别完成在队列尾部插入节点和在队列头部删除节点的功能. 分析: 我们通过一个具体的例子来分析 ...
- 条件sql ibatis
<!-- 多条件查询 --><select id="MS-CUSTOM-PANDECT-INFO-BY-CONDITIONS" resultMap="R ...
- java-day21
# DQL:查询语句 1. 排序查询 * 语法:order by 子句 * order by 排序字段1 排序方式1 , 排序字段2 排序方式2... ...
- Selenium3 + Python3自动化测试系列七——多窗口切换
多窗口切换 在页面操作过程中有时候点击某个链接会弹出新的窗口,这时就需要主机切换到新打开的窗口上进行操作. WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切 ...