zoukankan      html  css  js  c++  java
  • HDU Common Subsequence(最长公共子序列)

    Common Subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 32838    Accepted Submission(s): 14875


    Problem Description
    A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
    The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
     
    Sample Input
    abcfbc abfcab programming contest abcd mnp
     
    Sample Output
    4 2 0
     
    Source
     
    Recommend
    Ignatius   |   We have carefully selected several similar problems for you:  1087 1176 1003 1058 1069

    最长公共子序列问题,简单的动态规划

    1处纵轴比到f横轴比到第二个b,从前面序列看,自然为3,和上一位f对f相等
    2处横轴比到b,纵轴比到最后一个c,从序列上看,自然与纵轴上一个f相等,因为比到此处的cb处最多也就为2了。
    3处比到cc,加上上处bf自然为3
    反正看表多看几个特殊值感受下吧,感觉哪里没有完全明白

    #include <iostream>
    #include <cstring>
    using namespace std;
    int ans[1005][1005];
    int main()
    {
        char a[1005],b[1005];
    
        int i,j;
        while(cin>>a>>b){
        int len1=strlen(a);
        int len2=strlen(b);
    
        for(i=0;i<=len1;i++)
            ans[i][0]=0;
        for(j=0;j<=len2;j++)
            ans[0][j]=0;
    
            for(i=1;i<=len1;i++)
            {
               for(j=1;j<=len2;j++)
                {
                    if(a[i-1]==b[j-1])
                    {
                        ans[i][j]=ans[i-1][j-1]+1;
                    }
                    else
                    {
                        ans[i][j]=ans[i-1][j]>=ans[i][j-1]?ans[i-1][j]:ans[i][j-1];//为什么是上和左都取,因为这个横纵轴坐标是相互的,可互换
    
                    }
            }
        }
            cout<<ans[len1][len2]<<endl;
        }
        return 0;
    }
    

    在计算最优解的过程中,可以用另外一个二维数组来表示每次选择,用c[i][j]=1表示result[i][j]是由result[i-1][j-1]得来的,用c[i][j]=2表示result[i][j]是由result[i-1][j]得来的,由c[i][j]=3表示result[i][j]是由result[i][j-1]得来的。
    根据c数组的值,可以倒着从最后一步步推出每步的选择,进而确定出公共子序列中的元素。



  • 相关阅读:
    Unity3d Android SDK接入
    Unity打包IOS和Android以及之间的交互
    Gradle
    General error: 1205 Lock wait timeout exceeded; try restarting transaction的解决办法
    kubeadm init [ERROR ImagePull]: failed to pull image [k8s.gcr.io/kube-apiserver-amd64:v1.11.3]: exit status 1
    yum安装kubectl时报错
    PhpStorm激活码 2020年3月亲测可用
    Vmware 14 安装 Vmware Tools 并配置宿主机共享文件给Vmware中Centos7系统
    期权
    对冲-风险对冲
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256449.html
Copyright © 2011-2022 走看看