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
3+5*7+8*4
303
2+3*5
25
3*4*5
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的更多相关文章
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- CodeForces - 552E Vanya and Brackets
Vanya and Brackets Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合
题目链接:https://vjudge.net/contest/224393#problem/E Vanya is doing his maths homework. He has an expres ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- CF552E 字符串 表达式求值
http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...
- Codeforces Round #308 (Div. 2)
A. Vanya and Table Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows ...
- Brackets
按下Ctrl + E("编辑")或退出编辑.Brackets将搜索项目下所有CSS文件 Ctrl/Cmd + Alt + P 打开即时预览功能 alt + command + O目 ...
随机推荐
- [置顶] JDK工具(零)--简要介绍JDK1.6自带的42个工具
Java的开发人员肯定都知道JDK的bin目录中有“java.exe”和“javac.exe”这两个命令行工具, 但并非所有的Java程序员都了解过JDK的bin目录之中其它命令行程序的作用. JDK ...
- Android -- ListView与ArrayAdapter、SimpleAdapter
对于ArrayAdapter,里面虽然能添加图片,但只能是相同的图片. 废话不多说: 布局&&list的item布局 ...
- Android开发者指南(9) —— ProGuard
转: 前言 本章内容为开发者指南(Dev Guide)/Developing/Tools/ProGuard,本章内容为"混淆器",版本为Android3.0 r1,翻译来自:&qu ...
- c# String.IndexOf 方法 string查找字符串
c# String.IndexOf 方法 (value, [startIndex], [count]) 报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置. ...
- 使用javascript开发的视差滚动效果的云彩 极客标签 - 做最棒的极客知识分享平台
www.gbtags.com 使用javascript开发的视差滚动效果的云彩 阅读全文:使用javascript开发的视差滚动效果的云彩 极客标签 - 做最棒的极客知识分享平台
- 根据轨迹线构造GPS点的方法
在地理国情普查项目中,元数据中的轨迹线图层由外业GPS点转换构成.理论上,外业GPS轨迹点在采集时间.采集人员等方面应当保持一致.但是,由于管理上.操作上的原因导致有元数据轨迹线而外业GPS轨迹点的现 ...
- PL/SQL 之 基础
PL/SQL(Procedural Language extensions to SQL)是Oracle 对标准 SQL 语言的过程化扩展,是专门用于各种环境下对 Oracle 数据库进行访问和开发的 ...
- 深入理解磁盘文件系统之inode(转)
一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...
- 〖Linux〗关于Linux软件包安装位置、版本查询
1. 查询版本 aptitude show [软件] 2. 查询路径 dpkg -L [软件] whereis [软件] which [软件]
- java获取unicode码
public String toUNICODE(String s) { StringBuilder sb=new StringBuilder(); for(in ...