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 ...
随机推荐
- vue-ssr 文档备注
https://ssr.vuejs.org/zh/universal.html 基本用法 通过vue-server-renderer插件的createRenderer方法创建一个renderer,再调 ...
- 手动搭建redis集群(3台)
安装redis 1.搜索源中的redis包 apt-cache pkgnames | grep redis 2.安装redis-server apt-get install redis-server ...
- pytorch导入错误so: undefined symbol: _Z11libshm_initPKc
首先删除torch文件 或者直接卸载 删除会更彻底 https://blog.csdn.net/qq_37674858/article/details/88870124 但是会发现卸载重装pytorc ...
- 数据结构( Pyhon 语言描述 ) — —第11章:集和字典
使用集 集是没有特定顺序的项的一个集合,集中的项中唯一的 集上可以执行的操作 返回集中项的数目 测试集是否为空 向集中添加一项 从集中删除一项 测试给定的项是否在集中 获取两个集的并集 获取两个集的交 ...
- 杭电 5773 The All-purpose Zero
Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000). ...
- Oracle跟踪分析数据库启动的各个阶段
目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...
- mysql replication常见错误整理
这篇文章旨在记录MySQL Replication的常见错误,包括自己工作中遇到的与网友在工作中遇到的,方面自己及别人以后进行查找.每个案例都是通过Last_IO_Errno/Last_IO_Erro ...
- liunx 根目录介绍
1. /bin binary二进制 存放系统许多可执行程序文件 执行的相关指令,例如ls pwd whoami,后台的支持文件目录 2. /sbin super binary超级的二进制 存放系统许多 ...
- [android开发篇]java环境配置
http://www.runoob.com/java/java-environment-setup.html Java 开发环境配置 在本章节中我们将为大家介绍如何搭建Java开发环境. window ...
- JSPatch部署JS代码控制OC代码
前言: 这里算是比较实用的一篇了吧,进行了网络安全部署 准备: bmob账号,JSPatch,公钥,私钥,MD5加密,AFNetWorking下载文件 #import <CommonCrypto ...