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 ...
随机推荐
- linux 源码包安装拾遗
源码包安装和apt-get/yum的区别 安装前的区别:概念上的区别 rpm和dpkg包是经过编译过的包,并且其安装位置由厂商说了算,厂商觉得安装在哪里合适,就会装在哪里,而源码包则是没有经过编译的文 ...
- python的装饰器,迭代器用法
装饰器. 装饰器实际就是一个函数 定义:在不改变内部代码和调用方式的基础上增加新的功能 了解装饰器需要了解3个内容: 1.函数即变量 2.高阶函数 1).把一个函数名当作实参传给另一个函数 2).返回 ...
- 用pycharm运行django项目
[点击]run -> Edit Configrations 弹出如下页面 点击“+” 点击Django server 在弹出页面的host填0.0.0.0 点击这个“文件夹” 点击‘+’后填下面 ...
- android自己定义Application全局变量不能类型转换的问题
今天弄了个全局变量AppContext ,但一直出现例如以下错误,原来继承 Application的得在清单文件声明. java.lang.RuntimeException: Unable to st ...
- hiho模拟面试题2 补提交卡 (贪心,枚举)
题目: 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Ho给自己定了一个雄伟的目标:连续100天每天坚持在hihoCoder上提交一个程序.100天过去了.小Ho查 ...
- python爬虫 分页获取图片并下载
--刚接触python2天,想高速上手,就写了个爬虫,写完之后,成就感暴增,用起来顺手多了. 1.源代码 #coding=utf-8 import urllib import re class Pag ...
- dom 编程(html和xml)
html dom与xml dom关系: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...
- cocos2d-x 2.2.2 在lua中更换CCSprite的图片
废话不多说,直接上代码 --lua --获取场景 local scene= CCDirector:sharedDirector():getRunningScene() --创建精灵 local tes ...
- Java获取项目路径下的方法(全)
平时敲代码的时候,非常多时候提示文件找不到,而抛出了异常,如今整理例如以下 一 相对路径的获得 说明:相对路径(即不写明时候究竟相对谁)均可通过下面方式获得(不论是一般的java项目还是web项目) ...
- UVA 11971 - Polygon 数学概率
Polygon John has been given a segment of lenght N, however he n ...