题目链接

讲道理距离上一次写这种求值的题已经不知道多久了。

括号肯定是左括号在乘号的右边, 右括号在左边。 否则没有意义。 题目说乘号只有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 表达式求值的更多相关文章

  1. Codeforces 552E - Vanya and Brackets【表达式求值】

    给一个只有加号和乘号的表达式,要求添加一对括号使得最后结果最大.表达式长度5000,乘号最多12个,表达式中数字只有1位. 左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调 ...

  2. CF552E 字符串 表达式求值

    http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...

  3. 表达式求值(noip2015等价表达式)

    题目大意 给一个含字母a的表达式,求n个选项中表达式跟一开始那个等价的有哪些 做法 模拟一个多项式显然难以实现那么我们高兴的找一些素数代入表达式,再随便找一个素数做模表达式求值优先级表 - ( ) + ...

  4. 用Python3实现表达式求值

    一.题目描述 请用 python3 编写一个计算器的控制台程序,支持加减乘除.乘方.括号.小数点,运算符优先级为括号>乘方>乘除>加减,同级别运算按照从左向右的顺序计算. 二.输入描 ...

  5. 数据结构算法C语言实现(八)--- 3.2栈的应用举例:迷宫求解与表达式求值

    一.简介 迷宫求解:类似图的DFS.具体的算法思路可以参考书上的50.51页,不过书上只说了粗略的算法,实现起来还是有很多细节需要注意.大多数只是给了个抽象的名字,甚至参数类型,返回值也没说的很清楚, ...

  6. nyoj305_表达式求值

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  7. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  8. 数据结构--栈的应用(表达式求值 nyoj 35)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=35 题目: 表达式求值 时间限制:3000 ms | 内存限制:65535 KB描述 AC ...

  9. NOIP2013普及组 T2 表达式求值

    OJ地址:洛谷P1981 CODEVS 3292 正常写法是用栈 #include<iostream> #include<algorithm> #include<cmat ...

随机推荐

  1. DW 做一个table表 对单元格进行合并

    编辑前的代码 <body> <table width="500" border="0" bgcolor='#000000' backgroun ...

  2. java实现二叉树的相关操作

    import java.util.ArrayDeque; import java.util.Queue; public class CreateTree { /** * @param args */ ...

  3. C#获取Excel Sheet名称,对特殊字符、重名进行了处理

    /// <summary>        /// 获取指定Excel内Sheet集合        /// </summary>        /// <param na ...

  4. 分支-03. 三天打鱼两天晒网-B3

    /*B3-分支-03. 三天打鱼两天晒网 *Main.c *测试通过 */ #include <stdio.h> #include <stdlib.h> int main() ...

  5. EditPlus自动执行出结果设置

  6. 远程监控 – 应用程序运行状况测量 CSF 博客

    在远程监控基础知识和故障排除中,我们探讨了 Windows Azure 平台提供的基础指标.信息源.工具和脚本,介绍了有关监控和应用程序运行状况的基本原则.我们演示了如何利用这些基本原则对在 Wind ...

  7. Linux中service命令和/etc/init.d/的关系

    Linux中service命令和/etc/init.d/的关系   service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...

  8. Python导入模块的三种形式

    Python导入模块的3中方式: 1.import module_name 这样在程序里就可以通过module_name.metnod_name()的方式访问模块里的函数了 Example: > ...

  9. poj3062---输入什么输出什么

    #include <stdio.h> #include <stdlib.h> int main() { ]; while(gets(str) != NULL) { printf ...

  10. oracle实例名,数据库名,服务名等概念差别与联系

    数据库名.实例名.数据库域名.全局数据库名.服务名 这是几个令非常多刚開始学习的人easy混淆的概念.相信非常多刚開始学习的人都与我一样被标题上这些个概念搞得一头雾水.我们如今就来把它们弄个明确. 一 ...