Vanya and Brackets

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to9, 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.

Input

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.

Output

In the first line print the maximum possible value of an expression.

Sample Input

Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60

Hint

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).

#include<iostream>
#include<stdio.h>
using namespace std;
char exp[];
int t=;
long long cal(int left,int right)
{
long long now=;
long long ans=;
if(!left)
{
for(int i=; i<t-; i++)
{
if(exp[i]=='+')
{
if(now)
{
ans+=now;
now=;
}
}
else if(exp[i]=='*')
{
now*=(exp[i+]-'');
i++;
}
else now=exp[i]-'';
//cout<<now<<endl;
}
ans+=now;
}
else
{
long long tr=;
long long pos=;
for(int i=left+; i<right; i++)
{
if(exp[i]=='+')
{
if(pos)
{
tr+=pos;
pos=;
}
}
else if(exp[i]=='*')
{
pos*=(exp[i+]-'');
i++;
}
else pos=exp[i]-'';
}
tr+=pos;
//cout<<tr<<endl;
for(int i=; i<t-; i++)
{
if(exp[i]=='+')
{
if(now)
{
ans+=now;
now=;
}
}
else if(exp[i]=='*')
{
if(i==left)
{
now*=tr;
i=right-;
}
else
{
now*=(exp[i+]-'');
i++;
}
}
else now=exp[i]-'';
}
ans+=now;
}
return ans;
}
int main()
{ while(t)
{
exp[t++]=getchar();
if(exp[t-]=='\n') break;
}
exp[]='';
exp[]='*';
exp[t-]='*';
exp[t++]='';
/*for(int i=1;i<t;i++)
cout<<exp[i]<<" ";
//cout<<cal(0,0);*/
long long ans=cal(,);
for(int i=; i<t-; i++)
{
if(exp[i]=='*')
{
for(int j=i+; j<t-; j++)
{
if(exp[i]=='*')
{
ans=max(ans,cal(i,j));
}
}
}
}
printf("%I64d\n",ans);
return ;
}

这道题就是要想明白括号为什么要必须放在两个乘号的之间。

然后因为乘号至多有15个,所以暴力一遍就行了。

Vanya and Brackets的更多相关文章

  1. Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)

    题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...

  2. CodeForces - 552E Vanya and Brackets

    Vanya and Brackets Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u ...

  3. CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合

    题目链接:https://vjudge.net/contest/224393#problem/E Vanya is doing his maths homework. He has an expres ...

  4. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. Codeforces 552E - Vanya and Brackets【表达式求值】

    给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...

  6. codeforces 552 E. Vanya and Brackets 表达式求值

    题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...

  7. CF552E 字符串 表达式求值

    http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...

  8. Codeforces Round #308 (Div. 2)

    A. Vanya and Table   Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows ...

  9. Brackets

    按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...

随机推荐

  1. 【转载】惠新宸:PHP在百度的应用现状及展望

    http://blog.sina.com.cn/s/blog_645f8e970100qvd8.html 惠新宸,百度PHP高级顾问,年二十有八,好追根究底,有不良嗜好, 幸性本善.乙酉年识互联网,丁 ...

  2. Unity3d笔试题大全

    1.       [C#语言基础]请简述拆箱和装箱. 答: 装箱操作: 值类型隐式转换为object类型或由此值类型实现的任何接口类型的过程. 1.在堆中开辟内存空间. 2.将值类型的数据复制到堆中. ...

  3. javascript模拟post提交

    通过js模拟post提交1:请求需要的参数过长,超过get允许的最大长度2:想要隐藏地址栏的参数 //新创建一个form表单document.write('<form name=myForm&g ...

  4. linux CentOS7 修改系统时间

    linux在安装的时候如果时区选择错误,可以在系统安装完成之后修改.系统时间运行着也会有偏差,需要对时间进行实时同步,方法如下: 打开terminal 首先转到root权限 :并输入密码 然后输入:t ...

  5. ReportStudio中创建日期提示默认值模板

    很多人已经知道可以通过JS给RS中的日期提示控件设置运行前的默认值---------例如: 日期时间段默认为上一个月的开始日和结束日 在系统所有的报表中都这样操作,我们如何快速的引入?和方便下次修改统 ...

  6. CentOS6网卡静态IP设置

    CentOS网卡配置源文件如下:DEVICE=eth0HWADDR=00:0C:29:A8:67:46TYPE=EthernetUUID=4103d7a8-d073-4e93-ac68-e6f8496 ...

  7. CSS经验分享:如何书写可维护的CSS代码01

    转自:http://www.cnblogs.com/hxling/archive/2013/02/21/2920520.html 一.在样式表开头添加一个注释块,用以描述这个样式表的创建日期.创建者. ...

  8. QQ互联简单例子,七彩花都提供

    QQ互联简单例子 源码由七彩花都论坛提供 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  9. 使用 RDVTabBarController 制作底部凸起的 TabBar 笔记

    欢迎訪问我的个人博客http://mmoaay.photo/ 本文主要针对底部凸起的 TabBar 这样的特殊需求,不感兴趣的能够直接绕过- 近期做的一个项目须要底部凸起的 TabBar,效果例如以下 ...

  10. element-ui 源码学习

    https://athena0304.github.io/element-analysis/ 1.模板字符串实现字符串拼接 typeClass() { return `el-alert--${ thi ...