原题链接:

pid=1501">http://acm.hdu.edu.cn/showproblem.php?pid=1501

一:原题内容

Problem Description
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".
 
Input
The 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.

 
Output
For 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

二:分析理解

第三个字符串能否由前两个字符串依照原有顺序不变的原则交叉构成。须要注意的是,visit数组元素值为1时,表示该位置已被訪问过,下次无需訪问。

三:AC代码

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1 #include<iostream>
#include<string>
#include<string.h>
using namespace std; string str1, str2, str3;
int len1, len2, len3;
bool flag;//为真时,表示能够输出“yes” int visit[201][201];//标记数组,默认都是0 void DFS(int i, int j, int k); int main()
{
int N;
cin >> N;
for (int i = 1; i <= N; i++)
{
memset(visit, 0, sizeof(visit));
flag = false;
cin >> str1 >> str2 >> str3; len1 = str1.length();
len2 = str2.length();
len3 = str3.length(); DFS(0, 0, 0); if (flag)
cout << "Data set " << i << ": " << "yes\n";
else
cout << "Data set " << i << ": " << "no\n";
} return 0;
} void DFS(int i, int j, int k)
{
if (flag || visit[i][j])//假设为真或该点已被訪问过
return; if (k == len3)//由于依据题意len1+len2=len3
{
flag = true;
return;
} visit[i][j] = 1; if (i < len1 && str1[i] == str3[k])
DFS(i + 1, j, k + 1);
if (j < len2 && str2[j] == str3[k])
DFS(i, j + 1, k + 1); }

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. hdu1501 Zipper

    Zipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

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

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

  4. hdu 1501 Zipper dfs

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

  5. hdu1501 Zipper[简单DP]

    目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...

  6. HDU 1501 Zipper(DP,DFS)

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

  7. Zipper(poj2192)dfs+剪枝

    Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15277   Accepted: 5393 Descripti ...

  8. HDU 1501 Zipper 【DFS+剪枝】

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

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

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

  10. 【OpenJ_Bailian - 2192】Zipper(dfs)

    Zipper Descriptions: Given three strings, you are to determine whether the third string can be forme ...

随机推荐

  1. 简单暴力的TP5多主题方案

    一个小项目,需要配置多套前端主题.解决的思路是根据域名加载不同的主题配置. 一.在应用目录 application 下创建 common 目录. 二.application/common 目录下创建 ...

  2. 北京联通光猫WO-36(HG220GS-U)改为桥接模式

    家里弄了个极路由,想在公司里去操作路由器,交换文件.提前下载电影什么的,因此需要光猫改为桥接模式,让路由器拨号 由于WO-36(HG220GS-U)这个型号的光猫固件升级后(我的是3.x)不能用工程账 ...

  3. Asterisk[1]

    Asterisk[1]是一款GPLv2协议下的开源电话应用平台.简单来说,Asterisk是一个server应用.可以完毕发起电话呼叫.接受电话呼叫.对电话呼叫进行定制处理. 1.2.1 通道驱动 a ...

  4. GraphicsMagick java.io.FileNotFoundException: gm 错误解决办法

    GraphicsMagick java.io.FileNotFoundException: gm 解决办法, 方法一: ProcessStarter.setGlobalSearchPath(" ...

  5. jquery开发之代码风格

    1,链式操作风格. (1) 对于同一个对象不超过三个操作的.可直接写成一行.代码例如以下: $("li").show().unbind("click"); (2 ...

  6. 36.创建自定义的指令directive

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...

  7. BZOJ 3083 树链剖分+倍增+线段树

    思路: 先随便选个点 链剖+线段树 1操作 就直接改root变量的值 2操作 线段树上改 3操作 分成三种情况 1.new root = xx 整个子树的min就是ans 2. lca(new roo ...

  8. 升级JDK9后eclipse无法启动的解决方法

    解决方法-打开: D:\Program Files\eclipse\eclipse.ini 在文件末尾添加一行: --add-modules=ALL-SYSTEM 再次启动eclipse即可 感谢ht ...

  9. ViewPager中的数据更新

    getItemPosition(Object object) { return POSITION_NONE;} 出现的问题: 我希望能够通过调用 mAdapter.notifyDataSetChang ...

  10. SQL流程控制语句

    1 GoTo语句 IF 12>9GOTO print1ELSE GOTO print2 print1:PRINT '执行了流程1'--GOTO theEndprint2:PRINT '执行了流程 ...