您的位置:首页 > 其它

Vanya and Brackets

2016-04-24 20:45 239 查看
Vanya and Brackets
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 552E

Description

Vanya is doing his maths homework. He has an expression of form

#include<iostream>
#include<stdio.h>
using namespace std;
char exp[50005];
int t=3;
long long  cal(int left,int right)
{
long long  now=0;
long long  ans=0;
if(!left)
{
for(int i=1; i<t-1; i++)
{
if(exp[i]=='+')
{
if(now)
{
ans+=now;
now=0;
}
}
else if(exp[i]=='*')
{
now*=(exp[i+1]-'0');
i++;
}
else now=exp[i]-'0';
//cout<<now<<endl;
}
ans+=now;
}
else
{
long long tr=0;
long long pos=0;
for(int i=left+1; i<right; i++)
{
if(exp[i]=='+')
{
if(pos)
{
tr+=pos;
pos=0;
}
}
else if(exp[i]=='*')
{
pos*=(exp[i+1]-'0');
i++;
}
else pos=exp[i]-'0';
}
tr+=pos;
//cout<<tr<<endl;
for(int i=1; i<t-1; i++)
{
if(exp[i]=='+')
{
if(now)
{
ans+=now;
now=0;
}
}
else if(exp[i]=='*')
{
if(i==left)
{
now*=tr;
i=right-1;
}
else
{
now*=(exp[i+1]-'0');
i++;
}
}
else now=exp[i]-'0';
}
ans+=now;
}
return ans;
}
int main()
{

while(t)
{
exp[t++]=getchar();
if(exp[t-1]=='\n') break;
}
exp[1]='1';
exp[2]='*';
exp[t-1]='*';
exp[t++]='1';
/*for(int i=1;i<t;i++)
cout<<exp[i]<<" ";
//cout<<cal(0,0);*/
long long ans=cal(0,0);
for(int i=1; i<t-1; i++)
{
if(exp[i]=='*')
{
for(int j=i+1; j<t-1; j++)
{
if(exp[i]=='*')
{
ans=max(ans,cal(i,j));
}
}
}
}
printf("%I64d\n",ans);
return 0;
}


View Code
这道题就是要想明白括号为什么要必须放在两个乘号的之间。

然后因为乘号至多有15个,所以暴力一遍就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: