Zipper(DFS,DP)

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
3
cat tree tcraete
cat tree catrtee
cat tree cttaree

Sample Output
Data set 1: yes
Data set 2: yes
Data set 3: no

Source
Pacific Northwest 2004

题意:给三个字符串a,b,c,问a,b能否组成c,串a,b在c中的顺序不能颠倒

分析:可以用DFS。
另外按照题意,具有最优子结构,如上例,如果A、B可以组成C,那么,C最后一个字母,必定是 A 或 B 的最后一个字母组成。
C去除除最后一位,就变成是否可以求出 A-1和B 或者 A与B-1 与 是否可以构成 C-1。。。
状态转移方程:
**F[i][j] 表示 表示A的 前i位 和 B的前j位 是否可以组成 C的前i+j位        
  dp[i][j]= (dp[i-1][j]&&(a[i]==c[i+j]))||(dp[i][j-1]&&(b[j]==c[i+j]))**

代码如下:
1.DFS

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int  N=205;
char a[N],b[N],c[N*2];
int flag,len3;
void dfs(int len1,int len2,int len)
{
    if(flag)
        return;
    if(len==len3)///能组成
    {
        flag=1;
        return;
    }
    if(a[len1]==c[len])
        dfs(len1+1,len2,len+1);
    if(b[len2]==c[len])
        dfs(len1,len2+1,len+1);
}
int main()
{
    int t,k=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s%s",a,b,c);
        int len1=strlen(a),len2=strlen(b);
        len3=strlen(c);
        if(len1+len2!=len3)///若a和b的长度之和不等
        {
            printf("Data set %d: no\n",++k);
            continue;
        }
        flag=0;
        if(a[len1-1]==c[len3-1]||b[len2-1]==c[len3-1])//若a或者b的最后一位是c的最后一位
            dfs(0,0,0);
        printf("Data set %d: %s\n",++k,flag?"yes":"no");
    }
}

2.DP

#include<cstdio>
#include<cstring>
using namespace std;
const int N=205;
char a[N],b[N],c[N*2];
int la,lb,lc,dp[N][N];
int main()
{
    int t,k=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s%s",a+1,b+1,c+1);//从a[1]开始输入
        memset(dp,0,sizeof(dp));
        a[0]=b[0]=c[0]='0';///必须使第一位字符相等,以免循环处的下标为负值
        la=strlen(a);
        lb=strlen(b);
        for(int i=1; i<la; i++)///前la位能组成c的
            if(a[i]==c[i])
                dp[i][0]=1;
        for(int i=1; i<lb; i++)///前lb位能组成c的
            if(b[i]==c[i])
                dp[0][i]=1;
        for(int i=1; i<la; i++)
            for(int j=1; j<lb; j++)
                dp[i][j]=(dp[i-1][j]&&a[i]==c[i+j])||(dp[i][j-1]&&b[j]==c[i+j]);
        printf("Data set %d: ",++k);
        printf("%s\n",dp[la-1][lb-1]?"yes":"no");
    }
    return 0;
}

 

上一篇:2021-07-08


下一篇:【Go】strings库字符串处理详说