zipper dp与dfs poj4710

来源:互联网 发布:中药软件哪个好使 编辑:程序博客网 时间:2024/05/22 10:30

PageWeb ContestsProblemsRanklistStatusStatistics

Zipper

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 11
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
 
ACcode:dp wangyikun
PageWeb ContestsProblemsRanklistStatusStatisticsZipper Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 25   Accepted Submission(s) : 11Problem DescriptionGiven 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: catString B: treeString C: tcraeteAs 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: catString B: treeString C: catrteeFinally, notice that it is impossible to form "cttaree" from "cat" and "tree". InputThe 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.  OutputFor 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 Input3cat tree tcraetecat tree catrteecat tree cttaree Sample OutputData set 1: yesData set 2: yesData set 3: no SourcePacific Northwest 2004 
dfs zhangpeng:

#include <iostream>#include<cstdio>#include<string.h>using namespace std;#define maxn 10000bool flag;char a[maxn],b[maxn],c[maxn];int pos1,pos2,pos3;void dfs(int la,int lb,int lc ){    if(la+1==0&&lb+1==0)    {        flag=true;        return;    }    if(!flag&&a[la]==c[lc]&&(la+1))        dfs(la-1,lb,lc-1);    if(!flag&&b[lb]==c[lc]&&(lb+1))        dfs(la,lb-1,lc-1);    return;}int main(){    int t,cns=0;    cin>>t;    for(int i=0; i<t; i++)    {        cin>>a>>b>>c;        printf("Data set %d: ",++cns);        flag=false;        pos1=strlen(a);        pos2=strlen(b);        pos3=strlen(c);        if(a[pos1-1]==c[pos3-1]||b[pos2-1]==c[pos3-1])            dfs(pos1-1,pos2-1,pos3-1);        printf(flag?"yes\n":"no\n");    }    return 0;}


0 0