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".
 

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
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
原创粉丝点击