题目链接: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. Hadoop部署启动异常问题排查

    hadoop的日志目录(/home/hadoop/app/hadoop-2.6.4/logs) 1.hadoop启动不正常用浏览器访问namenode的50070端口,不正常,需要诊断问题出在哪里: ...

  2. [转]MySQL的简单使用和JDBC示例

    MySql简单操作 //启动mysql net start mysql //登陆 mysql -u root -p //创建建数据库 create database mydb; create data ...

  3. Shell脚本之:替换

    转义字符 #!/bin/bash a= echo -e "Value of a is $a \n" 使用-e表示对转义字符进行替换,默认情况是不转义的 命令替换 命令替换的语法,注 ...

  4. VMware-Fusion-7.0.0-2103067 Pro SN:序列号+ 百度云下载地址

    VMware-Fusion-7.0.0-2103067Pro SN: 5CQE9-H5PY3-04ND5-4Z6EW-3QGDE JZCNC-2H9X9-44TD9-Y0X5W-2KGP5 8ZNTC ...

  5. JavaWeb学习笔记:Servlet

    Servlet JavaWeb 概念 Java Web应用由一组Servlet.HTML页面.类.以及其他能够被绑定的资源构成. 他能够在各种供应商提供的实现Servlet规范的Servlet容器中执 ...

  6. Django之信息聚合

    feeds.py #coding:utf-8 __author__ = 'similarface' from django.contrib.syndication.views import Feed ...

  7. Rider

    听说你开发.NET还在用VS,小哥哥给你推荐全平台的Rider   本文地址:http://www.cnblogs.com/likeli/p/8461010.html 前言 .NET平台的开发一直都只 ...

  8. [ACM] HDU 1533 Going Home (二分图最小权匹配,KM算法)

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. ICMP控制报文协议

    1.引言 ICMP经常被认为是IP层的一个组成部分.它传递差错以及其他需要注意的信息.ICMP报文通常被IP层或更高层 协议(TCP或UDP)使用.一些ICMP报文把差错报文返回给用户进程.ICMP报 ...

  10. 通用安防摄像机通过RTSP转RTMP推流进行H5(RTMP/HLS)直播的方案

    EasyNVR摄像机无插件直播方案 随着互联网的发展,尤其是移动互联网的普及,基于H5.微信的应用越来越多,企业也更多地想基于H5.微信公众号来快速开发和运营自己的视频及视频相关性产品,那么传统的安防 ...