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. Windows的ODBC配置指南: MySQL, PostgreSQL, DB2, Oracle

    MySQL- 官网: https://dev.mysql.com/downloads/connector/odbc/- 安装: * msi格式, 直接安装即可 * zip格式, 解压缩, 命令行(管理 ...

  2. 自然数幂和&伯努利数(Bernoulli)

    二项式定理求自然数幂和 由二项式定理展开得 \[ (n+1)^{k+1}-n^{k+1}=\binom {k+1}1n^k+\binom {k+1}2n^{k-1}+\cdots+\binom {k+ ...

  3. 数字IC设计工程师成长之路

    学习的课程 仿真工具VCS实践学习 2019年12月9日-2019年12月23日

  4. $My$ $template$(持续更新)

    树链剖分:(来源:树的统计) #include<bits/stdc++.h> #define rint register int using namespace std; inline v ...

  5. NX二次开发-UFUN特征选择对话框UF_UI_select_feature

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //特征选择对话框 char sMessage[] = "特征 ...

  6. UvaLive6893_The_Big_Painting

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:传送门  Portal  原题目描述在最下面.  给你两个二 ...

  7. FastText总结,fastText 源码分析

    文本分类单层网络就够了.非线性的问题用多层的. fasttext有一个有监督的模式,但是模型等同于cbow,只是target变成了label而不是word. fastText有两个可说的地方:1 在w ...

  8. ultis, BIT(x), BITCOUNT(x)

    /* http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html */ #define BITCOUNT(x) (((BX_(x)+(BX_(x)> ...

  9. 20130317 如何批量把文件名称写入txt文档

    1.如何批量把文件名称写入txt文档 COMMAND 窗口例:存放图片的文件夹是 D:\123\就用下面一名命令就OKdir d:\123\*.jpg /b > A.TXT 那么你所以JPG格式 ...

  10. 如何提高阿里云cdn命中率?原命中率极低。

    博客搬迁了,给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/08/29/%E5%A6%82%E4%BD%95%E6%8F%90%E9%AB%98%E9 ...