最長公用子序列(Longest Common Sequence)

來源:互聯網
上載者:User
問題的定義: 子序列
X=(A, B, C, B, D, B) Z=(B, C, D, B)是X的子序例 W=(B, D, A)不是X的子序例 公用子序列
Z是序列X與Y的公用子序列如果Z是X的子序也是Y的子序列。 最長公用子序列(LCS)問題

輸入:X = (x1, x2, …, xn),Y = (y1, y2, …, ym)
輸出:Z = X與Y的最長公用子序列

蠻力法: 枚舉X的每個子序列Z; 檢查Z是否為Y的子序列; T(n)=O(m×2n)。

動態規劃:

最佳化子結構:
設X = (x1, x2, …, xn),Y = (y1, y2, …, ym) 是兩個序列,Z=(z1, z2, …, zn)是X與Y的LCS,我們有: 如果xm = yn, 則zk = xm = yn, Zk-1是Xm-1和Yn-1的LCS,即,LCSXY = LCSXm-1Yn-1+ (xm = yn)。 如果xm ≠ yn,且zk ≠ xm,則Z是Xm-1和Y的LCS,即LCSXY= LCSXm-1Y 如果xm ≠ yn,且zk ≠ yn,則Z是X和Yn-1的LCS,即LCSXY= LCSXYn-1

遞迴方程: C[i, j] = Xi與Yj 的LCS的長度 LCS長度的遞迴方程
C[i, j] = 0 , if i=0 or j=0 C[i, j] = C[i-1, j-1] + 1, if i, j>0 and xii = yjj C[i, j] = Max(C[i, j-1], C[i-1, j]), if i, j>0 and xii yjj

基本思想:
在知道C[i-1, j-1], C[i, j-1], C[i-1, j]的情況下,根據遞迴方程計算C[i, j]。

資料結構: C[0:m,0:n]: C[i,j]是Xi與Yj的LCS的長度 B[1:m,1:n]: B[i,j]是指標,指向計算C[i,j]時所選擇的子問題的最佳化解所對應的C表的表項

虛擬碼:

LCS-length(X, Y)    m = length(X);    n = length(Y);    For i = 1 To m Do         C[i,0] = 0;    For j = 1 To n Do         C[0,j] = 0;    For i = 1 To m Do        For j = 1 To n Do            If xi = yj Then                 C[i,j] = C[i-1,j-1] + 1;                B[i,j] = “”;            Else If C[i-1,j]>=C[i,j-1] Then                C[i,j] = C[i-1,j];                 B[i,j] = “↑”;            Else C[i,j] = C[i,j-1];                 B[i,j] = “←”;Return C and B.
C++代碼:
#include <iostream>#include <vector>#include <string>using namespace std;vector<vector<int>> b, c;void lcs(string str1, string str2, int len1, int len2){    b.resize(len1 + 1);    c.resize(len1 + 1);    for (int i = 0; i < len1 + 1; i++)    {        b[i].resize(len2 + 1, 0);        c[i].resize(len2 + 1, 0);    }    for (int i = 1; i <= len1; i++)    {        for (int j = 1; j <= len2; j++)        {            if (str1[i - 1] == str2[j - 1])            {                c[i][j] = c[i - 1][j - 1] + 1;                b[i][j] = 0;            }            else if(c[i - 1][j] >= c[i][j - 1])            {                c[i][j] = c[i - 1][j];                b[i][j] = 1;            }            else            {                c[i][j] = c[i][j - 1];                b[i][j] = -1;            }        }    }   }void PrintLCS(vector<vector<int>> b, string str1, int i, int j){    if (i == 0 || j == 0)        return;    if (b[i][j] == 0)    {        PrintLCS(b, str1, i - 1, j - 1);        cout << str1[i - 1] << " ";    }    else if (b[i][j] == 1)    {        PrintLCS(b, str1, i - 1, j);    }    else    {        PrintLCS(b, str1, i, j - 1);    }}int main(){       string str1 = "abcdef";    string str2 = "acef";    int len1 = str1.size();    int len2 = str2.size();    b.resize(len1 + 1);    c.resize(len2 + 1);    lcs(str1, str2, len1, len2);    cout << c[len1][len2] << endl;    PrintLCS(b, str1, len1, len2);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.