Problem Description: Given 2 sequences, X = x1,..., xm and Y = Y1,..., yn, find a common subsequence whose length is maximum. Subsequence neednot being consecutive, but mustis in order.
Program Ideas:
Using recursive thinking can solve this problem. Set the input two substrings to x[0...m-1] and y[0...n-1],l (X[0...m-1], y[0...n-1]) to the longest common substring length of X and Y. In two cases:
1. If the last character of the substring matches (that is, x[m–1] = = y[n–1]), then
L (X[0...m-1], y[0...n-1]) = 1 + L (x[0...m-2], y[0...n-2])
2. If the last character of the substring does not match (i.e. x[m–1]! = y[n–1]), then
L (X[0...m-1], y[0...n-1]) = Max (L (x[0...m-2], y[0...n-1]), L (X[0...m-1], y[0...n-2]))
However, in the case of recursion, many of the sub-conditions will be repeated several times, so you can use dynamic programming to optimize, here you can use the method of drawing tables to record the solution of the problem, for later calculation use. The table here records the length of the longest common substring.
For example: the longest common substring table for "abcfgr" and "ACKWGR" is
the value in the lower right corner is the longest common substring length of two substrings.
If you need to output the longest common substring of two substrings, you need extra space to save each character. The steps are as follows:
- Constructs the longest common substring length table l[][];
- Constructs a string s with a length of l[m][n];
- From l[m][n] This position begins traversing l[][] until M or n is 0, for each element in l[][] l[i][j],
A) if x[i-1] = = Y[j-1], that is, this character is the longest common substring of a character, placed at the end of S, moving to the upper left of a grid, that is, i–1,j–1, continue to traverse.
b) If not, select a larger value on the left or top of the table, and move one grid, i.e. i–1 or j–1, to continue the traversal.
Based on the above example, the traversal path is given:
So the longest common substring of "abcfgr" and "ACKWGR" is "ACGR".
Algorithm pseudo-code:
LCS algorithm
Input: substring x, length of substring y,x m,y length n
Output: Longest common substring
LCS (x[], y[], M, N)
Begin
Create l[m + 1][n + 1]
For each i from 0 to M do
For each j from 0 to N do
If i = = 0 or J = = 0
L[I][J] = 0
Else if x[i–1] = = y[j–1]
L[I][J] = l[i–1][j–1] + 1
Else
L[I][J] = max (L[i–1][j], l[i][j–1])
Create Char C[l[m][n]]
For L[m][n] to l[0][p] or l[q][0]
If x[i-1] = = y[j–1]
Push this char to C
Skip to left up
Else
Skip to left or up which is larger
End
Algorithm Performance Analysis:
The main thing this algorithm does is to build the longest common substring table and then traverse the table. Table creation requires a single fill-in table entry, so the complexity is O (MN).
Algorithm job 6 Dynamic planning-the longest common substring problem