HDU-1501 (POJ-2192) Zipper (DFS||DP)

来源:互联网 发布:淘宝上解id锁是真的吗 编辑:程序博客网 时间:2024/04/30 06:27

此处有目录↑

Zipper

http://acm.hdu.edu.cn/showproblem.php?pid=1501
http://poj.org/problem?id=2192
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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

解法一:DFS

dfs特别简单,但是有组数据会TLE,所以需要剪枝才行

#include <cstdio>#include <cstring>//#define LOCALusing namespace std;const int MAXN=205;char a[MAXN],b[MAXN],all[2*MAXN-5];int lenA,lenB,lenAll;bool DFS(int posA,int posB,int posAll) {    if(posA==lenA&&posB==lenB)        return true;    if(posA<lenA&&a[posA]==all[posAll]&&DFS(posA+1,posB,posAll+1))        return true;    if(posB<lenB&&b[posB]==all[posAll]&&DFS(posA,posB+1,posAll+1))        return true;    return false;}int main(){#ifdef LOCALfreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);#endif    int T,cnt=0;    scanf("%d",&T);    while(T--) {        printf("Data set %d: ",++cnt);        scanf("%s%s%s",a,b,all);        lenA=strlen(a);        lenB=strlen(b);        lenAll=strlen(all);        if((all[0]!=a[0]&&all[0]!=b[0])||(all[lenAll-1]!=a[lenA-1]&&all[lenAll-1]!=b[lenB-1])) {//剪枝,不加上就会TLE            printf("no\n");            continue;        }        printf("%s\n",DFS(0,0,0)?"yes":"no");    }    return 0;}



解法二:DP

要不是放在dp专题里面,我还一直以为只能dfs做

看到一个dp的解法,才明白如何进行状态转移
设dp[i][j]表示串a的前i个字符与串b的前j个字符是否能形成串c的前i+j个字符,
若dp[i-1][j]&&a[i]==c[i+j]或dp[i][j-1]&&b[j]==c[i+j]时,dp[i][j]才为true

我写的最外层循环是枚举串c的字符,内存分别枚举串a和串b的字符,这样方便初始化和理解(串c的下标从1开始),但有一个常数

感觉还不是很明白hdu的判决结果,第一次c数组小了,结果返回WA,第二次下标出现负数了,结果返回TLE

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n,m,al,bl,cl,lim;char a[205],b[205],c[405];bool dp[205][205];//dp[i][j]表示串a的前i个字符与串b的前j个字符是否能形成串c的前i+j个字符int main() {    int T,kase=0;    scanf("%d",&T);    while(kase<T) {        scanf("%s%s%s",a+1,b+1,c+1);        al=strlen(a+1);        bl=strlen(b+1);        cl=al+bl;        memset(dp,false,sizeof(dp));        dp[0][0]=true;        for(int k=1;k<=cl;++k) {//枚举串c的字符            lim=min(k,al);            for(int i=max(1,k-bl);i<=lim;++i) {//在a串上进行状态转移                if(dp[i-1][k-i]&&a[i]==c[k])//如果串a前i-1个字符与串b前k-i个字符能构成串c的前k个字符,并且当前a[i]==c[k]                    dp[i][k-i]=true;            }            lim=min(k,bl);            for(int j=max(1,k-al);j<=lim;++j) {//在b串上进行状态转移                if(dp[k-j][j-1]&&b[j]==c[k])//如果串a前k-j个字符与串b前j-1个字符能构成串c的前k个字符,并且当前b[j]==c[k]                    dp[k-j][j]=true;            }        }        printf("Data set %d: %s\n",++kase,dp[al][bl]?"yes":"no");    }    return 0;}


0 0