原题链接: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];//标记数组。默认都是0void 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";elsecout << "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);}

转载于:https://www.cnblogs.com/liguangsunls/p/7043687.html

hdu1501 Zipper--DFS相关推荐

  1. 杭电 1501 zipper(典型dfs)

    写在前面的: 最近一直在做搜索,这个题目也是DFS一道很典型的题目,一开始想不用搜索做,结果发现思路是错的,大神给了我几组样例之后就只能重拍了.所以开始考虑DFS.一开始没看出来时搜索,所以按自己方法 ...

  2. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  3. [C] [编程题]连通块(DFS解决)

    时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 256M,其他语言512M 来源:牛客网 金山办公2020校招服务端开发工程师笔试题(一) 题目描述 给一个01矩阵,1代表是陆地,0代表 ...

  4. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  5. usaco snail trails(dfs)

    dfs啊,我还写了好长时间,一天不如一天. /* ID:jinbo wu TASK: snail LANG:C++ */ #include<bits/stdc++.h> using nam ...

  6. usaco shuttle puzzle(dfs剪枝)

    这题一看我也以为找规律,然后无法下手之后又想到bfs最后看题解是用dfs大神dfs用的出神入化. 不过这题好像可以找规律. /* ID:jinbo wu TASK: shuttle LANG:C++ ...

  7. usaco street race(dfs)

    一开始我觉得很麻烦但第一题好做由于数据较小直接每个点删后dfs就可以了,第二问我不知道如何判断有没有公共的道路,最后看别人的也挺简单的就是看分别从两条公路的起点开始dfs如果他们能到达同一点就代表有公 ...

  8. Forest Program dfs+tanjar仙人掌

    题目链接 CCPC2019 F题. 题意:给一颗仙人掌树,让你求每一个小环的边的个数,用快速幂即可求解. 思路:第一反应是tanjar乱搞,把每个环上的点取出来,类似于缩点的方法.但是忽然感觉dfs能 ...

  9. HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化

    题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...

  10. A and B and Lecture Rooms CodeForces - 519E LCA+dfs序

    看到这个题的第一个思路就是就是统计以每一个点为根的所有节点个数,然后具体就分情况讨论一下即可. 因为刚刚学习了dfs序,这个题就用了dfs序来通过进出时间戳来相减表示其为根的子节点个数. 分情况 我们 ...

最新文章

  1. mysql的语句分类,查询、子查询及连接查询
  2. 为什么中国的程序员技术偏低
  3. 项目使用了redis还需要mysql_【11-05】lnmp项目中Redis和Mysql配合使用应该注意哪些问题?...
  4. C#通过FFmpeg获得视频文件参数
  5. CompletableFuture详解~allOf
  6. NAS新突破,仅需半个 GPU day 即可训练出高性能架构!阿里提出 Zen-NAS
  7. 在SQL中使用PL/SQL函数存在的问题
  8. 【Elasticsearch】Elasticsearch 热点线程 hot_threads
  9. 做问答系统是对题目修改的bug
  10. r roc曲线 语言_R语言系列6:生存分析中多重时间依赖性ROC曲线绘制 timeROC
  11. 轻轨与地铁有什么区别吗?
  12. 信息论与贝叶斯(二)
  13. python报表自动化系列 - Excel单元格(Cell)索引范围对应的所有单元格
  14. XCodeGhost 病毒检查方法
  15. python---turtle库(详解)
  16. Aliddns插件使用:小白超详细图文教程
  17. 深入理解Java-GC机制
  18. JavaScript中会打印出undefined的情况汇总
  19. 2022年开始学习Delphi并成为Delphi程序员的5个重要原因
  20. 【学习笔记 31】 buu [0CTF 2016]piapiapia

热门文章

  1. MySQL优化详解(四)——MySQL缓存设置
  2. NYOJ--91--阶乘之和
  3. Scrapy爬虫进阶操作之CrawlSpider(一)
  4. Android Q 将增强未知来源应用安装的安全性
  5. session 分布式处理-----https://segmentfault.com/a/1190000013447750?utm_source=tag-newest
  6. struts2框架概述
  7. 异步任务利器Celery(一)介绍
  8. 使用PADDING-TOP:(PERCENTAGE)实现响应式背景图片
  9. vagrant启动报错The following SSH command responded with a no
  10. ASP.NET MVC过滤器