Dynamic Planning of LCS

Source: Internet
Author: User
Overview

Dynamic Planning is effective when finding the optimal solution with many overlapping sub-problems. It combines the problem into a subproblem. In order to avoid multiple solutions to these subproblems, their results are gradually calculated and saved, from a simple problem until the entire problem is solved. Therefore, dynamic planning saves recursive results, so it does not take time to solve the same problem.

Dynamic Planning can only be applied to problems with optimal sub-structures. The optimal sub-structure means that the local optimal solution can determine the global optimal solution (for some problems, this requirement cannot be fully met, so some approximation is sometimes required ). Simply put, the problem can be resolved into sub-problems.

Longest Common subsequence (LCS ).

Question: If all the characters in string 1 appear in the order of the strings in another string,
Then, string 1 is called a substring of string 2.

Note that the character of a substring (string 1) must appear in string 2 consecutively.
Compile a function, enter two strings, calculate their longest public substrings, and print the longest public substrings.
For example, if two strings bdcaba and abcbdab are input, and both bcba and bdab are their longest common subsequences, the output length is 4 and any subsequence is printed. 1. describe an LCS given a sequence X = {x1, x2, X3 ,..., xm} for I = 0, 1, 2 ,..., m, which defines the I-th prefix of X as xi = {x1, x2, X3 ,..., XI }. For example, if X = {A, B, C, B, D, a, B}, X4 = {A, B, C, B}, and x0 is an empty sequence. LCS optimal sub-structure x = {x1, x2 ,..., xm} and Y = {y1, Y2 ,..., yn} is two sequences with Z = {Z1, Z2 ,..., ZK} is any LCS of X and Y. 1). If XM = YN, then zk = XM = YN and Zk-1 is the LCS of Xm-1 and Yn-1.2). If XM! = YN, then if ZK! = XM => Z is the LCS of Xm-1 and y.3). If XM! = YN, then if ZK! = YN => Z is the LCS of Yn-1 and x.2. a recursive solution for C [I, j] is the length of Xi and yjthen | ---- 0 if I = 0 or J = 0C [I, j] = | ---- C [I-1, J-1] + 1, if I, j> 0 & xi = YJ | ---- max {C [I, J-1], C [I-1, J]}, if I, j> 0 & Xi! = Yj3. calculate the LCS Length
LCS-LENGTH(X, Y)m <- X.lengthn <- Y.lengthfor(i <- 1 to m)    do c[i, 0] <- 0for(j <- 0 to n)    do c[0, j] <- 0for i <- 1 to m    do for j <- 1 to n       if xi==yj          c[i,j]=c[i-1, j-1]+1       else if c[i-1, j] >= c[i, j-1]          c[i,j]=c[i-1, j]       else           c[i, j]=c[i, j-1]return c 

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.