This commit is contained in:
Zengtudor 2024-10-17 21:30:20 +08:00
parent 70b5de109d
commit 4d520855f7
2 changed files with 62 additions and 0 deletions

View File

@ -93,4 +93,20 @@ dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
dp[i][j]=dp[i][j-1]+(a[i]!=a[j]);
dp[i][i]=1;
*/
```
### [oj8782](http://noi.openjudge.cn/ch0206/8782)
```cpp
/*
区间动态规划解题步骤:
1.根据问题推测dp[i][j]的含义
问题长度为N的数字串要求选手使用K个乘号将它分成K+1个部分
dp[i][j]的含义长度为i的数字串要求选手使用j个乘号将它分成j+1个部分
2.根据规则推出dp[i][j]的状态转移公式
在1-i之间找一个中间值k将1-i这一段分成两段1-k(有j-1个乘号)和k+1~i(没有乘号)
dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]);
3.边界问题比如设定dp[0][0],dp[0][j],dp[i][0]初始值)
num[i][j]
*/
```

46
src/oj8782/oj8782.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <bits/stdc++.h>
using namespace std;
/*
1.dp[i][j]
N的数字串使K个乘号将它分成K+1
dp[i][j]i的数字串使j个乘号将它分成j+1
2.dp[i][j]
1-i之间找一个中间值k1-i这一段分成两段1-k(j-1)k+1~i()
dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]);
3.dp[0][0],dp[0][j],dp[i][0]
num[i][j]
*/
#define NV(v)#v<<" : "<<(v)
using ll = long long;
const ll max_n = 40+5;
ll n,k, num[max_n][max_n],dp[max_n][max_n];
char c[max_n];
int main(){
cin>>n>>k;
for(ll i{1};i<=n;i++){
cin>>c[i];
}
for(ll i{1};i<=n;i++){
num[i][i]=c[i]-'0';
for(ll j{i+1};j<=n;j++){
num[i][j]=num[i][j-1]*10+c[j]-'0';
// cout<<num[i][j]<<' ';
}
dp[i][0]=num[1][i];
// cout<<'\n';
}
for(ll j{1};j<=k;j++){
for(ll i{j+1};i<=n;i++){
for(ll k{j-1};k<i;k++){
// cout<<NV(i)<<' '<<NV(j)<<' '<<NV(k)<<'\n'<<NV(dp[i][j])<<' '<<NV(dp[k][j-1]*num[k+1][n])<<'\n';
dp[i][j]=max(dp[i][j],dp[k][j-1]*num[k+1][i]);
}
}
}
cout<<dp[n][k]<<'\n';
}