The longest common subsequence, longest common subsequence,lcs. A string that is formed by deleting any character, without requiring continuous, attention, and the longest common substring.
Application of LCS: Paper check weight, graph similarity comparison, gene sequence comparison and so on.
Brute Force Solution:
The sub-sequences of x and Y strings are calculated separately, and then the search comparison is made, which is easy to get the complexity of the algorithm O (2^m 2^n), which is obviously undesirable.
Dynamic planning:
has two strings X[1....M],Y[1....N] to find its longest common substring
Assuming that Xi Yi is the first character of the number of two strings starting from 1, if Xm=yn, then XM must be in the longest common sub-sequence, i.e.
Set Z as the longest common substring, then z = LCS (Xm,yn) = LCS (Xm-1, Yn-1) + Xm;
If not equal, then Z = Max{lcs (Xm-1,yn), LCS (xm,yn-1)};
Using a two-dimensional array c[m][n], C[i][j] records the length of the longest common subsequence of the sequence Xi and Yi, when I or J equals 0 o'clock, the common subsequence length is 0, which is c[i][j]=0;
So:
Implementation code:
If the longest increment sequence is required, the original sequence can be sorted to find the longest common subsequence of two sequences.
The longest common sub-sequence of string algorithm