Dynamic Planning and corresponding instances

Source: Internet
Author: User

Dynamic programming)The whole problem is solved by combining the sub-problems. The programming is a planning, and DP is applicable to the problem that sub-problems are not independent (the two sub-problems do not share resources is independent, that is, each subproblem contains a public subproblem. DP only solves the subproblem once and saves the value in a table to avoid repeated solutions to the same subproblem. DP is usually used for optimization. It is usually a problem with many feasible solutions. Each solution contains a value and we hope to find an optimal solution (maximum or minimum ).

The general design steps of DP are as follows:

1. describe the structure of the Optimal Solution

2. recursively define the value of the Optimal Solution

3. Calculate the optimal value based on the bottom-up method

4. Construct the optimal solution with computing results

DP nature: it is necessary to determine whether a problem can be solved by using DP. If the optimal solution of a problem contains the optimal solution of the subproblem, that is, the problem has the optimal sub-structure nature, we are prompted that using DP may solve the problem. When a recursive algorithm continuously calls the same problem, this optimal problem includes overlapping subproblems. At the same time, we can use DP to solve the problem.

The following is a simple DP small example:

Digital triangle Problems, Problem description:

If you enter a digital triangle, the number of integers in each row corresponds to the number of rows. You need to find a path from the top to the bottom of the triangle to maximize the sum of the numbers in the path and output the maximum value, the number of triangle rows is less than 100. Each step in the path can only go down or right. The following is an example of a problem:


Find the maximum solution of the above example?

Analysis: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yc/K9sr9vt2/ydLU08PSu7j2tv7OrMr91 + m9 + NDQw + jK9qOsvLQ8L3A + CjxwcmUgY2xhc3M9 "brush: java; "> D [I] [j] The number of j in row I goes from D [I] [j] to D [I + 1] [j] Or D [I + 1] [j + 1] Max [I] [j] indicates the sum of the optimal path numbers in the path from D [I] [j] to the end.

If there is only one row, Max [I] [j] = D [I] [j], otherwise, Max [I] [j] = max (Max [I + 1] [j], Max [I + 1] [j + 1]) + D [I] [j];


The following shows the recursive DP:

# Include
 
  
# Include
  
   
Using namespace std; const int num = 101; int D [num] [num]; int n; int Max [num] [num]; int main () {int I, j; cin> n; for (I = 1; I <= n; I ++) for (j = 1; j <= I; j ++) cin> D [I] [j]; for (int k = 1; k <= n; k ++) max [n] [k] = D [n] [k]; for (int I = n-1; I> = 1; I --) {for (int j = 1; j <= I; j ++) Max [I] [j] = max (Max [I + 1] [j], max [I + 1] [j + 1]) + D [I] [j];} cout <
   
    
The output result is: 30.
    

The time complexity o (n ^ 2) and space complexity o (n ^ 2) of the preceding algorithms are a waste of memory space, it will not be fully utilized. When the number of rows is large, it is a waste of time. We can store the solution of each calculated subproblem in a one-dimensional array, overwriting the write, the final solution is the Header element of the array. The space complexity is o (n ).

Demo:


That is, the solution.

# Include
     
      
# Include
      
       
Using namespace std; const int num = 101; int D [num] [num]; int n; int * Max; int main () {int I, j; cin> n; for (I = 1; I <= n; I ++) for (j = 1; j <= I; j ++) cin> D [I] [j]; Max = D [n]; for (int I = n-1; I> = 1; I --) {for (int j = 1; j <= I; j ++) Max [j] = max (Max [j], Max [j + 1]) + D [I] [j];} cout <
       
        
Longest public substring:
        

The two strings s1 and s2 use maxlen (I, j) to represent the I characters on the left of s1, and the maximum length of the common subsequence of j characters on the left of s2. (Resolution status)

If either of the two substrings is empty, the length of the common substrings is 0. The Code is as follows:

#include 
         
          #include #include 
          
           #include 
           
            char s1[100];char s2[100];int maxlen[100][100];using namespace std;int main(){    while(cin>>s1>>s2){        int len1 = strlen(s1);        int len2 = strlen(s2);        int i,j;        for(i=0;i<=len1;i++)            maxlen[i][0] = 0;        for(j=0;j<=len2;j++)            maxlen[0][j] = 0;        for(i=1;i<=len1;i++){            for(j=1;j<=len2;j++){                if(s1[i-1]==s2[j-1])                    maxlen[i][j] = maxlen[i-1][j-1]+1;                else                    maxlen[i][j] = max(maxlen[i-1][j],maxlen[i][j-1]);            }        }         cout << maxlen[len1][len2] << endl;    }    return 0;}
           
          
         
The longest public substring is not required. The substring is continuous.


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.