Dynamic programming solution for the longest common subsequence (LCS) problem (with printable LCS complete code)

Source: Internet
Author: User

I. Dynamic Programming method

Often encountering complex problems cannot be easily decomposed into several child problems, but a series of child problems will be decomposed. The problem solving time can be increased by the scale of the problem by simply using the method of decomposing the big problem into a child problem and synthesizing the solution of the problem.

In order to save time for repeating the same sub problem, an array is introduced, whether or not they are useful for the final solution, and the solution of all the child problems is in the array, which is the basic method of dynamic programming.

Second, the question: the longest common character subcode sequence (LCS) for two character sequences

Problem Description: The subsequence of a character sequence is a sequence of characters that is formed from a given sequence of characters that is arbitrarily (not necessarily continuous) to remove several characters (which may not be removed). The given sequence of characters is x= "X0,x1,...,xm-1", the sequence y= "Y0,y1,...,yk-1" is a subsequence of x, and there is a strictly ascending subscript sequence of x <i0,i1,...,ik-1>, which makes for all j=0,1,...,k-1, has xij=yj. For example, x= "Abcbdab", y= "bcdb" is a subsequence of X.

Third, analysis

Consider how the longest common subsequence problem can be decomposed into sub problems, set a= "A0,a1,...,am-1", b= "B0,b1,...,bm-1", and z= "Z0,z1,...,zk-1" as their longest common subsequence. It is not difficult to prove the following nature:

(1) If am-1=bn-1, then zk-1=am-1=bn-1, and "Z0,z1,...,zk-2" is a longest common subsequence of "a0,a1,...,am-2" and "b0,b1,...,bn-2";

(2) If the am-1!=bn-1 is zk-1!=am-1, the implication of "Z0,z1,...,zk-1" is a longest common subsequence of "a0,a1,...,am-2" and "b0,b1,...,bn-1";

(3) If the am-1!=bn-1 is zk-1!=bn-1, the implication "z0,z1,...,zk-1" is one of the longest common subsequence of "a0,a1,...,am-1" and "b0,b1,...,bn-2".

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.