Dynamic Programming Method -- longest common subsequence Problem

Source: Internet
Author: User




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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.