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三个字符串,c的长度是a+b的长度,在a、b原先顺序不变的情况下,问是否能拼出c 思路:原以为是字符串匹配问题,结果是深搜。。
   因为给出的a、b两个字符串的长度刚好等于第三个字符串的长度,所以可以dfs。所以当长度满足或者该字符已经用过就可以进行return。
   搜索传入的是三个字符串的下标,根据下标进行判断。
   最后跳出的条件就是c串的最后的一个字符要么是a串的最后一个字符,要么是b串的最后一个字符。
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; //3
//cat tree tcraete
//cat tree catrtee
//cat tree cttaree //out
//Data set 1: yes
//Data set 2: yes
//Data set 3: no string s1,s2,s3;
int ls1,ls2,ls3;
int flag;
int book[][]; void dfs(int x,int y,int z)//传入下标
{
if(z==ls3)//长度找到之后
{
flag=;
return;
}
if(flag==||book[x][y]==)//已经找到了或者该字符已经用过了
return;
book[x][y]=;
if(x<ls1&&s1[x]==s3[z])//a的第x个字符跟c串的第z个字符相等
dfs(x+,y,z+);//加1就说明是按照顺序来的
if(y<ls2&&s2[y]==s3[z])//b的第y个字符跟c串的第z个字符相等
dfs(x,y+,z+);
return;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin>>n;
int t=;
while(n--)
{
s1.erase();
s2.erase();
s3.erase();
memset(book,,sizeof(book)); cin>>s1>>s2>>s3;
ls1=s1.length();
ls2=s2.length();
ls3=s3.length(); flag=;
dfs(,,); if(flag)
cout<<"Data set "<<dec<<t++<<": yes"<<endl;
else
cout<<"Data set "<<dec<<t++<<": no"<<endl;
}
return ;
}
 

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

  1. HDU 1501 Zipper(DP,DFS)

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

  2. HDU 1501 Zipper 字符串

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

  3. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  4. hdu 1501 Zipper dfs

    题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...

  5. (step4.3.5)hdu 1501(Zipper——DFS)

    题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...

  6. HDU 1501 Zipper(DFS)

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

  7. hdu 1501 Zipper

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...

  8. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. hdu 1501 Zipper(DP)

    题意: 给三个字符串str1.str2.str3 问str1和str2能否拼接成str3.(拼接的意思可以互相穿插) 能输出YES否则输出NO. 思路: 如果str3是由str1和str2拼接而成,s ...

  10. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. jq-demo-点击选择(英雄联盟)

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

  2. 使用Fiddler抓取手机包

    配置Fiddler 设置抓取HTTPS包 允许为外部连接 配置移动端 移动端需要能够连接到主机做代理, 设置移动端的网络, 端口为Fiddler的端口, 然后给移动端安装证书, 访问主机名+代理端口号 ...

  3. 从psd图中将图层导出成单独文件

  4. eclipse背景设置什么颜色缓解眼睛疲劳

    eclipse背景设置什么颜色缓解眼睛疲劳 1.打开window->Preference,弹出Preference面板 2.展开General标签,选中Editors选项,展开. 3.选中 Te ...

  5. Java中的线程Thread方法之---stop()

    搞过Java线程的人都知道,stop这个方法是臭名昭著了,早就被弃用了,但是现在任然有很多钟情与他的人,永远都放不下他,因为从他的字面意思上我们可以知道他貌似可以停止一个线程,这个需求是每个搞线程开发 ...

  6. php-fpm 服务

    编译安装PHP的时候已经加入了--enable-fpm 在此基础上启动php-fpm服务 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/p ...

  7. hive shell参数

    Hive Shell参数 1.Hive命令行 语法结构 hive [-hiveconf x=y]* [<-i filename>]* [<-f filename>|<-e ...

  8. NOIp2018集训test-9-4

    老张让我们2.5h考NOI%你题,喵喵喵? 因为今(我)天(实)的(在)题(太)鬼(弱)畜(了)了,我还只改了t1. Problem A. reorder 考试的时候大家都写了最长不降子序列,然后全员 ...

  9. Spring-Security (学习记录六)--采用ehcache缓存UserDetails

    目录 1. spring-security提供了缓存UserDetails的UserDetailsService实现类. 2. 通过配置来完成缓存 UserDetails (使用ehcache) 3. ...

  10. ionic-CSS:ionic tab(选项卡)

    ylbtech-ionic-CSS:ionic tab(选项卡) 1.返回顶部 1. ionic tab(选项卡) ionic tab(选项卡) 是水平排列的按钮或者链接,用以页面间导航的切换.它可以 ...