[Speaking algorithm small class] Find the length of the longest common subsequence of two sequences, and give any solution that meets the conditions (DP)

Insert picture description here

The longest common subsequence
is a sequence X=(x_0,x_1,…,x_(m-1)), if the sequence Y=(y_0,y_1,…,y_(k-1)), when there is a strictly increasing The subscript sequence (i_0,i_1,…,i_(k-1) ), so that ∀j∈[0,k-1], j∈Z, both x_(i_j )=y_j, then the sequence Y is the sequence of X Subsequence.
Obviously, the subsequence can be obtained by removing several items from the original sequence (and not removing one of them).
If the sequence Z is a subsequence of A and B, then Z is said to be a common subsequence of A and B. When Z contains the largest number of items, it is called the longest common subsequences (LCS) of A and B.

Now request: find the longest common subsequence of two given sequences A and B (give the length of the solution and the solution itself).
Solution
Find the length of the longest common subsequence.
Set A=(a_0,a_1,…,a_(m-1) ), B=(b_0,b_1,…,b_(n-1) ), Z=(z_0, z_1,...,z_(k-1)) is the longest common subsequence of A and B. We have:

[1] If a_(m-1)=b_(n-1), then
(1) z_(k-1)=a_(m-1)=b_(n-1).
(2) (z_0,z_1,…,z_(k-2)) is the best of (a_0,a_1,…,a_(m-2)) and (b_0,b_1,…,b_(n-2)) Long common subsequence.

[2] If a_(m-1)≠b_(n-1), and z_(k-1)≠a_(m-1), then
(1)(z_0,z_1,...,z_(k-1) ) Is the longest common subsequence of (a_0,a_1,...,a_(m-2)) and (b_0,b_1,...,b_(n-1)).

[3] If a_(m-1)≠b_(n-1), and z_(k-1)≠b_(n-1), then
(1)(z_0,z_1,...,z_(k-1) ) Is the longest common subsequence of (a_0,a_1,...,a_(m-1)) and (b_0,b_1,...,b_(n-2)).

It is not difficult to find that the situations [1] and [2] [3] are mutually exclusive respectively. And, when the situation [1] is not true, at least one situation [2] [3] is true, and it can be true at the same time.

When searching for the common subsequence of A and B, there are two cases to deal with:
[1] If a_(m-1)=b_(n-1), then recursive search (a_0,a_1,...,a_(m-2) )) And (b_0,b_1,...,b_(n-2)) a longest common subsequence.
[2] If a_(m-1)≠b_(n-1), two cases need to be divided:
[2.1] Recursive search (a_0,a_1,...,a_(m-2)) and (b_0,b_1) ,...,B_(n-1)) is a longest common subsequence.
[2.2] Recursively find a longest common subsequence of (a_0,a_1,...,a_(m-1)) and (b_0,b_1,...,b_(n-2)).
With the recursive (forward, such as from 1 to n) or recursive (reverse, such as from n to 1) solution, it is easy to write the state transition equation of dynamic programming:
let d_(i, j) be The length of the longest common subsequence of the sequence (a_0, a_1,..., a_(i-1)) and (b_0, b_1,..., b_(j-1)).
Add the initial condition d_(0, 0)=0 as the recursive boundary. Thus:
d_(i, j)=d_(i-1, j-1)+1,a_(i-1)=b_(j-1)
d_(i, j)=max⁡(d_(i, j -1), d_(i-1, j) ), a_(i-1)≠b_(j-1) The
final result is d_(m, n).
Time complexity: O(mn).
Given a specific longest common subsequence
After obtaining the length of the longest common subsequence, give a such longest common subsequence:
[1] Let i=m, j=n.
[2] Loop the following parts until the longest common subsequence of length d_(m, n) is constructed:
[2.1] If d_(i, j)=d_(i-1, j), then a_(i-1)≠b_(j-1), the terms a_(i-1), a_(j-1) are both Does not belong to any one of the longest common subsequences.
Let i=i-1.
[2.2] If d_(i, j)=d_(i, j-1), then a_(i-1)≠b_(j-1), the terms a_(i-1), a_(j-1) are both Does not belong to any one of the longest common subsequences.
Let j=j-1.
[2.3] When both [1] and [2] are not true, it means d_(i, j)≠d_(i-1, j) and d_(i, j)≠d_(i, j-1).
According to the state transition equation, there must be
d_(i, j)=d_(i-1, j-1)+1 and
a_(i-1)=b_(j-1). Add any of a_(i-1), a_(j-1) to the sequence (sequence length+1).
[3] If the sequence is filled from left to right, the sequence must be reversed to get the longest common subsequence.
If you fill in from right to left, you have already got the longest common subsequence.

Guess you like

Origin blog.csdn.net/COFACTOR/article/details/111184821