codeforces 552 E. Vanya and Brackets 表达式求值
题目链接
讲道理距离上一次写这种求值的题已经不知道多久了。
括号肯定是左括号在乘号的右边, 右括号在左边。 否则没有意义。 题目说乘号只有15个, 所以我们枚举就好了。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
stack <char> sign;
stack <ll> digit;
int a[20];
string str;
void cal() {
char s = sign.top(); sign.pop();
ll x = digit.top(); digit.pop();
ll y = digit.top(); digit.pop();
ll tmp;
if(s == '+')
tmp = x+y;
else
tmp = x*y;
digit.push(tmp);
}
ll solve(){
if(!digit.empty())
digit.pop();
for(int i = 0; i < str.size(); i ++) {
if(isdigit(str[i])) {
digit.push(str[i]-'0');
} else if(str[i] == '(') {
sign.push('(');
} else if(str[i] == ')') {
while(sign.top() != '(') {
cal();
}
sign.pop();
} else if(str[i] == '*') {
sign.push('*');
} else {
while(!sign.empty() && sign.top() == '*')
cal();
sign.push('+');
}
}
while(!sign.empty())
cal();
return digit.top();
}
int main()
{
string s;
cin>>s;
int cnt = 0;
a[cnt++] = -1;
for(int i = 0; i < s.size(); i++)
if(s[i] == '*')
a[cnt++] = i;
a[cnt++] = s.size();
ll ans = 0;
for(int i = 0; i < cnt; i ++) {
for(int j = i+1; j < cnt; j ++) {
str = s;
str.insert(a[i]+1, 1, '(');
str.insert(a[j]+1, 1, ')');
ans = max(ans, solve());
}
}
cout<<ans<<endl;
return 0;
}
codeforces 552 E. Vanya and Brackets 表达式求值的更多相关文章
- Codeforces 552E - Vanya and Brackets【表达式求值】
给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...
- CF552E 字符串 表达式求值
http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...
- 表达式求值(noip2015等价表达式)
题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...
- 用Python3实现表达式求值
一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...
- 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值
一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...
- nyoj305_表达式求值
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- 利用栈实现算术表达式求值(Java语言描述)
利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...
- 数据结构--栈的应用(表达式求值 nyoj 35)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...
- NOIP2013普及组 T2 表达式求值
OJ地址:洛谷P1981 CODEVS 3292 正常写法是用栈 #include<iostream> #include<algorithm> #include<cmat ...
随机推荐
- asp.net uploadfile 上传文件,连接已重置问题
修改web.config中的配置 <httpRuntime maxRequestLength="/> //设置上传文件大小(kb)和响应时间(s) 针对iis7或更高版本另需要在 ...
- UVa 202 - Repeating Decimals
给你两个数,问你他们相除是多少,有无限循环就把循环体括号括起来 模拟除法运算 把每一次的被除数记下,当有被除数相同时第一个循环就在他们之间. 要注意50个数之后要省略号...每一次输出之后多打一个回车 ...
- [Linked List]Delete Node in a Linked List
otal Accepted: 48115 Total Submissions: 109291 Difficulty: Easy Write a function to delete a node (e ...
- C# 封装 System.Data.SQLite
参考1: 关于如何使用System.Data.SQLite的入门: http://www.dreamincode.net/forums/topic/157830-using-sqlite-with-c ...
- tcp窗口滑动以及拥塞控制
转自:http://blog.chinaunix.net/uid-26275986-id-4109679.html TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥 ...
- Servle原理
这篇博客将以Tomcat为例讲一讲Servlet的原理 Servlet容器 Servlet与Servlet容器的关系举个不恰当的例子就像枪和子弹的关系.而Servlet就是子弹,容器就是枪.子弹都有统 ...
- 【转】commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar实现文件上传
总共:一个upload.jsp,一个FileUploadServlet.java,两个文件:ImagesUploaded,ImagesUploadTemp, 一个web.xml,两个架包:common ...
- PDO的事物处理机制
Mysql的事务处理: 1.MySQL目前只有InnoDB 和BDB两个数据表类型才支持事务. 2.在默认条件下,MySQL是以自动提交(autocommit)模式运行的,这就意味着所执行的每一个语句 ...
- ecshop使用Google API及OAuth2.0登录授权(PHP)
一.申请clientID https://console.developers.google.com/project 二.开启Google+ API权限 https://console.develop ...
- Struts2 注解(转)
转自:http://blog.csdn.net/wwwqvod/article/details/6214431 也叫Zero Configuration(零配置),它省去了写xml文件的麻烦,可以直接 ...