码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1501 Zipper dfs

时间:2015-04-19 00:57:26      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: HDU - 1501

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.
题意描述:给出三个字符串,在前两个字符串不改变自身相对顺序的前提下,判断第三个字符串是否由前两个字符串组成。
算法分析:dfs,优化一:如果第三个字符串的第一个和最后一个字符分别不是第一个或第二个字符串的首字母、末尾字母,那么肯定是不可能的;优化二:dfs过程中标记一个数组,判断此时状态是否访问过。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 const int maxn=200+10;
10 
11 int flag;
12 char s[maxn],s2[maxn],s3[maxn+maxn];
13 int vis[maxn][maxn];
14 int cnt,cnt2,cnt3,len,len2,len3;
15 
16 void dfs(int sum,int x,int y)
17 {
18     if (sum >= len3) {flag=1;return ;}
19     if (s[x]!=s3[sum] && s2[y]!=s3[sum])
20         return ;
21     if (vis[x][y]) return ;
22     vis[x][y]=1;
23     if (s[x]==s3[sum]) dfs(sum+1,x+1,y);
24     if (s2[y]==s3[sum]) dfs(sum+1,x,y+1);
25     return ;
26 }
27 
28 int main()
29 {
30     int t,ncase=1;
31     while (scanf("%d",&t)!=EOF)
32     {
33         while (t--)
34         {
35             scanf("%s%s%s",s,s2,s3);
36             len=strlen(s);
37             len2=strlen(s2);
38             len3=strlen(s3);
39             flag=0;
40             memset(vis,0,sizeof(vis));
41             if (s3[len3-1]==s[len-1]||s3[len3-1]==s2[len2-1])
42                 dfs(0,0,0);
43             if (flag) printf("Data set %d: yes\n",ncase++);
44             else printf("Data set %d: no\n",ncase++);
45         }
46     }
47     return 0;
48 }

 

hdu 1501 Zipper dfs

标签:

原文地址:http://www.cnblogs.com/huangxf/p/4438410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!