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. Ubuntu 16.04 LTS软件包管理基本操作

    前文 Ubuntu 16.04 新特性中我们已经介绍过,随着 Ubuntu 16.04 LTS 的发布,Ubuntu 的软件包管理命令也发生了变化,新系统采用了 Debian 项目中所使用的 APT( ...

  2. C#控制台下的AO程序

    好几年没写过控制台程序了(上一次是大二时写坐标转换时用过),因为我觉得用户难以接受没有良好操作界面的应用程序,我不想偷懒,今天来将应用程序改写为控制台程序,记录一下,忘记的内容: 1.数据的交互 不管 ...

  3. QtGui.QCalendarWidget

    A QtGui.QCalendarWidget provides a monthly based calendar widget. It allows a user to select a date ...

  4. QtGui.QSlider

    A QtGui.QSlider is a widget that has a simple handle. This handle can be pulled back and forth. This ...

  5. ant design pro (十四)advanced 使用 CLI 工具

    一.概述 原文地址:https://pro.ant.design/docs/cli-cn 为了更好以及高效的开发效率,我们提供了配套的 ant-design-pro-cli 工具. pro cli 提 ...

  6. knockoutjs -- all built-in buildings

    所有可用的binding值 文字和显示:visible, text, html, css, style, attr 流程控制:foreach, if, ifnot, with form字段:click ...

  7. 几种通讯协议的比较RMI > Httpinvoker >= Hessian >> Burlap >> web service (转)

    一.综述 本文比较了RMI,Hessian,Burlap,Httpinvoker,web service等5种通讯协议的在不同的数据结构和不同数据量时的传输性能.RMI是java语言本身提供的通讯协议 ...

  8. linux动态链接库导出函数控制

    windows 环境的vc的话,可以方便的指定__declspec(dllexport) 关键字来控制是否把dll中的函数导出.我也来测试一下linux下面是如何做的:先看gcc 和ld的相关选项 = ...

  9. 微信小程序弹窗组件

    概述 自己封装的一个比较简单微信弹窗小组件,主要就是教会大家对微信小组件的用法和理解,因为微信小程序对组件介绍特别少,所以我就把自己的理解分享给大家 详细 代码下载:http://www.demoda ...

  10. SuperMap iManager跨网段配置许可

    作者:非法小恋 1.开启Docker容器1947映射 修改docker-compose.yml,在iManager的ports添加- "1947:1947" 2.重启iManage ...