zoukankan      html  css  js  c++  java
  • POJ 2192 Zipper

    Zipper
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14193   Accepted: 4992

    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
    题目大意:输入三个字符串A,B,C,问C能否有A,B中的字符组成,且字符的顺序必须是原来在字符A,B中的顺序。
    解题方法:用动态规划,我用了两种方法,其实思想都是一样的。
    方法一:
    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int dp[205][205];
    
    int main()
    {
        int N;
        scanf("%d", &N);
        char str1[205], str2[205], str3[405];
        for (int i = 1; i <= N; i++)
        {
            cin>>str1+1>>str2+1>>str3+1;
            int nLen1 = strlen(str1 + 1);
            int nLen2 = strlen(str2 + 1);
            int nLen3 = strlen(str3 + 1);
            memset(dp, 0, sizeof(dp));
            int Max = -1;
            for (int j = 0; j <= nLen1; j++)
            {
                for (int k = 0; k <= nLen2; k++)
                {
                    if (j > 0 && str1[j] == str3[j + k])
                    {
                        dp[j][k] = max(dp[j][k], dp[j - 1][k] + 1);
                        Max = max(Max, dp[j][k]);
                    }
                    if (k > 0 && str2[k] == str3[j + k])
                    {
                        dp[j][k] = max(dp[j][k], dp[j][k - 1] + 1);
                        Max = max(Max, dp[j][k]);
                    }
                }
            }
            if (nLen3 == Max)
            {
                printf("Data set %d: yes
    ", i);
            }
            else
            {
                printf("Data set %d: no
    ", i);
            }
        }
        return 0;
    }

    方法二:

    #include <stdio.h>
    #include <string>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int dp[205][205];
    
    int main()
    {
        int N;
        scanf("%d", &N);
        char str1[205], str2[205], str3[405];
        for (int i = 1; i <= N; i++)
        {
            cin>>str1+1>>str2+1>>str3+1;
            int nLen1 = strlen(str1 + 1);
            int nLen2 = strlen(str2 + 1);
            int nLen3 = strlen(str3 + 1);
            memset(dp, 0, sizeof(dp));
            int Max = -1;
            dp[0][0] = 1;
            for (int j = 0; j <= nLen1; j++)
            {
                for (int k = 0; k <= nLen2; k++)
                {
                    if (j > 0 && str1[j] == str3[j + k] && dp[j - 1][k])
                    {
                        dp[j][k] = 1;
                    }
                    if (k > 0 && str2[k] == str3[j + k] && dp[j][k - 1])
                    {
                        dp[j][k] = 1;
                    }
                }
            }
            if (dp[nLen1][nLen2])
            {
                printf("Data set %d: yes
    ", i);
            }
            else
            {
                printf("Data set %d: no
    ", i);
            }
        }
        return 0;
    }
  • 相关阅读:
    微信支付:redirect-uri参数错误 的解决办法
    PHP计划任务之关闭浏览器后仍然继续执行的函数
    windows7安装phpnow Apache非管理员权限不能操作Windows NT服务的解决方法
    php 本周开始时间和结束时间;本月开始时间结束时间;上月开始时间结束时间
    图片预览
    生成随机验证码(记得下载字体文件啊)
    git ,github实际操作指南
    git,github初识命令
    博客园项目(BBS)
    django之分页器
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3163693.html
Copyright © 2011-2022 走看看