设为首页 收藏本站
查看: 1587|回复: 0

[经验分享] CF E. Vanya and Brackets(添加一对括号使得表达式的值最大)

[复制链接]

尚未签到

发表于 2015-11-24 03:33:13 | 显示全部楼层 |阅读模式
E. Vanya and Brackets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
[size=1em]
Vanya is doing his maths homework. He has an expression of form DSC0000.png ,
where x1, x2, ..., xn are
digits from 1 to 9, and sign DSC0001.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
[size=1em]
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  * .
[size=1em]
The number of signs  *  doesn't exceed 15.

Output
[size=1em]
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
[size=1em]
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
[size=1em]
Note to the second sample test. (2 + 3) * 5 = 25.
[size=1em]
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
[size=1em]
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
const int N = 5005;
#define LL __int64
char fh[N],s[N];  //����ջ�����ʽ
LL num[N];     //����ջ
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>='0'&&s<='9')){
ans=ans*10+s-'0';
flagNum=1;
}
else{
if(flagNum)num[++ntop]=ans; flagNum=0;  ans=0;
if(i==slen)break;
if(s=='+'||s=='-'){
while(ftop&&fh[ftop]!='(') calculate();
fh[++ftop]=s;
}
else if(s=='*'&&i==r){
while(ftop&&fh[ftop]!='(') calculate();   ftop--;
while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate();
fh[++ftop]=s;//printf(&quot;# &quot;);
}
else if(s=='*'||i==l){
while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate();
fh[++ftop]=s;
if(i==l)
fh[++ftop]='(';
}
}
}
while(ftop) calculate();
}
int main(){
while(scanf(&quot;%s&quot;,s)>0){
LL ans=0;
int id[20],k=0;
for(int i=strlen(s); i>=0; i--)
s[i+2]=s;
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=='*')
id[k++]=i;
for(int i=0; i<k-1; i++)
for(int j=i+1; j<k; j++){
countfuction(id,id[j]);
if(num[1]>ans)
ans=num[1];
}
printf(&quot;%I64d\n&quot;,ans);
}
}




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-142789-1-1.html 上篇帖子: UNP卷1:第十一章(名字与地址转换) 下篇帖子: 界面布局动态调整
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表