2016校招编程 lcs 动态规划

来源:互联网 发布:高中化学知识网络结构 编辑:程序博客网 时间:2024/06/16 17:30

问题

题目:[最长公共子序列]

思路

参考算法导论原文:
LetX=<x1,x2,...xm>and Y=<y1,y2,...yn> be subsequences, and letZ=<z1,z2,...zk> be any lcs of X and Y.
1.if xm=yn , then zk=xm=yn, and Zk1 is an LCS of Xm1 and Yn1
2. if xmyn , then zkxm implies that Z is an LCS of Xm1 and Y
3. if xmyn , then zkyn implies that Z is an LCS of X and Yn1

Let us define c[i][j] to be the length of an LCS of the sequences Xi and Yj
转移方程如下图:

c[i][j]=0,c[i1][j1]+1,max(c[i1][j],c[i][j1]),ifi==0,or j=0ifxi=yj and i>0 j>0ifxiyj and i>0 j>0(15.14)

代码

class LCS {public:    int findLCS(string A, int n, string B, int m) {        // write code here        if( !n || !m )            return 0;        std::vector< std::vector<int> > dp( n + 1, std::vector<int>( m + 1, int() ) );        for( int i = 1; i < n + 1; ++i ){            for( int j = 1; j < m + 1; ++j ){                dp[i][j] = (A[i-1]==B[j-1]) ? dp[i-1][j-1] + 1 : std::max( dp[i-1][j], dp[i][j-1] );            }        }        return dp[n][m];    }};

代码1

class LCS {public:    int findLCS(string A, int n, string B, int m) {        // write code here        if(!n || !m) return 0;        vector<vector<int>> c(n+1, vector<int>(m+1, int())); // c[i][j] be the length of lcs of x[0...i] and y[0...j]        for(int i = 0; i <=n; ++i) c[i][0] = 0;        for(int i = 0; i <=m; ++i) c[0][i] = 0;        for(int i = 1; i <= n; ++i){            for(int j = 1; j <= m; ++j){                c[i][j] = (A[i-1]==B[j-1])?c[i-1][j-1] + 1:max(c[i-1][j],c[i][j-1]);            }        }               return c[n][m];    }};

得到lcs的长度之后,考虑如何获得序列。
这点其实和我在复习DAG模型的动态规划差不多。
利用递归的办法,从最终状态出发。递归向前即可。

打印序列的时候需要引入辅助数组b[i][j],用来表明状态的转移。
此时对lcs的代码需要修改,在计算c[i][j]的时候,需要同时更新b[i][j]的值。

  • 当c[i][j] = c[i-1][j-1] + 1的时候,说明x[i]==b[j]==z[k]。所以,他们两任意一个都是lcs序列中的元素。
  • 当c[i][j] = c[i][j-1]的时候,说明x[i]==z[k],此时没有发生有效状态转移。需要判断c[i][j-1]是否为有效状态。因为只有有效的状态转移的元素才是lcs的元素。
  • 当c[i][j] = c[i-1][j]的时候,说明y[j]==z[k],此时没有发生有效状态转移。需要判断c[i-1][j]是否为有效状态。

代码(算法导论demo)

需要注意一点,辅助表的下标从1开始。字符串的下标从0开始。这点小心,写的时候错了。

#include <iostream>#include <vector>#include <string>typedef std::vector<std::vector<int>> TABLE;int lcs( std::string& A, int m, std::string& B, int n ,TABLE& c, TABLE& b ){    for(int i = 0; i <= m; ++i) c[i][0] = 0;    for(int i = 0; i <= n; ++i) c[0][i] = 0;    for(int i = 1; i <= m; ++i){        for(int j = 1; j <= n; ++j){            if(A[i-1] == B[j-1]){                c[i][j] = c[i-1][j-1] + 1;                b[i][j] = 2; // 有效的状态转移            }            else if( c[i][j-1] > c[i-1][j] ){                c[i][j] = c[i][j-1]; // zk != yn                b[i][j] = 1;            }            else{                c[i][j] = c[i-1][j]; // zk != xm                b[i][j] = 3;            }        }    }    return c[m][n];}void print_lcs(std::string& A, TABLE& b, int i, int j){    if(!i || !j) return ; // 此处为初始状态    else{        if(b[i][j] == 1) // c[i][j-1]            print_lcs(A, b, i, j-1);        else if(b[i][j] == 2){ // c[i-1][j-1]            print_lcs(A, b, i - 1, j - 1);            std::cout << A[i-1];        }        else{ // c[i-1][j]            print_lcs(A, b, i-1, j);        }    }}int main( void ){    std::string A("ABCBDAB");    std::string B("BDCABA");    int ans = 0;    int m = A.size();    int n = B.size();    if(!m || !n) ans = 0;    else{        TABLE c(m+1, std::vector<int>(n+1, int())); // C[i][i] is the length of lcs of x[0..i] and y[0...j]        TABLE b(m+1, std::vector<int>(n+1, int())); // b[i][j] is the construction of an optimal solution        ans = lcs(A, m, B, n, c, b);        std::cout << ans << std::endl;        for( int i = 0; i <= m; ++i ){            for( int j = 0; j <= n; ++j ){                std::cout << c[i][j] << " ";            }            std::cout << std::endl;        }        std::cout << "---------------------" << std::endl;        for( int i = 0; i <= m; ++i ){            for( int j = 0; j <= n; ++j ){                std::cout << b[i][j] << " ";            }            std::cout << std::endl;        }        print_lcs(A, b, m, n);        std::cout << std::endl;    }    return 0;}
0 0
原创粉丝点击