-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1017.cpp
64 lines (52 loc) · 869 Bytes
/
1017.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
题目:p1017 乘积最大
*/
//dp[j][k] 表示从1到j个数字插入k个乘号所得到的最大值
//dp[j][k] = max(dp[i][k-1]*A[i+1][j]) k=<i<j
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
long strtol(string s)
{
string::reverse_iterator it = s.rbegin();
int i = 0;
long sum = 0;
while(it!=s.rend())
{
sum += (*it++-'0')*pow(10,i);
i++;
}
return sum;
}
long dp[41][7];
int GetMax(int n, int k, string s)
{
int i,j,m,maxnum,temp;
for(i=1;i<=n;i++)
dp[i][0] = strtol(s.substr(0,i));
for(j=1;j<=k;j++)
{
maxnum = 0;
for(i=j+1;i<=n;i++)
{
for(m=j;m<i;m++)
{
temp = dp[m][j-1]*strtol(s.substr(m,i-m));
if(temp>maxnum)
maxnum = temp;
}
dp[i][j] = maxnum;
}
}
return dp[n][k];
}
int main()
{
int n,k;
string s;
cin >> n >> k >> s;
cout << GetMax(n,k,s) << endl;
return 0;
}