题意:给出一个数字串,把它划分成K + 1份,使其乘积最大.
类似矩阵连乘的DP 设dp[i][j][p]为区间[i, j]划分成p份的最大乘积.
那么dp[i][j][p] = max(dp[i][k][p - 1] * val[k + 1][j]) val[i][j]为数字串i到j的值.
base case: dp[i][j][1] = val[i][j]
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
const int MAX = 41;
double dp[MAX][MAX][7];
char num[MAX];
double val[MAX][MAX];
int main(int argc, char const *argv[]){
int N, K;
scanf("%d%d", &N, &K);
scanf("%s", num + 1);
for(int i = 1; i <= N; ++i){
val[i][i] = num[i] - '0';
for(int j = i + 1; j <= N; ++j){
val[i][j] = val[i][j - 1] * 10 + num[j] - '0';
dp[i][j][1] = val[i][j];
}
dp[i][i][1] = val[i][i];
}
++K;
for(int m = 1; m < N; ++m){
for(int i = 1; i < N && i + m <= N; ++i){
int j = i + m;
for(int p = 2; p <= K && p <= i + m; ++p){
for(int k = i; k < j; ++k){
dp[i][j][p] = max(dp[i][j][p], dp[i][k][p - 1] * val[k + 1][j]);
}
}
}
}
printf("%.0lf\n", dp[1][N][K]);
return 0;
}