hdu1501 Zipper--DFS
原题链接: pid=1501">http://acm.hdu.edu.cn/showproblem.php?pid=1501
一:原题内容
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".
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.
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.
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
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的更多相关文章
- 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 ...
- hdu1501 Zipper
Zipper Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- hdu1501 Zipper[简单DP]
目录 题目地址 题干 代码和解释 参考 题目地址 hdu1501 题干 代码和解释 最优子结构分析:设这三个字符串分别为a.b.c,如果a.b可以组成c,那么c的最后一个字母必定来自a或者b的最后一个 ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- Zipper(poj2192)dfs+剪枝
Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15277 Accepted: 5393 Descripti ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- 【OpenJ_Bailian - 2192】Zipper(dfs)
Zipper Descriptions: Given three strings, you are to determine whether the third string can be forme ...
随机推荐
- 使用windowbuilder的时候更方便——设置默认把控件生成为成员变量而不是局部变量
找了一大圈,最后还是上Google才找到这个方法的.以前改过了,重新设置工作目录之后设置都丢失了,却找不到改的办法,这次长个记性,记在自己博客里. 设置成成员属性的好处是随后使用这些控件的时候方便.
- NOIp2018模拟赛四十三
有了昨天的经验,不慌,开题先看source ******** 再看看题,看到C题标题: ******** 有毒... B题的“显然”50分结论推了我一个小时,然后就弃疗了... 成绩:0+50+5=5 ...
- [POJ2823][洛谷P1886]滑动窗口 Sliding Window
题目大意:有一列数,和一个窗口,一次能框连续的s个数,初始时窗口在左端,不断往右移动,移到最右端为止,求每次被框住的s个数中的最小数和最大数. 解题思路:这道题是一道区间查询问题,可以用线段树做.每个 ...
- redis搭建与安装
redis提供五种数据类型:string,hash,list,set及zset(sorted set). 第一部分:安装redis 希望将redis安装到此目录 1 /usr/local/redis ...
- Vue异步组件Demo
Vue异步组件Demo 在大型应用中,我们可能需要将应用拆分为多个小模块,按需从服务器下载.为了进一步简化,Vue.js 允许将组件定义为一个工厂函数,异步地解析组件的定义.Vue.js 只在组件需要 ...
- 2015,鬼王Xun和GGL比赛,带给我们无尽的欢乐
一如既往的风格,正文之前,先扯蛋~ 这篇文章好久就想写了,一直没有动笔,最近在忙于Android和iOS等技术研究,又忙于金融投资等方面的学习和写作.这个周末,把技术进度延缓了点,把很多闲杂的 ...
- numpy学习笔记 - numpy数组的常见用法
# -*- coding: utf-8 -*- """ 主要记录代码,相关说明采用注释形势,供日常总结.查阅使用,不定时更新. Created on Mon Aug 20 ...
- 转:让MySQL支持emoji表情
转自:http://www.cnblogs.com/suifu/p/5848269.html 公司有新要求,ios客户端要上线评论中可以使用emoji表情的功能,在mysql 5.5 之前,UTF-8 ...
- iOS代码添加视图约束
项目要做这样一个效果的启动页. 考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动画效果 - 简书 考虑到屏幕适配问题,因此采用代码对视图添加约束.在添加约束的过程中遇 ...
- 11.字符,字符常见开发,_itoa函数
各种字符所占字节 wchar_t wch = L'我'; //占4个字节 char ch;//占1个字节 printf("%d\n", sizeof("A")) ...