NYOJ746 Integer Division

This question is an interval DP topic. I have done several interval DPs. Speaking of which, it is just the form of DP in the interval. The core idea is to think of transfer -> planning.

The meaning of the question is to add m titles in the middle of n digits, so that the final product is the largest.

The state transition equation is as follows:

dp[ i ][ j ]=max( dp[ i ][ j ],dp[ k ][ j - 1]*a[ k + 1][ i ])

a[ i ][ j ] represents the number composed of the i-th to the j-th bit, which needs to be preprocessed.

Let's talk about the transfer equation, there are no more than two cases, plus or not.

No title is added to the k position, it is dp[ i ] [ j ],

If the title is added, it is the result of multiplying the number consisting of bits k+1 to i by dp [ k ][ j - 1] (the maximum value of k bits plus j-1 multiplication signs).

Taking the maximum value of the above two forms the transition equation.

code show as below:

 

#include<iostream>  
#include<string.h>  
#include<algorithm>  
using namespace std;  
#define ll long long 
ll dp[20][20],f,a[25][25];  
int main()  
{  
    int t,n,m,i,j,b[25];  
    char s[25],k;  
    cin>>t;  
    while(t--)  
    {  
        cin>>s>>m;  
        n=strlen(s);  
        memset(dp,0,sizeof(dp));  
        for(i=0;i<n;i++)  
            b[i]=s[i]-'0';  
        for(i=0;i<n;i++)  
        {  
            f=0;  
            for(j=i;j<n;j++)  
            {  
                f=f*10+b[j];  
                a[i][j]=f;  
            }  
        }  
        for(i=0;i<n;i++)  
            dp[i][0]=a[0][i];//没有乘号时的dp值。  
        for(i=0;i<n;i++)  
            for(k=0;k<i;k++)  
                for(j=1;j<m;j++)  
                    dp[i][j]=max(dp[i][j],dp[k][j-1]*a[k+1][i]);  
        cout<<dp[n-1][m-1]<<endl;  
     }  
     return 0;  
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325293951&siteId=291194637