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. T-SQL经典语句(SQL server)

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  2. MIME简介

    MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器 ...

  3. [Python爬虫] 之六:Selenium 常用控件用法

    Selenium 常用控件用法 1.文本框 上图中,如何定位搜索文本框,并输入搜索内容进行搜索 首先:利用方法 find_element_by_xpath定位元素:inputElements = se ...

  4. 每日一小练——Armstrong数

    上得厅堂.下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Armstrong数 内容: 在三位的正整数中,比如abc.有一些能够满足a^3+b^3+c^3=abc的条件,也就是说各 ...

  5. Sqlserver DateTime转换成SMALLDATETIME时“产生一个超出范围的值”

    工作中遇到一个问题,A表中字段(DateTime1)的数据类型为DateTime,新建了一张表B的SMALLDATETIME1字段的数据来自A表的DateTime1 但在将A表字段DateTime1导 ...

  6. Eureka 的 Application Service client的注冊以及执行演示样例

            Eureka 服务器架起来了(关于架设步骤參考博客<Linux 下 Eureka 服务器的部署>),如今怎样把我们要负载均衡的服务器(也就是从 Application Cl ...

  7. (笔试题)关于C++的虚函数和多态性

    以下两段程序的输出是什么? 程序1: #include "stdio.h" class Base { public: int Bar(char x) { return (int)( ...

  8. HAWQ技术解析(八) —— 大表分区

    一.HAWQ中的分区表        与大多数关系数据库一样,HAWQ也支持分区表.这里所说的分区表是指HAWQ的内部分区表,外部分区表在后面"外部数据"篇讨论. 在数据仓库应用中 ...

  9. Unity发布各平台路径

    #if UNITY_EDITOR string filepath = Application.dataPath + "/StreamingAssets"; #elif UNITY_ ...

  10. Linux dig

    安装dig: yum install bind-utils dig 常用命令: # dig 最基本的用法dig @server sina.com.cn. # 用 dig 查看 zone 数据传输 di ...