原题链接:

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. WordPress 自动草稿和文章修定版本

    写文章的时候发现 WordPress 有两个有意思的地方, WordPress 自动草稿和文章修定版本: 1.点击创建新文章的时候,会在数据库自动生成一条草稿数据: 2.修改数据的时候会将历史文章当做 ...

  2. VBA 中Dim含义

    楼主是个初学者,在应用vba时遇到了dim方面的问题,查了很多资料后想把关于dim的这点儿知识简单整理出来 首先,从我遇到的问题作为切入点吧, (不得不承认我遇到的错误是很低级的) 具体的情境就不还原 ...

  3. 12种CSS BUG解决方法与技巧

    一. 针对浏览器的选择器 这些选择器在你需要针对某款浏览器进行css设计时将非常有用. IE6及其更低版本,本文由52CSS.com整理,转载请注明出处! * html {} IE7及其更低版本 *: ...

  4. redis 篇 - 键 and string

    redis 进入控制台 redis-cil 需要输入密码的时候可以是用 -a redis-cil -a abcd1234 redis 数据类型 string hash list set zset( 有 ...

  5. PCA一些性质的定性理解

    1.通过本征向量和本征值求主成分 关系:本征值是本征向量的缩放倍数,本征值大的对应的本征向量上的样本的数目就越多:相反本征值越小的,就本征向量上的样本数量就会少.因此可以求出PCA的主成分 主成分分析 ...

  6. [洛谷P1750]KC喝咖啡

    题目大意:给你n个物品,每个物品有一个价值$v_i$和一个时间$t_i$,要你取m个物品,使得他们的美味度($\frac{\sum v_i}{\sum t_i}$)最大,求这个美味度. 解题思路:由于 ...

  7. Vue2.0父子组件间事件派发机制

    从vue1.x过来的都知道,在vue2.0中,父子组件间事件通信的$dispatch和$broadcase被移除了.官方考虑是基于组件树结构的事件流方式实在是让人难以理解,并且在组件结构扩展的过程中会 ...

  8. 紫书 例题 10-3 UVa 10375 (唯一分解定理)

    这道题感觉非常的秀 因为结果会很大,所以就质因数分解分开来算 非常的巧妙! #include<cstdio> #include<vector> #include<cstr ...

  9. mysql关联更新表

    UPDATE ecm_store s LEFT JOIN (SELECT store_id, COUNT(goods_id) AS goods_count FROM ecm_goods GROUP B ...

  10. How to customize Skin Gallery - Remove / rename skins and groups

    1. REMOVE (HIDE) A SPECIFIC SKIN Traverse through the gallery group collection, then through its gal ...