First, the longest common sub-sequence introduction
the subsequence of a sequence is the sequence that is obtained after deleting several elements in the sequence. For example, "ABCD" and "BDF" are all sub-sequences of "ABCDEFG".
Longest common subsequence (Longest Common subsequence, abbreviated LCS) problem : Given two sequences x and Y, the common subsequence with the largest x and y lengths is obtained. Example: x= "Abbcbde", y= "dbbcdb", LCS (x, y) = "BBCD"
Application scenario: string similarity ratio pair, gene alignment .
1. The optimal substructure principle of LCS
Make x=<x1,x2,..., xm> and Y=<y1,y2,..., yn> to two sequences, z=<z1,z2,..., zk> for any LCS of X and Y.
1. If Xm=yn, then Zk=xm=yn and Zk-1 are an LCS of Xm-1 and Yn-1.
2. If Xm≠yn, then ZK≠XM means Z is an LCS for Xm-1 and Y.
3. If Xm≠yn, then Zk≠yn means Z is an LCS of X and Yn-1.
2. Recursive type of optimal solution
C[I,J] Represents the length of the LCS of Xi and YJ.
Second, examples and analysis
For example, LCS that require a= "Abcbdab" and B= "Bdcaba":
Because the last "B" =? " A ", so the LCS (a, b) should originate from one of the LCS (A[:-1],B) and LCS (A,b[:-1]). It means that the last one is different, either remove the last one from a, or remove the last one from B.
Longest common sub-sequence (LCS)