题目链接:https://vjudge.net/contest/224393#problem/E

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.

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.

Examples

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

Note

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

题意:

给出一个只有加法和乘法的算式,且数字的范围为1~9,算式里面没有括号。问:怎样加一对括号,使得算式的结果最大?

题解:
1.比划一下,可发现规律:括号加在两‘+’中间,最终结果没有改变;括号加在‘+’和‘*’之间,可使结果变大,但不一定最优。只有当括号加在两'*'之间时,结果是最大。

2.题目规定了‘*’不会超过15个,所以可直接枚举左右括号的放置位置,然后求出算式的值,取最大值即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; char a[];
LL s[MAXN], len;
int pos[MAXN]; LL solve(int l, int r)
{
LL sum = , t1 = , t2 = , t3 = ;
int top = ; if(<=l) //左边
{
s[top++] = a[]-'';
for(int i = ; i<=l-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
t1 = s[--top];
while(top) sum += s[--top];
} //中间:
s[top++] = a[l+]-'';
for(int i = l+; i<=r-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
t2 = ;
while(top) t2 += s[--top]; // 右边:
if(r<=len-)
{
s[top++] = a[r+]-'';
for(int i = r+; i<=len-; i+=)
{
if(a[i]=='*') s[top-] = 1LL*s[top-]*(a[i+]-'');
else if(a[i]=='+') s[top++] = (a[i+]-'');
}
while(top>) sum += s[--top];
t3 = s[--top];
}
sum += 1LL*t1*t2*t3;
return sum;
} int main()
{
while(scanf("%s",a+)!=EOF)
{
int cnt = ;
pos[++cnt] = ;
len = strlen(a+);
for(int i = ; i<=len; i++)
if(a[i]=='*') pos[++cnt] = i;
pos[++cnt] = len+; LL sum = ;
for(int l = ; l<=cnt; l++) //枚举左右括号
for(int r = l+; r<=cnt; r++)
sum = max(sum, solve(pos[l], pos[r])); cout<<sum<<endl;
}
}

CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合的更多相关文章

  1. CodeForces - 552E Vanya and Brackets

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

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

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

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

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

  4. Vanya and Brackets

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

  5. codeforces 492E. Vanya and Field(exgcd求逆元)

    题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...

  6. Codeforces 677D Vanya and Treasure 暴力+BFS

    链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...

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

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

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

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

  9. codeforces 492C. Vanya and Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/492/C 题目意思:给出 3 个整数:n,  r,  avg.然后有 n 行,每行有两个数:第 i 行有 ...

随机推荐

  1. context:exclude-filter spring事宜【经典-转】

    context:exclude-filter spring事务 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行. ...

  2. String转Map的工具类

    借鉴代码 public class StringToMapUtil { public static Map<String, String> getValue(String param) { ...

  3. 2017.2.9 开涛shiro教程-第十章-会话管理(一)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十章 会话管理(一) 10.1 会话 shiro提供的会话可以用 ...

  4. haifeng

    [root@localhost 桌面]# yum list|grep wubi ibus-table-chinese-wubi-haifeng.noarch -.el7 base ibus-table ...

  5. 如何Enable FireFox里的Java Plugin

    步骤,Tools->Add-ons->Plugins 然后把Java(TM) PlatformXXX...的状态修改为Always Activate 如下图:

  6. 网络电台(WIZ550io)

    网络电台是用WIZ550io(内嵌MAC地址)和ATMEGA1284(Flash 128K,EEPROM4K)制作的.用户可注冊多达80个无线电广播. 无线电广播的注冊可在内嵌网页中进行. 网络电台的 ...

  7. UNP学习笔记(第八章 基本UDP套接字编程)

    UDP应用程序客户不与服务器建立连接,而是只管使用sendto函数给服务器发送数据报,其中必须指定目的地的地址作为参数. 下图给出典型的UDP客户/服务器程序的函数调用. recvfrom和sendt ...

  8. 利用MFC里面格式化函数也可以实现可变长度的问题

    直接粘代码: 1: CString str1;  //定义两个MFC里面的CString里面的字符串 2: CString str2; 3: str1.Format("(%d)", ...

  9. ExtJs4学习(一):正确认识ExtJs4

    认识ExtJs 1.Javat能用ExtJs吗? 它是展现层的技术,与JS,HTML,CSS有关.至于server端是.Net,还是PHP等无关. 2.ExtJs适合什么样的项目? 依照官方的说法,E ...

  10. Bootstrap学习速查表(二) 排版及表格

    一.h1~h6标签 固定不同级别标题字体大小,h1=36px,h2=30px,h3=24px,h4=18px,h5=14px和h6=12px. 1.重新设置了margin-top和margin-bot ...