【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 ...
随机推荐
- ZOJ QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- 【代码】Django学习笔记
一些设置setting.py DEBUG = True ALLOWED_HOSTS = ['*'] DATABASES = { 'default': { 'ENGINE': 'django.db.ba ...
- Android基于xmpp的即时通讯应用
xmpp是一个通信协议.因为这是个开放的协议,为了节俭开发成本,很多即时应用都采用了这个协议.Android上最常用的组合asmack +openfire.Asmack是smack的android版, ...
- 使用node.js+babel,支持import/export语法
如果要在node里面支持import/export default语法步骤: 1.使用npm安装 babel的客户端工具 npm init 会生成package.json文件 2.接着安装bebel客 ...
- Java Web学习总结(14)——JSP基础语法
任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依然有其自己扩充的语法,而且在JSP中,所有的JAVA语句都可以使用. 一.JSP模版元素 JSP页面中的HTML内容称之 ...
- 原 HttpClient 4.3超时设置
https://my.oschina.net/u/577453/blog/173724 http://blog.csdn.net/zh521zh/article/details/51994140
- 将一个类写成WebService服务的形式
WebService是一种跨编程语言和跨操作系统平台的远程调用技术,主要解决不同语言写的应用程序之间.不同平台(linux/windows/andrid)之间的通信,即异构系统之间的通信. 常用的天气 ...
- windows 控制台下 无法获取完整的回车键值
问题描述: 收集的网友分析: http://bbs.csdn.net/topics/370084904 因为C语言和UNIX的开发者是同事…… C语言里统一用的\n表示另起一行.微软的DOS受到了当时 ...
- Android ServiceManager启动
许久就想写篇关于servicemanager的文章,之前对服务启动顺序诸如zygote,systemserver.等启动顺序理解有点混乱,现做例如以下理解分析: 事实上init进程启动后,Servic ...
- 重构——DataTable转泛型
泛型简单介绍 泛型能够最大限度的重用代码.保护类型的安全.提高性能. 泛型最常见的用途是创建集合类 泛型数据类型中使用的信息可在执行时通过反射 ...