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 ...
随机推荐
- RuntimeError: Failed to init API, possibly an invalid tessdata path: E:\python36\报错
OCR:光学识别符,tesserocr是python中一个OCR识别库,是对tesseract做的一个python的 API封装,所以它的核心是tesseract 在这里我安装的版本是:tessera ...
- 【支付宝支付】扫码付和app支付,回调验证签名失败问题
在检查了参数排序,编码解码,文件编码等问题后,发现还是签名失败,最后找出原因: 扫码付和app支付采用的支付宝公钥不一样 Pid和公钥管理里面: 开放平台密钥界面和开放平台应用界面的密钥应该一 ...
- Lex与Yacc学习(三)之符号表
符号表 列举单词表的方式虽然简单但是不全面,如果在词法分析程序运行时可以构建一个单词表,那么就可以在添加新的单词时不用修改词法分析程序. 下面示例便利用符号表实现,即在词法分析程序运行时从输入文件中读 ...
- 【HIHOCODER 1529】 不上升序列
描述 给定一个长度为 n 的非负整数序列 a[1..n]. 你每次可以花费 1 的代价给某个 a[i] 加1或者减1. 求最少需要多少代价能将这个序列变成一个不上升序列. 输入 第一行一个正整数 n. ...
- 关于面试总结-SQL经典面试题
关于面试总结6-SQL经典面试题 前言 用一条SQL 语句查询xuesheng表每门课都大于80 分的学生姓名,这个是面试考sql的一个非常经典的面试题 having和not in 查询 xueshe ...
- lnmp环境的使用
lnmp环境的使用 安装的软件都安装到了:/usr/local 管理nginx service nginx start|stop|restart|reload 管理mysql 直接执行mysql即可登 ...
- TOJ 4095: love168yk的选美大赛
4095: love168yk的选美大赛 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: ...
- 【Luogu】P2051中国象棋(DP)
题目链接 去看STDCALL的题解吧 #include<cstdio> #include<cctype> #define mod 9999973 inline long lon ...
- HDU——1195Open the Lock(双向BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Codeforces633G - Yash And Trees
Portal Description 给出一个\(n(n\leq10^5)\)个点的带点权树,以\(1\)为根:以及正整数\(m(m\leq10^3)\).进行\(q(q\leq10^5)\)次操作: ...