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 ...
随机推荐
- STM32 关于HAL库硬件SPI要注意的问题总结
利用STM32CUbeMx编写程序,大大方便了开发,最近做的项目利用到了 STM32CUbeMx的硬件SP,这里对SPI的使用做一个总结. HAL库里的硬件SPI主要有以下几个库函数: /* hspi ...
- 【【henuacm2016级暑期训练】动态规划专题 F】Physics Practical
[链接] 我是链接,点我呀:) [题意] 给你n个数字 让你删掉最小的数字 使得: 剩余的数字中 "最大的数字"小于等于"最小的数字*2" [题解] 把数据从小 ...
- FastDFS 工具类实现文件上传_02
一.jar 包 jar包下载:https://pan.baidu.com/s/1nwkAHU5 密码:tlv6 或者 下载工程,安装到 maven 本地仓库 工程下载:https://pan.baid ...
- 使用excel进行数据挖掘(6)---- 预測
在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/article/details/46445435 例子 DM ...
- ECMAScript 6新特性之Proxy
ECMAScript 6中新增了一个全局构造函数:Proxy.该构造函数能够接收两个參数:一个目标对象.一个处理对象. 代码演示样例: var target = {}; var handler = { ...
- IOS 数据存储之 SQLite具体解释
在IOS开发中常常会须要存储数据,对于比較少量的数据能够採取文件的形式存储,比方使用plist文件.归档等,可是对于大量的数据,就须要使用数据库,在IOS开发中数据库存储能够直接通过SQL訪问数据库, ...
- Zorka监控平台的Online reconfiguration基本效果展示
在上一篇日志中,我简介了Zorka的Online reconfiguration的用法,可是没怎么介绍如何看到在线更改的效果,这里简单说说. 还是以之前的tomcat为例,我们在文件夹zorka\sc ...
- nyoj--214--单调递增子序列(二)(二分查找+LIS)
单调递增子序列(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给定一整型数列{a1,a2...,an}(0<n<=100000),找出单调递增最长子序 ...
- rails数据库操作rake db一点心得
问题描述,对于很多的新手rails lover来说,搞定db是件头疼的事情,当建立了一个model,测试了半天发现我草列名写错了,再过一会儿发现association里面竟然没有xxx_id,这下子s ...
- Go语言结构体转json的坑
Go语言结构体转json的坑 标签(空格分隔): go json.Marshal() JSON输出的时候必须注意,只有导出的字段(首字母是大写)才会被输出,如果修改字段名,那么就会发现什么都不会输出, ...