ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Zipper(DFS,DP)

2021-07-06 13:51:32  阅读:169  来源: 互联网

标签:set int Data tree DFS cat Zipper strings 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;
}

 

标签:set,int,Data,tree,DFS,cat,Zipper,strings,DP
来源: https://blog.51cto.com/u_13696685/2988719

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有