HDU 1501
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7813 Accepted Submission(s): 2765
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
//本题主要思路依据第三个字符串dnf搜索从前两个字符串中查找,查找到的字符放入数组res中。当res与第三个字符串相等时搜索结
//束
#include <stdio.h>
#include <string.h>
char ss[420];
char s1[210];
char s2[210];
char res[420];
bool vis[210][210]; //用数组记录非常重要不然会超时
int flag,cnt,len1,len2;
void dfs(int ini,int init)
{
if(vis[ini][init]) return;
vis[ini][init]=1;
if(strcmp(res,ss)==0)
{
flag=1;
return;
}
else
{
if(s1[ini]==ss[cnt]) //当搜索到与第三字符串中的字符相等时记录下字符再递归搜索
{
res[cnt++]=s1[ini];
dfs(ini+1,init);
cnt--;
if(flag)
return;
}
if(s2[init]==ss[cnt])
{
res[cnt++]=s2[init];
dfs(ini,init+1);
cnt--;
if(flag)
return; }
}
} int main()
{
int n;
int cnt1=1;
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s%s%s",s1,s2,ss);
len1=strlen(s1);
len2=strlen(s2);
cnt=flag=0;
memset(res,'\0',sizeof(res)); //我调试了快半个小时了,才发现假设用0的话数组不能全然清空
memset(res,0,sizeof(vis));
dfs(0,0);
printf("Data set %d:",cnt1++);
if(flag)
printf(" yes\n"); //注意空格
else
printf(" no\n");
}
return 0 ;
}
HDU 1501的更多相关文章
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- hdu 1501 Zipper dfs
题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...
- hdu 1501 Zipper
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...
- (step4.3.5)hdu 1501(Zipper——DFS)
题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...
- HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)
题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...
- HDU 1501 Zipper 动态规划经典
Zipper Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 1501 Zipper(DP,DFS)
意甲冠军 是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法 DP或者DFS 考虑DP 令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...
- HDU 1501 Zipper 字符串
题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...
- HDU 1501 Zipper(DFS)
Problem Description Given three strings, you are to determine whether the third string can be formed ...
随机推荐
- Life is short.,You need Python
真棒Python https://awesome-python.com/ 精选的Python框架,库,软件和资源的精选列表. 灵感来自awesome-php. 真棒Python 管理员面板 算法和设 ...
- 离线web-ApplicationCache
https://www.html5rocks.com/en/tutorials/appcache/beginner/ http://diveintohtml5.info/offline.html#fa ...
- Springboot(一)-IDEA搭建springboot项目(demo)
jdk版本:1.8.0_162 1.打开IDEA-file-new-project-Spring Initializer,JDK和URL选默认,next (这一步如果是不能联网的话,可以选择直接创建m ...
- ZOJ 2058 The Archaeologist's Trouble II(贪心+模拟)
[题目大意] 一个n高的塔,由@ * ?三种字符组成.每行相邻两个字符不能相邻. '?' 表示未确定是 '@' 还是 '*' . 求'@' 可能出现的最多和最少次数. [分析] 在可以填的情况下 先填 ...
- set的应用:UVa10815-Andy's First Dictionary
Andy's First Dictionary Andy, 8, has a dream - he wants to produce his very own dictionary. This is ...
- python中set()函数的用法
set顾名思义是集合,里面不能包含重复的元素,接收一个list作为参数 list1=[1,2,3,4] s=set(list1) print(s) #逐个遍历 for i in s: print(i) ...
- python基础——10(三元运算符、匿名函数)
一.三元运算符 本质是if--else--的语法糖 前提:简化if--else--的结构,且两个分支有且只有一条语句 案例: a = 20 b = 30 res = a if a > b els ...
- Python基础之yield,匿名函数,包与re模块
一.表达式形式的yield 1.另外一种形式的yield def deco(func): def wrapper(*arges, **kwargs): res = func(*arges, **kwa ...
- iOS 开发之多线程之GCD
1.GCD(Grand Centrol Dispath) 并行:宏观以及微观都是两个人再拿着两把铁锹在挖坑,一小时挖两个大坑 并发:宏观上是感觉他们都在挖坑,微观是他们是在使用一把铁锹挖坑,一小时后他 ...
- Appium自动化-基于java的环境搭建
引言 自动化测试框架搭建主要分为以下几个方面的下载安装及环境配置: 1.jdk 2. adt 3. appium 4. testng插件 工具链接: https://pan.baidu.com/s/1 ...