1. Basic Concepts
1,x2,......,xm}, another sequence z={z 1, z2 、......, z< Sub style= "margin:0px; padding:0px ">k}, if there is a strictly incrementing small sequence of x <i1,i 2......,iK> so that for all j=1,2,......k, there xij = zj, then Z is the sub-sequence of x. for example: Z={b,c,d,b} is X={a,b,c,b,d,a, A sub-sequence of B}, the corresponding small mark is <2,3,5,7>. From the definition you can see that the elements directly in the subsequence are not necessarily contiguous.
Common sub-sequences: Given two sequences x and y, if Z is both a sub-sequence of X and a subsequence of y, then the sequence z is the common subsequence of x and Y. for example: X={a,b,c,b,d,a,b},y={b,d,c,a,b,a}, the sequence {b,c,a} is a common sub-sequence of X and Y, but not the longest common subsequence.
Maximum Common subsequence (LCS) problem Description: Given two sequences x={x1,x2,......,xm} and Y={y1,y2,......,yn}, find the longest common subsequence of x and Y.
2. Dynamic planning and solving process
1) Describe one of the longest common sub-sequences
If the sequence is shorter, the brute force method can be used to enumerate all the sub-sequences of X, and then check if it is a subsequence of Y and record the oldest sequence found. If the sequence is longer, this method requires a number of times, which is impractical.
Span style= "margin:0px; padding:0px; Color:rgb (255,0,255) ">lcs Optimal substructure theorem : set x={x1,x2,......,xm" and Y={y1,y2,......,yn} is two sequences and set z={z 1, z2 、......, Z k} is any of x and y LCS, then:
(1) If xm=yn, So zk=xm=y n, and Zk-1 is X m-1 and Yn-1.
(2) If Xm≠yn, then zk≠xm implication Z is xm-1 and Yn.
(3) If xm≠yn, then zk≠yn implies Z isan LCS of x m and yn-1.
The theorem illustrates that an LCS of two sequences also contains a two-sequence prefix for an LCS, i.e. the LCS problem has the optimal substructure properties.
2) a recursive solution
According to the sub-structure of LCS, to find the sequence x and Y LCS, according to Xm and yN is equal to judge, if Xm=yN produces a sub-problem, or two sub-problem. Set C[I,J] is the length of an LCS for sequence xI and YJ. If I=0 or j=0, that is, the length of a sequence is 0, the length of the LCS is 0. The recursive formula for the optimal substructure of the LCS problem is as follows:
3) Calculate the length of the LCS
Dynamic programming is used to calculate the solution from bottom up. The lcs_length of the solution process is given in the book, with two sequences as input. The length of the computed sequence is saved to a two-dimensional array c[m][n], and a two-dimensional array b[m][n] is introduced to preserve the optimal solution of the construction process. M and n represent the length of the two series, respectively. The pseudo-code for this procedure is shown below
Note that my code continues to use vector vectors to represent two-dimensional arrays:
The longest common subsequence problem://Given two two sequences x = <x1, x2, ....., xm> and y = <y1, y2, ....., Yn> To find the common subsequence of the maximum length of x and Y, you can use dynamic programming to solve//theorem 15.1 ( Optimal substructure of LCS)//Set x = <x1, x2, ...., xm> and y = <y1, y2, ...., yn> for two sequences, and set z = {z1, z2, ...., ZK} for any one of the X and y lcs//(1) if XM = yn, then zk = XM = yn, and Zk-1 is a Yn-1 of Xm-1 with lcs//(2) if xm! = yn, then ZK! = XM implication Z is a lcs//of Xm-1 and Y (3) if xm! = yn, then ZK! = yn, which implies that Z is a lcs//definition of x and Yn-1 C[i][j] is an LCS length for sequence XI and Yi, then there is the following recursive expression//when i = 0 or j = 0 when c[i][j]=0;//if I, J > 0 and Xi = Yi C[i ][J] = c[i-1][j-1] + 1//If I, J > 0 and Xi! = Yi c[i][j]= max (c[i][j-1], c[i-1][j])//Based on The recursive style of the polygon can write the bottom-up dynamic programming code//author:ugly_chen (Stallman) time:2014.12.15 0:51//if You has any problems, pls ask me and leave S ome words, loughing ... #include <iostream> #include <string> #include <vector> #include < Algorithm>using namespace Std;pair<vector<vector<int>>,vector<vector<int>>> LCS_ Length (string x, string y) {int m = x.size (); int n= Y.size ();vector<vector<int>> C (m+1, vector<int> (n+1, 0));vector<vector<int>> B (M+1, Vector<int> (n+1, 0)); for (int i = 1; I <= m; ++i) {c[i][0] = 0;} for (int j = 1; j <= N; ++j) {c[0][j] = 0;} for (int i = 1, i <= m; ++i) {for (int j = 1; j <= N; ++j) {if (x[i-1] = y[j-1]) {c[i][j] = C[i-1][j-1] + 1;b[ I][J] = 1;//Here I use 1 to indicate the arrow pointing to upper left,}else if (C[i-1][j] >= c[i][j-1]) {c[i][j] = c[i-1][j];b[i][j] = 2;//Here I use 2 to indicate arrows pointing up}else {C[i][j] = c[i][j-1];b[i][j] = 3;//Here I use 3 to indicate arrows pointing to left}}} Return Make_pair (c, b);} void Print_lcs (Vector<vector<int>>b, string x, int i, int j) {if (i = = 0 | | j = 0) return;if (b[i][j] = = 1) {pri Nt_lcs (b, X, i-1, j-1); cout << x[i-1]<< "";} else if (b[i][j] = = 2) Print_lcs (b, X, I-1, j); Elseprint_lcs (b, X, I, j-1);} int main () {string x = "Abcbdab"; string y = "Bdcaba"; int i = x.size (); int j = y.size ();vector<vector<int>> C = Lcs_length (x, y) .first;vector<vector<int>> B = LCS_length (x, y). Second;cout << "The LCS is:" << c[x.size ()][y.size ()] << Endl;print_lcs (b, X, I, j); Retu RN 0;}
Introduction to Algorithms--------------LCS issues (longest common child series)