原题链接:

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. BZOJ 3790 神奇项链(回文自动机+线段树优化DP)

    我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...

  2. dockerhub 推送镜像

    登录dockerhub [root@riyimei-node1:/home] > docker login Login with your Docker ID to push and pull ...

  3. Linux系统之间文件传输 scp 命令

    个人使用记录 scp /home/liwm/Downloads/mysql-5.5.32-linux2.6-x86_64.tar.gz root@192.168.122.3:/home/oldboy/ ...

  4. 做一个可复用的 echarts-vue 组件(延迟动画加载)

    在 vue 项目使用 echarts 的场景中,以下三点不容忽视:1. 可视化的数据往往是异步加载的:2. 若一个页面存在大量的图表( 尤其当存在关系图和地图时 ),往往会导致该页面的渲染速度很慢并可 ...

  5. 使用 vue + thinkjs 开发博客程序记录

    一入冬懒癌发作,给自己找点事干.之前博客程序写过几次,php 的写过两次,nodejs 用 ThinkJS 写过,随着 ThinkJS 版本从1.x 升级到 2.x 之前的博客程序也做过升级.但是因为 ...

  6. Apache activemq入门示例(maven项目)

    http://outofmemory.cn/java/mq/apache-activemq-demo

  7. Redis加入Centos Linux开机启动

    Redis加入Centos Linux开机启动 网上有很多redis在linux下自动启动的例子,实现的方式很多,很多都是参考一个老外流传出来启动的例子,其实直接使用是不行,而且有很多地方有一些语法错 ...

  8. CSDN博客2014年4月24日清理缓存

    亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...

  9. 如何让alertdialog选择完后自动关闭

    builder.setIcon(R.drawable.ic_system) .setTitle("串口号") .setSingleChoiceItems(mPorts, mSele ...

  10. Hadoop-CDH源码编译

    * Hadoop-CDH源码编译 这一节我们主要讲解一下根据CDH源码包手动编译的过程,至于为什么要使用CDH,前几节已经说明,那为什么又要自己手动编译,因为CDH的5.3.6对应的Hadoop2.5 ...