<pre name="code" class="cpp">//
// main.cpp
// Richard
//
// Created by 邵金杰 on 16/9/6.
// Copyright © 2016年 邵金杰. All rights reserved.
//
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=20;
const int inf=99999999;
char s[maxn];
int f[maxn][maxn],num[maxn][maxn];
int m,n;
void pre_work()
{
for(int i=1;i<=n;i++)
{
for(int j=i,d=0;j<=n;j++)
{
d=d*10+s[j]-'0';
num[i][j]=d;
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
f[i][j]=inf;
}
}
}
void dp()
{
for(int i=1;i<=n;i++)
f[0][i]=num[1][i];
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=n;j++)
{
for(int k=1;k<j;k++)
{
f[i][j]=min(f[i][j],f[i-1][k]+num[k+1][j]);
}
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
scanf("%s",s+1);
pre_work();
dp();
cout<<f[m][n]<<endl;
}
return 0;
}
最佳加法表达式(dp)
最新推荐文章于 2024-01-12 15:36:10 发布