CF E. Vanya and Brackets(添加一对括号使得表达式的值最大)

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 __int64char 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[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);}}

,放弃等于又一次可以选择的机会。

CF E. Vanya and Brackets(添加一对括号使得表达式的值最大)

相关文章:

你感兴趣的文章:

标签云: