Dynamic programming -- Examples

Problem solving ideas of dynamic planning:
There are four steps:
1 create a new one-dimensional array or two-dimensional array, and determine the type of array according to the variables given in the question.
2 if it is a one-dimensional array, determine the value of the first element of the array.
If it is a two-dimensional array, determine the values of the first row and first column of the array.
3 find out the state transition equation.
4 return results.
Example 1: find the maximum discontinuous subsequence
Let L = < A1, A2,..., an > be a sequence of n different real numbers, and the increasing subsequence of L is such a subsequence Lin = < AK1, ak2,..., AKM >, where K1 < K2 <... < km and AK1 < ak2 <... < AKM. Find the maximum value of m.
Idea: first, set an array temp[i] to store the longest subsequence of I. Secondly, initialize temp[i]={1}, and then find the state transition equation: if for (J = 1; j<i; J + +) when arr [i] > arr [J], temp[i]=max(temp[i],temp[j]+1) when arr [i] < arr [J], then temp[i] is not updated

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
//test data
/*
4
2 1 3 50
*/
//  {2,3,50} test result: 3

//Longest discontinuous subsequence problem
int main()
{
    int n;
    int arr[101]; //Define array storage sequence
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    int temp[101]; //The first step of dynamic programming is to establish a one-dimensional array
   for(int i=1;i<=n;i++) //Initialize element
    temp[i]=1;
    for(int i=2;i<=n;i++)    //state transition 
    {
        for(int j=1;j<i;j++)
        {
            if(arr[i]>arr[j])
            {
                temp[i]=max(temp[i],temp[j]+1);

            }
        }
    }
    //Find the maximum value of temp array
    int maxt=0;
    for(int i=1;i<=n;i++)
    {
        if(maxt<temp[i])
        {

            maxt=temp[i];
        }
    }
    cout<<maxt<<endl;
    return 0;
}

Example 2: find the sum of continuous maximum subsequences
Given an integer array arr [], find a continuous subarray with the largest sum (the subarray contains at least one element), and return its maximum sum
Idea: first set an array temp[i] to store and; temp[1] is initialized to arr[1]; When temp[i-1] + arr [i] > arr [i], temp = temp[i-1] + arr [i], otherwise temp[i]=arr[i];
code:

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

/*test data
 Input: 9
-2 1 -3 4 -1 2 1 -5 4

Output: 6

Explanation: continuous subsequence {4 - 1 2 1} and maximum, 6
*/

//Maximum subsequence sum

int main()
{

    int n;
    int arr[101];  //Store original array
    int temp[101]; //Storage and
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    temp[1]=arr[1]; //initialization
    for(int i=2;i<=n;i++)   //state transition 
    {

        if(temp[i-1]+arr[i]>arr[i])
            temp[i]=temp[i-1]+arr[i];
        else
            temp[i]=arr[i];
    }
    //Find the maximum value of temp [] array
    int maxand=temp[1];
    for(int i=2;i<=n;i++)
    {
        if(temp[i]>maxand)
            maxand=temp[i];
    }
    cout<<maxand<<endl;
    return 0;

}

Example 3: maximum common subsequence of two strings
Given two strings, the Longest Common Sequence of the two strings is solved. For example, string 1: BDCABA; String 2: ABCBDAB
Then the length of the longest common subsequence of the two strings is 4, and the longest common subsequence is BCBA
Idea: set a two-dimensional array dp[i][j] to represent the longest subsequence of coordinates (i,j). Initialize the first row and column. If x[i]=y[j], dp[i][j]=dp[i-1][y-1]+1;
Otherwise, dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
code:

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

//Maximum common subsequence
int main()
{
    string x;
    string y;
    cin>>x;
    cin>>y;
    int dp[21][21];
    int len1=x.length();
    int len2=y.length();
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=len1;i++)
    {

        for(int j=1;j<=len2;j++)
        {
           if(x[i-1]==y[j-1])
           {
               dp[i][j]=dp[i-1][j-1]+1;
           }
           else
           {

               dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
           }
        }
    }
    cout<<dp[len1][len2]<<endl;
    int k=0;
    char temp[101];
    int p=len1-1;
    int q=len2-1;
    while(p>=0&&q>=0)
    {

        if(dp[p+1][q+1]==dp[p][q]+1&&x[p]==y[q])
        {
            temp[k++]=x[p];
            p--;
            q--;
        }
        else if(dp[p+1][q+1]==dp[p][q+1])
        {
            p--;
        }
        else
        {
            q--;
        }
    }
    for(int i=k-1;i>=0;i--)
    {
        cout<<temp[i]<<" "<<endl;
    }
    return 0;

}


Example 4: change problem
Problem Description:
There are a pile of coins with denominations of 1,2,5,11,20,50. How many coins do you need to find the change with a total value of N units
**Idea: * * create a two-dimensional array, the ordinate represents the face value of coins, and the abscissa represents the change of j units. Initialize the first row and the first column. For each coin, if this coin is used, dp[i][j]=dp[i][j-p[i]]+dp[j], otherwise dp[i][j]=dp[i-1][j];
code:

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
/*Test sample
3
1 3 4
4
*/
// Output: 3

//Change problem

/*
In the first line, enter the number of face values n
 Enter face value arr []
On the third line, enter the amount of change to be found
*/
int main()
{

    int n;
    int p;
    int arr[101];
    int dp[21][21];
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>arr[i];
    }
    cin>>p;
    memset(dp,0,sizeof(dp));
    for(int i=1; i<=n; i++)
    {
        dp[i][0]=1;
    }
    for(int i=0; i<=p; i++)
    {
        if(i%arr[1]==0)
            dp[1][i]=1;
        else
            dp[1][i]=0;
    }
    for(int i=2; i<=n; i++)
    {
        for(int j=1; j<=p; j++)
        {

            if(j>=arr[i])
            {
                dp[i][j]=dp[i-1][j]+dp[i][j-arr[i]];
            }
            else
            {

                dp[i][j]=dp[i-1][j];
            }
        }
    }
    cout<<dp[n][p]<<endl;
    return 0;

}

Tags: Dynamic Programming

Posted on Tue, 07 Sep 2021 21:10:37 -0400 by herreram