跟着編程之美學算法——最長公共子序列


最長公共子序列是一個很經典的動態規划問題,最近正在學習動態規划,所以拿來這里再整理一下。

這個問題在《算法導論》中作為講動態規划算法的例題出現。

 

動態規划,眾所周知,第一步就是找子問題,也就是把一個大的問題分解成子問題。這里我們設兩個字符串A、B,A = "a0, a1, a2, ..., am-1",B = "b0, b1, b2, ..., bn-1"。

(1)如果am-1 == bn-1,則當前最長公共子序列為"a0, a1, ..., am-2"與"b0, b1, ..., bn-2"的最長公共子序列與am-1的和。長度為"a0, a1, ..., am-2"與"b0, b1, ..., bn-2"的最長公共子序列的長度+1。

(2)如果am-1 != bn-1,則最長公共子序列為max("a0, a1, ..., am-2"與"b0, b1, ..., bn-1"的公共子序列,"a0, a1, ..., am-1"與"b0, b1, ..., bn-2"的公共子序列)

如果上述描述用數學公式表示,則引入一個二維數組c[][],其中c[i][j]記錄X[i]與Y[j]的LCS長度,b[i][j]記錄c[i][j]是通過哪一個子問題的值求得的,即,搜索方向。

這樣我們可以總結出該問題的遞歸形式表達:

recursive formula

按照動態規划的思想,對問題的求解,其實就是對子問題自底向上的計算過程。這里,計算c[i][j]時,c[i-1][j-1]、c[i-1][j]、c[i][j-1]已經計算出來了,這樣,我們可以根據X[i]與Y[j]的取值,按照上面的遞推,求出c[i][j],同時把路徑記錄在b[i][j]中(路徑只有3中方向:左上、左、上,如下圖)。

flow

計算c[][]矩陣的時間復雜度是O(m*n);根據b[][]矩陣尋找最長公共子序列的過程,由於每次調用至少向上或向左移動一步,這樣最多需要(m+n)次就會i = 0或j = 0,也就是算法時間復雜度為O(m+n)。

一下是代碼實現:

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string>
  4 #include <string.h>
  5 
  6 using namespace std;
  7 
  8 void LCS_Print(int **LCS_Direction, char *str, int row, int column)
  9 {
 10     if(str == NULL)
 11         return;
 12 
 13     int nLen1 = strlen(str);
 14 
 15     if(nLen1 == 0 || row < 0 || column < 0)
 16         return;
 17     
 18     if(LCS_Direction[row][column] == 1)
 19     {
 20         if(row > 0 && column > 0)
 21             LCS_Print(LCS_Direction, str, row - 1, column - 1);
 22         printf("%c ", str[row]);
 23     }
 24     else if(LCS_Direction[row][column] == 2)
 25     {
 26         if(row > 0)
 27             LCS_Print(LCS_Direction, str, row - 1, column);
 28     }
 29     else if(LCS_Direction[row][column] == 3)
 30     {
 31         if(column > 0)
 32             LCS_Print(LCS_Direction, str, row, column - 1);
 33     }
 34 }
 35 
 36 int LCS(char *str1, char *str2)
 37 {
 38     if(str1 == NULL || str2 == NULL)
 39         return 0;
 40 
 41     int nLen1 = strlen(str1);
 42     int nLen2 = strlen(str2);
 43 
 44     if(nLen1 <= 0 || nLen2 <= 0)
 45         return 0;
 46 
 47     // 申請一個二維數組,保存不同位置的LCS值
 48     int **LCS_Length = new int*[nLen1];
 49     // 申請一個二維數組,保存公共序列的位置
 50     int **LCS_Direction = new int*[nLen1];
 51     for(int i = 0; i < nLen1; i++)
 52     {
 53         LCS_Length[i] = new int[nLen2];
 54         LCS_Direction[i] = new int[nLen2];
 55     }
 56 
 57     for(int i = 0; i < nLen1; i++)
 58         LCS_Length[i][0] = 0;
 59     for(int i = 0; i < nLen2; i++)
 60         LCS_Length[0][i] = 0;
 61     
 62     for(int i = 0; i < nLen1; i++)
 63     {
 64         for(int j = 0; j < nLen2; j++)
 65         {
 66             LCS_Direction[i][j] = 0;
 67         }
 68     }
 69 
 70     cout<<"Init OK!"<<endl;
 71 
 72     for(int i = 0; i <nLen1; i++)
 73     {
 74         for(int j = 0; j < nLen2; j++)
 75         {
 76             if(i == 0 || j == 0)
 77             {
 78                 if(str1[i] == str2[j])
 79                 {
 80                     LCS_Length[i][j] = 1;
 81                     LCS_Direction[i][j] = 1;
 82                 }
 83                 else
 84                     LCS_Length[i][j] = 0;
 85             }
 86             else if(str1[i] == str2[j])
 87             {
 88                 LCS_Length[i][j] = LCS_Length[i - 1][j - 1] + 1;
 89                 LCS_Direction[i][j] = 1;
 90             }
 91             else if(LCS_Length[i - 1][j] > LCS_Length[i][j - 1])
 92             {
 93                 LCS_Length[i][j] = LCS_Length[i - 1][j];
 94                 LCS_Direction[i][j] = 2;
 95             }
 96             else
 97             {
 98                 LCS_Length[i][j] = LCS_Length[i][j - 1];
 99                 LCS_Direction[i][j] = 3;
100             }
101         }
102     }
103 
104     LCS_Print(LCS_Direction, str1, nLen1 - 1, nLen2 - 1);
105     cout<<endl;
106     int nLCS = LCS_Length[nLen1 - 1][nLen2 - 1];
107     for(int i = 0; i < nLen1; i++)
108     {
109         delete[] LCS_Length[i];
110         delete[] LCS_Direction[i];
111     }
112     delete [] LCS_Length;
113     delete [] LCS_Direction;
114     return nLCS;
115 }
116 
117 int main()
118 {
119     cout<<LCS("ABCBDAB", "BDCABA")<<endl;
120     return 0;
121 }
View Code

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM