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的更多相关文章

  1. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  2. hdu 1501 Zipper dfs

    题目链接: HDU - 1501 Given three strings, you are to determine whether the third string can be formed by ...

  3. hdu 1501 Zipper

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1501 思路:题目要求第三个串由前两个组成,且顺序不能够打乱,搜索大法好 #include<cstdi ...

  4. (step4.3.5)hdu 1501(Zipper——DFS)

    题目大意:个字符串.此题是个非常经典的dfs题. 解题思路:DFS 代码如下:有详细的注释 /* * 1501_2.cpp * * Created on: 2013年8月17日 * Author: A ...

  5. HDU 1501 & POJ 2192 Zipper(dp记忆化搜索)

    题意:给定三个串,问c串是否能由a,b串任意组合在一起组成,但注意a,b串任意组合需要保证a,b原串的顺序 例如ab,cd可组成acbd,但不能组成adcb. 分析:对字符串上的dp还是不敏感啊,虽然 ...

  6. HDU 1501 Zipper 动态规划经典

    Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. HDU 1501 Zipper(DP,DFS)

    意甲冠军  是否可以由串来推断a,b字符不改变其相对为了获取字符串的组合c 本题有两种解法  DP或者DFS 考虑DP  令d[i][j]表示是否能有a的前i个字符和b的前j个字符组合得到c的前i+j ...

  8. HDU 1501 Zipper 字符串

    题目大意:输入有一个T,表示有T组测试数据,然后输入三个字符串,问第三个字符串能否由第一个和第二个字符串拼接而来,拼接的规则是第一个和第二个字符串在新的字符串中的前后的相对的顺序不能改变,问第三个字符 ...

  9. HDU 1501 Zipper(DFS)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

随机推荐

  1. python实现FTP

    原文地址:https://www.cnblogs.com/huangxm/p/6274645.html#undefined FTP服务的主动模式和被动模式 在开始之前,先聊一下FTP的主动模式和被动模 ...

  2. if else elif 用法和区别

    1.If语句:“如果条件为真,执行子句中的代码."始终包含以下部分: if关键字: 条件(即求值为True或False的表达式): 冒号: 在下一行开始,缩进的代码块(称为if子句) 例如: ...

  3. 数据结构( Pyhon 语言描述 ) — — 第3章:搜索、排序和复杂度分析

    评估算法的性能 评价标准 正确性 可读性和易维护性 运行时间性能 空间性能(内存) 度量算法的运行时间 示例 """ Print the running times fo ...

  4. DocView mode 1 -- 手册翻译

    文档原文在线地址 * 35 Document Viewing** DocView mode is a major mode for viewing DVI, PostScript (PS), PDF, ...

  5. skkyk:题解 洛谷P3865 【【模板】ST表】

    我不会ST表 智推推到这个题 发现标签中居然有线段树..? 于是贸然来了一发线段树 众所周知,线段树的查询是log(n)的 题目中"请注意最大数据时限只有0.8s,数据强度不低,请务必保证你 ...

  6. CentOS下配置LVM和RAID

    1.CentOS配置LVM http://www.cnblogs.com/mchina/p/linux-centos-logical-volume-manager-lvm.html http://ww ...

  7. 利用MySQL数据库如何解决大数据量存储问题?

    提问:如何设计或优化千万级别的大表?此外无其他信息,个人觉得这个话题有点范,就只好简单说下该如何做,对于一个存储设计,必须考虑业务特点,收集的信息如下:1.数据的容量:1-3年内会大概多少条数据,每条 ...

  8. PTA 08-图9 关键活动 (30分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/719 假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它 ...

  9. 九度oj 题目1398:移动次数

    题目描述: 众所周知JOBDU旗下的JOBBALA公司是一家以个性.亲民著称的IT公司.在JOBBALA公司成立50周年的日子里,公司CEO组织全体员工登山旅游.按照往常的习惯,导游通常要求游客按照身 ...

  10. 【Luogu】P1972HH的项链(链表+树状数组)

    题目链接 难题,所以会讲得细一些. 首先我们想如何统计区间[l,r]内不同贝壳的个数. 第一个思路就是线段树/树状数组,query(1,r)-query(1,l-1)对不对? 然而这样是不对的. 然后 ...