【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
3+5*7+8*4
output
303
input
2+3*5
output
25
input
3*4*5
output
60
Note
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).
【题目链接】:http://codeforces.com/contest/552/problem/E
【题解】
括号肯定要加在乘号的旁边,或者加在最开头或最结尾这样最好.
不然你加在加号的旁边没有意义的。。
因为乘号最多15个再加上开头和结尾。总共17个;
就O(17^2*长度);
枚举括号的C(N,2)个组合,然后写个函数搞出表达式的值就好.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
string s;
vector <int> a;
stack <char> ch;
stack <LL> num;
LL cl2(LL x,LL y,char key)
{
if (key=='+') return x+y;
else
return x*y;
}
void cal()
{
LL x = num.top();
num.pop();
LL y = num.top();
num.pop();
LL z = cl2(x,y,ch.top());
ch.pop();
num.push(z);
}
LL cl(string s)
{
int len = s.size();
rep1(i,0,len-1)
if (s[i] == '(')
ch.push(s[i]);
else
if (isdigit(s[i]))
num.push(s[i]-'0');
else
if (s[i]=='*')
ch.push(s[i]);
else
if (s[i]=='+')
{
while (!ch.empty() &&ch.top()=='*')
cal();
ch.push(s[i]);
}
else
if (s[i]==')')
{
while (ch.top()!='(')
cal();
ch.pop();
}
while (!ch.empty())
cal();
return num.top();
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> s;
a.pb(-1);
int len = s.size();
for (int i = 1;i <= len-1;i+=2)
if (s[i]=='*')
a.pb(i);
a.pb(len);
LL ans = 0;
len = a.size();
rep1(i,0,len-2)
rep1(j,i+1,len-1)
{
int l = a[i],r = a[j];
string temps = s;
temps.insert(l+1,"(");
temps.insert(r+1,")");
while (!num.empty()) num.pop();
ans = max(ans,cl(temps));
}
cout << ans << endl;
return 0;
}
【39.29%】【codeforces 552E】Vanya and Brackets的更多相关文章
- JAVA 基础编程练习题29 【程序 29 求矩阵对角线之和】
29 [程序 29 求矩阵对角线之和] 题目:求一个 3*3 矩阵对角线元素之和 程序分析:利用双重 for 循环控制输入二维数组,再将 a[i][i]累加后输出. package cskaoyan; ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- codeforces 552 E. Vanya and Brackets 表达式求值
题目链接 讲道理距离上一次写这种求值的题已经不知道多久了. 括号肯定是左括号在乘号的右边, 右括号在左边. 否则没有意义. 题目说乘号只有15个, 所以我们枚举就好了. #include <io ...
- 【39.66%】【codeforces 740C】Alyona and mex
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【35.29%】【codeforces 557C】Arthur and Table
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- [官方软件] Easy Sysprep v4.3.29.602 【系统封装部署利器】(2016.01.22)--skyfree大神
[官方软件] Easy Sysprep v4.3.29.602 [系统封装部署利器](2016.01.22) Skyfree 发表于 2016-1-22 13:55:55 https://www.it ...
随机推荐
- 一个小的考试系统 android 思路
一个小的考试系统 android 思路 假如有 100 组,每组有4个单选钮,设置超时检测确认后去测结果估分视图去切换,如果还有,就再显示下一组 所有结束就给个总结显示 有超时结束过程加上 提示正确选 ...
- Vue自定义函数挂到全局方法
方法一:使用Vue.prototype //在mian.js中写入函数 Vue.prototype.getToken = function (){ ... } //在所有组件里可调用函数 this.g ...
- pytest_多用例执行(1)
一.首先创建测试套件 # -*- coding:utf-8 -*-from __future__ import print_functionimport pytestimport allure cla ...
- 微信小程序简单常见首页demo
wxml <view class='index-contier'> <view class="index-left"> <view>电池剩余&l ...
- 动态链接库DLL的创建生成及调用
一.背景 最近在做CANTOUSB底层驱动的调用,是调用别人已经封装好的库,看不到别人写的源程序.程序中调用的是隐式调用即 x.h+x.lib+x.dll,其中DLL即是动态链接库(Dynamic L ...
- STM32 之ADC单次转换模式和连续转换模式
一.背景 在STM32中的AD的单通道采样中可以设置成单次转换模式和连续转换模式,如何理解这两个转换模式的区别,通过程序又是怎样实现的? 二.正文 首先理解单次转换模式,即ADC进行单次转换(单样本) ...
- 14.inline与namespace使用
#include <iostream> using namespace std; namespace all { //inline作用为默认调用 inline namespace V201 ...
- Jenkins学习总结(1)——Jenkins详细安装与构建部署使用教程
Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发 ...
- Java vs C++:子类覆盖父类函数时缩小可访问性的不同设计
Java 和 C++ 都是面向对象的语言,允许对象之间的继承.两个语言的继承都设置有允许子类覆盖父类的“虚函数”,加引号是因为 Java 中没有虚函数这一术语,但是我们的确可以把 Java 的所有函数 ...
- jquery 获取上一个兄弟元素和下一个兄弟元素
jQuery.prev(),返回上一个兄弟节点,不是所有的兄弟节点 jQuery.prevAll(),返回所有之前的兄弟节点 jQuery.next(),返回下一个兄弟节点,不是所有的兄弟节点 jQu ...