An introduction to Algorithms _ Dynamic programming _ the longest common sub-sequence

Source: Internet
Author: User

First, the concept of dynamic planning

Dynamic programming (programming) solves the whole problem by combining the solution of sub-problems. Divide-and-conquer refers to dividing the problem into some independent sub-problems, solving each sub-problem recursively, then merging the solution of the sub-problem to get the solution of the original problem, and, unlike this, the dynamic programming applies to the sub-problem is not independent situation, that is, each sub-problem contains the common sub-problem. In this case, the use of the division of the law will do a lot of unnecessary work, that is, to solve the common sub-problem. The dynamic programming algorithm solves each sub-problem only once and saves its results in a table, avoiding the recalculation of the answer each time each sub-problem is encountered.

Dynamic planning is typically applied to optimization problems. There are a number of possible solutions to such problems. Each solution has a value, and we want to find the solution with the optimal (maximum or minimum) value. This solution is called the "one" optimal solution of the problem, rather than the "definite" optimal solution, because there may be multiple solutions to the optimal value.

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

Ii. General steps for solving dynamic programming

The design of the dynamic programming algorithm can be divided into the following 4 steps:

1> describes the structure of the optimal solution.

2> defines the value of the optimal solution recursively.

The 3> calculates the value of the optimal solution in the bottom-up way.

4> an optimal solution is constructed from the calculated results.

Introduction to algorithms the description of dynamic programming in this book is detailed, with several examples of the code that uses the dynamic programming algorithm to solve the longest common sub-sequence:

#include <iostream> #include <vector> #include <utility>//To return two-dimensional arrays, using pairusing namespace Std;pair <int**,int**> lcs_length (const string &strx, const string &stry) {int nxlen = strx.length (); int nylen = Stry. Length ();//two-dimensional array allocation method//int** C = (int**) malloc ((Nxlen + 1) * sizeof (int*));//for (int i = 0; i<= nxlen; i++)//c[i] = ( int*) malloc ((Nylen + 1) * sizeof (int)), int** C = new Int*[nxlen + 1];for (int i = 0; i<= nxlen; i++) c[i] = new Int[nyle n + 1];int** B = new Int*[nxlen + 1];for (int i = 0; I <= nxlen; i++) b[i] = new Int[nylen + 1];for (int i = 0; I <= Nxlen; i++) {c[i][0] = 0;} for (int j = 0; J <= Nylen; j + +) {C[0][j] = 0;} for (int i = 0; I! = Nxlen; i++) {for (int j = 0; J! = Nylen; j + +) {if (strx[i] = = Stry[j]) {c[i + 1][j + 1] = C[i][j] + 1;  B[i + 1][j + 1] = 0; Description of the common element in LCS}else{if (c[i][j+1] >= c[i+1][j]) {c[i + 1][j + 1] = c[i][j + 1];   B[i + 1][j + 1] = 1; //! Shift Left}else{c[i + 1][j + 1] = c[i + 1][j]; B[i + 1][j + 1] =-1; //! Move Right}}}}pair<int**, int**> result (C, B); return result;} void Printallcase (pair<int**, int**> result, const string &strx, int i, int j) {if (i = = 0 | | j = 0) {return;} if (result.second[i][j] = = 0) {printallcase (result, Strx, i-1, j-1); cout << strx[i-1];} else if (result.second[i][j] = = 1) {printallcase (result, Strx, I-1, j);} Else{printallcase (result, Strx, I, j-1);}} int main () {string Strx = "10010101"; string stry = "010110110";p air<int**, int**> result = Lcs_length (Strx, stry); cou T << "The max length of LCS is" << result.first[strx.length ()][stry.length ()] << endl;cout << "Th E LCS is: ";    Printallcase (result, Strx, Strx.length (), stry.length ()); cout << Endl; return 0;}

This code has a reference to this blog: http://blog.csdn.net/houqd2012/article/details/39928159

But this blog code is a bit problematic, the problem is that in C + +, the first element of the array or string A is a[0], not a[1], and in the introduction of the algorithm, the basic pseudo-code in this book is the default first element is a[1], which will cause many problems, For example, in the case of the longest common subsequence, when x= "A", y= "B", the code in the above-mentioned blog has the result that the longest common subsequence length is 1, because x[1] and y[1] are equal, are null characters, which is obviously wrong, because they do not have a common subsequence, So the above code is improved on the basis of the original code, still thanks to the Yumbo Master code.

Writing code is an art, need to think about, the most wonderful thing is to turn their thoughts into the code that can be run.

It's late, darkness is coming

An introduction to Algorithms _ Dynamic programming _ the longest common sub-sequence

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.