CF E. Vanya and Brackets(添加一对括号使得表达式的值最大)
2015-06-20 13:38
387 查看
E. Vanya and Brackets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vanya is doing his maths homework. He has an expression of form
![](http://codeforces.com/predownloaded/03/e7/03e7806690e89572eb262e573be02d4b33f3abf8.png)
,
where x1, x2, ..., xn are
digits from 1 to 9, and sign
![](http://codeforces.com/predownloaded/1d/58/1d58186ddbda10b386e18a275ace82bea3a578ff.png)
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.
Sample test(s)
input
output
input
output
input
output
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).
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Vanya is doing his maths homework. He has an expression of form
![](http://codeforces.com/predownloaded/03/e7/03e7806690e89572eb262e573be02d4b33f3abf8.png)
,
where x1, x2, ..., xn are
digits from 1 to 9, and sign
![](http://codeforces.com/predownloaded/1d/58/1d58186ddbda10b386e18a275ace82bea3a578ff.png)
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.
Sample test(s)
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).
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> using namespace std; const int N = 5005; #define LL __int64 char fh ,s ; //����ջ�����ʽ LL num ; //����ջ int ftop,ntop ,slen; //����ջ��������ջ�� void calculate(){ if(fh[ftop]=='+') num[ntop-1]+=num[ntop],ntop--; else if(fh[ftop]=='-') num[ntop-1]-=num[ntop],ntop--; else if(fh[ftop]=='*') num[ntop-1]*=num[ntop],ntop--; else if(fh[ftop]=='/') num[ntop-1]/=num[ntop],ntop--; ftop--; } void countfuction(int l,int r){ ftop=0;ntop=0; int flagNum=0; LL ans=0; for(int i=0; i<=slen; ++i){ if(i!=slen&&(s[i]>='0'&&s[i]<='9')){ ans=ans*10+s[i]-'0'; flagNum=1; } else{ if(flagNum)num[++ntop]=ans; flagNum=0; ans=0; if(i==slen)break; if(s[i]=='+'||s[i]=='-'){ while(ftop&&fh[ftop]!='(') calculate(); fh[++ftop]=s[i]; } else if(s[i]=='*'&&i==r){ while(ftop&&fh[ftop]!='(') calculate(); ftop--; while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate(); fh[++ftop]=s[i];//printf("# "); } else if(s[i]=='*'||i==l){ while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate(); fh[++ftop]=s[i]; if(i==l) fh[++ftop]='('; } } } while(ftop) calculate(); } int main(){ while(scanf("%s",s)>0){ LL ans=0; int id[20],k=0; for(int i=strlen(s); i>=0; i--) s[i+2]=s[i]; s[0]='1'; s[1]='*'; slen=strlen(s); s[slen]='*'; s[slen+1]='1'; s[slen+2]='\0'; slen=strlen(s); for(int i=0; i<slen; i++) if(s[i]=='*') id[k++]=i; for(int i=0; i<k-1; i++) for(int j=i+1; j<k; j++){ countfuction(id[i],id[j]); if(num[1]>ans) ans=num[1]; } printf("%I64d\n",ans); } }
相关文章推荐
- Java串口通信详细解释
- java产生任意范围内不重复的随机数
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
- 黑马程序员--java学习笔记第七天
- VS中的活动debug和活动cpu
- leetcode--Roman to Integer
- Explicit keyword
- 【安德鲁斯】基于脚本的数据库"增量更新",如果不改变,每次更新java代码、!
- Spring中的AOP简述
- win7硬盘安装ubuntu双系统的方法
- 游戏有这么好玩么?
- USACO——Milking Cows 挤牛奶
- 如何在 Ubuntu/Debian/Linux Mint 中编译和安装 wxWidgets
- JavaScript里call,apply,bind方法简介
- FZU 2192 位置信息挖掘 (种类并查集)
- EditPlus保存时不生成bak文件(转)
- nginx ats squid varnish使用场景对比介绍
- Android学习第三课:I/O文件读写操作(一)
- CSU - 1556 Jerry's trouble(快速幂)
- 列表中li标签设置margin在IE6和IE7中第一个行失效的解决方法