http://codeforces.com/contest/552/problem/E
1 second
256 megabytes
standard input
standard output
Vanya is doing his maths homework. He has an expression of form ,
where x1, x2, ..., xn are
digits from 1 to 9, and
sign represents
either a plus '+' or the multiplication sign '*'.
Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.
The first line contains expression s (1 ≤ |s| ≤ 5001, |s| is
odd), its odd positions only contain digits from 1 to 9,
and even positions only contain signs + and * .
The number of signs * doesn't exceed 15.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
/**
CF552E 字符串 表达式求值
题目大意:给定一个字符串仅仅是做1~9之间的加法和乘法,如今在表达式中加上一对括号。问怎样加才干使表达式的值最大
解题思路:左括号必须在一个*的后面,右括号必须在一个*的前面,假设不是这样一定不是最优。 有了这个结论,分成三部分算一下就能够了
*/
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long LL;
char s[5005];
int n,a[105];
LL get(int l,int r)
{
LL ans=0,tmp=1;
for(int i=l;i<=r;i++)
{
if(isdigit(s[i]))
{
tmp*=s[i]-'0';
}
if(s[i]=='+')
{
ans+=tmp;
tmp=1;
}
}
ans+=tmp;
tmp=1;
LL sum=0;
for(int i=0;i<n;i++)
{
if(i==l)
{
tmp*=ans;
i=r;
}
else
{
if(isdigit(s[i]))
{
tmp*=s[i]-'0';
}
if(s[i]=='+')
{
sum+=tmp;
tmp=1;
}
}
}
sum+=tmp,tmp=1;
// printf(">>>%d %d %d %d\n",l,r,ans,sum);
return sum;
}
int main()
{
scanf("%s",s);
n=strlen(s);
int k=0;
a[k++]=0;
for(int i=0;i<n;i++)
{
if(s[i]=='*')
{
a[k++]=i-1;
a[k++]=i+1;
}
}
if(a[k-1]!=n-1)
a[k++]=n-1;
LL maxx=0;
for(int i=0;i<k;i++)
{
for(int j=i;j<k;j++)
{
LL t=get(a[i],a[j]);
maxx=max(maxx,t);
}
}
printf("%lld\n",maxx);
return 0;
}