【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 ...
随机推荐
- JS实践与写博客-序
大二的时候,就开始接触JavaScript了. 当时学了1年多,主要是认真看了一本JavaScript的入门书籍,了解了JavaScript大致怎么回事.在独自做Web项目的时候,用的都是JavaSc ...
- 【Python】用Python的“结巴”模块进行分词
之前都是用计算所的分词工具进行分词,效果不错可是比較麻烦,近期開始用Python的"结巴"模块进行分词,感觉很方便.这里将我写的一些小程序分享给大家,希望对大家有所帮助. 以下这个 ...
- rhel5安装 oracle11
readhat 安装11gr2文档 需要注意的地方:必须关掉的 1,防火墙:2,SElinux . root 用户运行 setup 命令可关防火墙与SElinux 修改网络配置文件,一定要重启此文 ...
- 【组件】微信小程序input搜索框的实现
开发小程序的过程,是一个学习知识,解决问题的过程,每当实现了一个需求,总会有很大的成就感,每天记录一个开发过程中的细节.实现效果如下: 官方参考链接:https://developers.weixin ...
- c# 调用 C++ dll 传入传出 字符串
c# 调用 C++ dll 传入传出 字符串 2013-07-02 09:30 7898人阅读 评论(2) 收藏 举报 本文章已收录于: 分类: windows 版权声明:随便转载,随便使用. C ...
- 学习easyui疑问(三)
今天我学习easyui中碰到的还有一问题是:怎样创建一个表格? 首先,在easyui中文官网上提供的这样一种定义方式: <!--table--> <table id="tt ...
- SSMM框架
IDEA搭建SSMM框架(详细过程) 相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij ...
- winform最大化后不遮挡任务栏
在窗体初始化后添加一句代码 this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
- iOS_01_C语言简介
1.先学C语言的原因 * oc基于C. * oc 跟 C的思想和语法很多地方不太一样,而且OC能和C混用. * C 是所有编程语言中的经典,很多高级语言都是从C语言中衍生出来的,比如 C++,C#.O ...
- 具体解释。。设计模式5——DAO。。studying
设计模式5--DAO ★ 场景和问题 在Java程序中,常常须要把数据持久化.也须要获取持久化的数据,可是在进行数据持久化的过程中面临诸多问题 (如:数据源不同.存储类型不同.供应商不同.訪问方式不同 ...