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数组的值,可以倒着从最后一步步推出每步的选择,进而确定出公共子序列中的元素。



  • 相关阅读:
    NAMESPACE
    所谓has a 和 is a
    C++ 的多重继承
    c# 与 c++ 编译
    初始化的顺序:和定义的顺序以及初始化函数都有关系。都要先定义的在前,后定义的在后。甚至连类的顺序都必须这样。
    关于转换
    隐藏
    第四章第四个例题(LRJ)
    初来扎到啊(觉得有些神圣尼)
    理解JS的执行环境
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246300.html
Copyright © 2011-2022 走看看