HDU 1501 Zipper 【DFS】
来源:互联网 发布:音墙网络 编辑:程序博客网 时间:2024/05/16 18:25
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7435 Accepted Submission(s): 2625
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".
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.
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.
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
3cat tree tcraetecat tree catrteecat tree cttaree
Sample Output
Data set 1: yesData set 2: yesData set 3: no
Source
Pacific Northwest 2004
题目思路很清晰,考的部分也只是剪枝还有记忆化,,但不知为何,与别人代码没有什么差别,就是过不了。。暂时先放着吧。。。哪位路过的大侠,可以帮着找找看。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define maxn 200+5int n;char arr[maxn];char brr[maxn];char crr[maxn*2];int vis[maxn][maxn];int lena,lenb,lenc;bool flag;void dfs(int pos1,int pos2,int pos3);int main(){scanf("%d",&n);for(int t = 1; t <= n; t++){scanf("%s%s%s",arr,brr,crr);//getchar();lena = strlen(arr);lenb = strlen(brr);lenc = strlen(crr);flag = false;memset(vis,0,sizeof(vis));if(arr[lena-1] != crr[lenc-1] && brr[lenb-1] != crr[lenc-1]){printf("Data set %d: no\n",t);continue;}dfs(0,0,0);if(flag){printf("Data set %d: yes\n",t);}else{printf("Data set %d: no\n",t);}}return 0;}void dfs(int pos1,int pos2,int pos3){if(pos3 >= lenc){flag = true;return ;}if(flag)return;if(vis[pos1][pos2]){return ;}vis[pos1][pos2] = 1;if(pos1 < lena && arr[pos1] == crr[pos3]){dfs(pos1+1,pos2,pos3+1);}if(pos2 < lenb && brr[pos2] == crr[pos3]){dfs(pos1,pos2+1,pos3+1);}}
0 0
- hdu 1501 Zipper (DFS)
- HDU 1501 Zipper(DFS)
- HDU 1501 Zipper 【DFS】
- HDU 1501 Zipper(DFS)
- hdu 1501 Zipper(DFS)
- Hdu 1501 Zipper【dfs】
- HDU 1501 Zipper (DFS)
- HDU 1501 Zipper(DFS)
- HDU 1501 Zipper(DP,DFS)
- HDU 1501 Zipper(DP,DFS)
- HDU 1501 Zipper (DFS)
- hdu 1501 Zipper(dfs)
- 【DFS(记忆化)】hdu 1501 Zipper
- hdu 1501 Zipper(记忆化DFS)
- HDU-1501 Zipper DFS+记忆化搜索
- Zipper(HDU 1501) —— DFS
- hdu 1501 Zipper(dfs或dp)
- HDU 1501 Zipper (DFS+剪枝做法)
- html网站模板积累
- 第六周项目三 人数不确定的工资类
- 2015届3i工作室正式成员名单公布
- 深搜基础
- 产品 7 2033 万美元,Pebble Time 众筹创新纪录,可战争才刚刚开始
- HDU 1501 Zipper 【DFS】
- CodeForces 478A----Initial Bet
- C#的那些事(二)命名空间 namespace
- 四张图看懂Jquery
- linux系统管理---获取文件的大小
- android 开发如何做内存优化
- CSDN-markdown编辑器
- 自定义表盘
- kernel 网桥代码分析