CodeForces - 552E 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 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.
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).
Source
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<int>pos;
stack<LL>num;
stack<char>op;
LL calc(const string &str) {
while(!num.empty()) num.pop();
while(!op.empty()) op.pop();
for(int i = ,slen = str.length(); i < slen; ++i) {
if(str[i] == ')') {
while(op.top() != '(') {
LL tmp = num.top();
num.pop();
if(op.top() == '*') num.top() *= tmp;
else if(op.top() == '+') num.top() += tmp;
op.pop();
}
op.pop();
continue;
}
if(isdigit(str[i])) num.push(str[i] - '');
else if(str[i] == '+' && !op.empty() && op.top() == '*') {
while(!op.empty() && op.top() == '*') {
LL tmp = num.top();
num.pop();
num.top() *= tmp;
op.pop();
}
op.push(str[i]);
} else op.push(str[i]);
}
while(!op.empty()) {
LL tmp = num.top();
num.pop();
if(op.top() == '*') num.top() *= tmp;
else if(op.top() == '+') num.top() += tmp;
op.pop();
}
return num.top();
}
int main() {
string str;
cin>>str;
pos.push_back(-);
int slen = str.length();
for(int i = ; i < slen; i += )
if(str[i] == '*') pos.push_back(i);
pos.push_back(slen);
slen = pos.size();
LL ret = INT_MIN;
for(int i = ; i+ < slen; ++i)
for(int j = i+; j < slen; ++j) {
string s = str;
s.insert(pos[i]+,,'(');
s.insert(pos[j]+,,')');
ret = max(ret,calc(s));
}
cout<<ret<<endl;
return ;
}
CodeForces - 552E Vanya and Brackets的更多相关文章
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- CodeForces - 552E Vanya and Brackets —— 加与乘运算的组合
题目链接:https://vjudge.net/contest/224393#problem/E Vanya is doing his maths homework. He has an expres ...
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- Vanya and Brackets
Vanya and Brackets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- codeforces 492E. Vanya and Field(exgcd求逆元)
题目链接:codeforces 492e vanya and field 留个扩展gcd求逆元的板子. 设i,j为每颗苹果树的位置,因为gcd(n,dx) = 1,gcd(n,dy) = 1,所以当走 ...
- Codeforces 677D Vanya and Treasure 暴力+BFS
链接 Codeforces 677D Vanya and Treasure 题意 n*m中有p个type,经过了任意一个 type=i 的各自才能打开 type=i+1 的钥匙,最初有type=1的钥 ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- CodeForces 552C Vanya and Scales
Vanya and Scales Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- BZOJ 3672 [NOI2014]购票 (凸优化+树剖/树分治)
题目大意: 略 题面传送门 怎么看也是一道$duliu$题= = 先推式子,设$dp[x]$表示到达$x$点到达1节点的最小花费 设$y$是$x$的一个祖先,则$dp[x]=min(dp[y]+(di ...
- IDEA Maven 打包运行 jar java.io.FileNotFoundException: 问题?
当 使用 idea maven 将项目打包运行的时候,能够成功运行,但是总会跑到 xxx\xxx\lib 下 找jar包 如下异常: java.io.FileNotFoundException: D: ...
- LeetCode 11. Container With Most Water 单调队列
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- C#-逆变 协变 反射 代码
首先看一段测试代码,自己写的 class Program { static void Main(string[] args) { man OneMan = new man(); var d = One ...
- Raspberry PI 系列 —— 裸机点亮LED灯
Raspberry PI 系列 -- 裸机点亮LED灯 背景 近期刚买了Raspberry PI B+,配置执行了官方提供的Raspbian系统,折腾了一周Linux系统,感觉没啥意思,于是就试着想了 ...
- [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter
Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...
- 怎样获取ios设备的唯一标识
非常多地方都会须要用到唯一标志. 比方: 1. 我们相用一个设备的唯一标志当作用户id,特别是网络游戏,这样就能够省去注冊的麻烦. 2. 想把app相关的文件加密,密钥哪里来的?有些人可能会说hard ...
- Android用canvas画哆啦A梦
先上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss ...
- 单片机: EEPROM和串口通信
名称:IIC协议 EEPROM24c02 通过串口通信存数读取数据 内容:此程序用于检測EEPROM性能,測试方法例如以下:写入24c02一个数据,然后在内存中改变这些数据. 掉电后主内存将失去这些信 ...
- HDU 1392 凸包子
Surround the Trees Problem Description There are a lot of trees in an area. A peasant wants to buy a ...