Zipper(poj2192)dfs+剪枝
Zipper
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15277 | Accepted: 5393 |
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
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yes Data set 3: no
Output Limit Exceeded的问题了N次。。。最后发现自己死循环了。
之前,剪枝没写好,可惜不能最后秒杀啊!o(︶︿︶)o 唉
具体的见代码吧,
ps:http://poj.org/problem?id=2192
#include<stdio.h> #include<string.h> char a[510],b[205],c[205]; int visit[500]; int lena,lenb,lenc; bool flag; int check()//与c串匹配 { int k,t=0; for(k=0;k<lena;k++) { if(!visit[k]&&a[k]-c[t]==0) t++; } if(t==lenc) return 1; else return 0; } int dfs(int x,int y,int t)//x是b串的瞬时位置,y是a串的瞬时位置,t是c串的瞬时位置, { if(x>=lenb) { if(check()) flag=true; return 0;//返回 } if(flag) //剪枝,flag已经为真 return 0; if(b[x]==a[y]) { visit[y]=1; if(y<lena) dfs(x+1,y+1,t);//匹配下一个 visit[y]=0; } else { if(a[y]!=c[t]) //不在b串中的,肯定在c串中,否则退出!只是剪枝的关键 return 0; } if(!flag && y<lena)// 不在b串中的,肯定在c串中,所以匹配c串 dfs(x,y+1,t+1); return 0; } int main() { int T,i,j; scanf("%d",&T); i=1; getchar(); while(i<=T) { scanf("%s%s%s",b,c,a); memset(visit,0,sizeof(visit)); lena=strlen(a); lenb=strlen(b); lenc=strlen(c); printf("Data set %d: ",i); int temp=0; flag=false; //a串的最后一个字符要么是b串的最后一个,要么是c串的最后一个,都不是的话,应该输出no if(a[lena-1]!=b[lenb-1] && a[lena-1]!=c[lenc-1]) flag=false; else if(lena!=lenb+lenc)//若是b串和c串的长度之和不等于a串的话,应该输出no flag=false; else//否则深搜 dfs(0,0,0); if(flag) printf("yes\n"); else printf("no\n"); i++; } } /* 50 cat tree tcraete cat tree catrtee cat tree cttaree cat tree tcraeta cat tree catree cat tree cttaree */
转载请注明出处:http://www.cnblogs.com/yuyixingkong/
自己命运的掌控着!