zoukankan      html  css  js  c++  java
  • Combine String---hdu5727 &&& Zipper(LCS变形)

    题目链接:http://poj.org/problem?id=2192

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5707

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1501

    这三道题除了输入输出格式不一样,其他都一样,意思是给你三个字符串,问你能不能由前两个组成第三个,要按顺序;

    但是hdu5707和poj2192数据太水,直接判断字符个数,然后一个一个的判断先后顺序是否满足即可,但是这样是有bug的,例如cbe cadfg cabcdefg本来是No的,但是如果这样做的话就是yes;

    作为一个比赛题出这样数据我也是醉了,就这样让学妹们水过了;

    其实是最长公共子序列的一种变形;(刚开始的时候听别人这样说,我感觉怎么会呢,后来想想确实就是)

    hdu1501用那种方法是过不了的,我们可以用dp[i][j]表示a的前i个字符和b的前j个字符是否能组合成c的前i+j个字符;所以最后只需判断dp[L1][L2]是否为1即可;

    当c[i+j] = a[i] && c[i+j] = b[j] 时,dp[i][j]可以由dp[i-1][j]或者dp[i][j-1]的来,所以只要这两个有一个为真,dp[i][j]就为真;

    其他两种情况也是这样推得;

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <map>
    using namespace std;
    #define N 1010
    #define met(a, b) memset(a, b, sizeof(a))
    #define INF 0x3f3f3f3f
    #define LINF 10e16
    typedef long long LL;
    
    int dp[N][N];
    
    int main()
    {
        char a[N], b[N], c[N];
        int T, t = 1;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%s %s %s", a+1, b+1, c+1);
    
            printf("Data set %d: ", t++);
    
            met(dp, 0);
    
            int L1 = strlen(a+1), L2 = strlen(b+1), L3 = strlen(c+1);
    
            if(L1+L2 != L3)
            {
                puts("no");
                continue;
            }
    
            dp[0][0] = 1;
    
            for(int i=0; i<=L1; i++)
            {
                for(int j=0; j<=L2; j++)
                {
                    if(i==0 && j==0)continue;
    
                    if(c[i+j] == a[i] && c[i+j] == b[j])
                        dp[i][j] = dp[i-1][j] || dp[i][j-1];
                    else if(c[i+j] == a[i])
                    {
                        if(i == 0) dp[i][j] = 1;
                        else dp[i][j] = dp[i-1][j];
                    }
                    else if(c[i+j] == b[j])
                    {
                        if(j == 0) dp[i][j] = 1;
                        else dp[i][j] = dp[i][j-1];
                    }
                }
            }
            if(dp[L1][L2]) puts("yes");
            else puts("no");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    08-JS中table隔行换色
    07-JS中 li 排序
    HTML DOM 事件
    JavaScript 事件
    jQuery事件函数
    JQuery与JS对象相互转换
    jQuery中的选择器
    jQuery实现放大镜特效
    java线程(2016-4-7)
    Java 线程的转换及状态
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5796490.html
Copyright © 2011-2022 走看看