【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 ...
随机推荐
- weblogic安装(无界面静默安装)
一.环境准备 1. 用户准备 Generic通用版weblogic不能用ROOT用户安装,如无其他用户需先创建用户,创建用户步骤此处略过 2. 下载weblogic 在官网下载weblogic,将下载 ...
- powerdesigner逆向自动生成mysql说明文档、PDM
做EDI的项目的时候,用到相关工具powerdesigner,正好我们的一个项目对数据设计阶段时相关文档没有很好的保存下来,查找了一下powderdesigner相关文档,采用逆向工程,从mysql数 ...
- Windows上安装多个MySQL实例(转)
在学习和开发过程中有时候会用到多个MySQL数据库,比如Master-Slave集群.分库分表,开发阶段在一台机器上安装多个MySQL实例就显得方便不少. 在 MySQL教程-基础篇-1.1-Wind ...
- body{display:none}
body{display:none} 使浏览器不显示内容,用这样的代码删除 $document = str_replace('body{display:none}','',$document);
- Java 函数的参数说
java函数参数传递的到底是值还是引用对确实容易让人迷糊.而很多时候因为对这个问题的模糊甚至造成一些错误.最常见的说法是基本类型传的是值,对象传的引用.对于基本类型,大家都达成共识,没有什么可以争论的 ...
- jquery追加元素,移除DOM,jqueryDOM操作
1.append() 方法在被选元素的结尾插入内容. 2.prepend() 方法在被选元素的开头插入内容. 3.after() 方法在被选元素之后插入内容. 4.before() 方法在被选元素之前 ...
- python3中让程序暂停运行的语句
https://blog.csdn.net/zmz971751504/article/details/78288988
- usr/bin/mysqladmin: refresh failed; error: 'Unknown error'
debian wheezy 升级后, 由于授权错误, 导致password给改动, 在debian的mysql safe下也无法进入. 我在/etc/mysql/my.cnf 里面已经改动了bin ...
- HTTP--Request Headers及Cookies
简介: HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.大多数请求头并不是必需的,但Content-L ...
- .NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一)
原文:.NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一) 写下此文章只为了记录Surging微服务学习过程,并且分享给广大想学习surging的基友,方便广大 ...