• HUD 1501 Zipper(记忆化 or 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
     
    记忆化和DP均能够,通过两个串合成第三个串。顺序不变。
    记忆化:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    typedef long long LL;
    using namespace std;
    const int maxn=220;
    char s1[maxn],s2[maxn],s[maxn<<1];
    int dp[maxn][maxn];
    int len,len1,len2;
    int t;
    int dfs(int i,int j,int k)
    {
       if(k==len)
          return 1;
       if(dp[i][j])
            return 0;
       dp[i][j]=1;
       if(s1[i]==s[k])
       {
           if(dfs(i+1,j,k+1))
            return 1;
       }
       if(s2[j]==s[k])
       {
           if(dfs(i,j+1,k+1))
            return 1;
       }
       return 0;
    }
    int main()
    {
        int l=0;
        scanf("%d",&t);
        while(t--)
        {
             scanf("%s%s%s",s1,s2,s);
             len1=strlen(s1);
             len2=strlen(s2);
             len=strlen(s);
             memset(dp,0,sizeof(dp));
             if(dfs(0,0,0))
                printf("Data set %d: yes
    ",++l);
             else
                printf("Data set %d: no
    ",++l);
        }
        return 0;
    }
    

    DP:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<limits.h>
    using namespace std;
    const int maxn=220;
    char s1[maxn],s2[maxn],s[maxn<<1];
    int dp[maxn][maxn];
    
    int main()
    {
        int t;
        int l=0;
        scanf("%d",&t);
        getchar();
        while(t--)
        {
            scanf("%s%s%s",s1+1,s2+1,s+1);
            int len1=strlen(s1+1);
            int len2=strlen(s2+1);
            int len=strlen(s+1);
            memset(dp,0,sizeof(dp));
            dp[0][0]=1;
            for(int i=0;i<=len1;i++)
            {
               for(int j=0;j<=len2;j++)
               {
                   if(i==0&&j==0)
                      continue;
                   if(s[i+j]==s1[i]&&i&&dp[i-1][j])
                      dp[i][j]=1;
                   if(s[i+j]==s2[j]&&j&&dp[i][j-1])
                      dp[i][j]=1;
               }
            }
            if(dp[len1][len2])
               printf("Data set %d: yes
    ",++l);
            else
               printf("Data set %d: no
    ",++l);
        }
        return 0;
    }


  • 相关阅读:
    Javascript与数据结构系列(一)——栈的实现
    css页面布局
    你不知道的Console
    EMV/PBOC 解析(一) 卡片文件结构
    javascript中的事件委托
    css中的浮动以及清除浮动
    npm以及gulp相关操作
    node环境配置安装(nvm)
    ajax详解
    jQuery中的事件机制深入浅出
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3919752.html
Copyright © 2020-2023  润新知