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目 ...
随机推荐
- [Todo]很不错的Java面试题类型整理,要看
http://www.importnew.com/21445.html 1. 问,以下,会返回什么. public int func() { int ret = 0; try{ throw new E ...
- 【帧动画总结】AnimationDrawable Frame
Drawable Animation 开发者文档 位置:/sdk/docs/guide/topics/graphics/drawable-animation.html Drawable animati ...
- Webview 支持文件上传
默认情况下情况下,在一个带有input tpye=file标签的Html页面,使用Android的WebView是不能够支持上传文件的(在iOS和微信上完全正常工作).而这个,也是在我们的前端工程师告 ...
- C#创建word,操作、读写
要使用C#操作word,首先要添加引用: 1.添加引用->COM->Microsoft Word 11.0 Object Library 2.在.cs文件中添加 using Word;下面 ...
- nDCG学习笔记
参考:http://en.wikipedia.org/wiki/Discounted_cumulative_gain Normalized Discounted Cumulative Gain:一种对 ...
- 初次使用IntelliJ IDEA
一.认识IDEA IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持 ...
- input 禁止 复制 粘贴 剪切 操作
1.代码 <Input onCopy={(e)=>{ // 禁止拷贝 e.preventDefault(); }} onPaste={(e)=>{ // 禁止粘贴 e.prevent ...
- Apple Watch 会再一次改变世界么?
这一次苹果发布会,苹果推出了Apple Watch.他会像iPhone系列一样,去改变大家对于手表的理解么?会像iPhone一样受到大家的追捧么? 我觉得第一代的产品只可能是试探性的,真正能去引爆市场 ...
- .Net 两大利器Newtonsoft.NET和Dapper
你可以使用ado.net返回的DataTable让Newtonsoft.NET来序列化成Json. 当然你可以使用Dapper返回的List让Newtonsoft.NET来序列化成JSON. 参考资料 ...
- [Windows驱动开发](三)基础知识——驱动例程
一.NT式驱动的基本例程 1. 驱动入口函数——DriverEntry // 驱动程序的一般性定义 NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObje ...