The reason why this question cannot be viewed at the beginning is that it misunderstood what is the longest public subsequence and thought it was a problem. In fact, if you understand what is the longest public subsequence, this solves half of the problems.
What is the longest common subsequence?
What is the longest common subsequence? For example, if a sequence S is a subsequence of two or more known sequences and is the longest of all conforming sequences, S is the longest common subsequence of known sequences.
Note:
Longest Common substring and longest common substring
The difference between Longest Common substirng and longest common subsequence (LCS) Is that a substring is a continuous part of a string, sub-sequences do not change the sequence, but remove any elements from the sequence to obtain a new sequence. That is to say, the positions of Characters in the sub-string must be continuous, subsequences do not need to be consecutive.
The longest common subsequence is illustrated as follows:
First, we must first find a process to construct the optimal solution:
Then let's briefly talk about the entire process of dynamic planning (General Algorithm ):
In this topic, I want to solve L [7th], then I first find the mark of column 6th in row in the table and find it is an upward arrow, description: L [] = L []. Now I find L [6 ., 6]. It is marked with an arrow in the upper left corner, indicating that a is included in the solution array and added to the solution array, then scale down the problem to L [5, 5]. Then let's look at L [5, 5]...
As I and j change in L [I, j], the scale of this problem gradually decreases until we encounter L [I, j] = 0: stop searching.
In addition, we construct the table above. During the construction, we construct the table from the bottom to the top, but we solve the problem from top to bottom.
The specific pseudo-code will not be explained. This is relatively simple, as long as the idea is OK.
Summary:
The arrows marked in this question are interesting and can clearly see the changing trend of the problem scale.
Dynamic Programming Method -- longest common subsequence Problem