码迷,mamicode.com
首页 > 其他好文 > 详细

Vanya and Brackets

时间:2016-04-24 21:40:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

Vanya and Brackets
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Vanya is doing his maths homework. He has an expression of form 技术分享, where x1, x2, ..., xn are digits from 1 to9, 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.

Sample Input

Input
3+5*7+8*4
Output
303
Input
2+3*5
Output
25
Input
3*4*5
Output
60

Hint

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<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个,所以暴力一遍就行了。

Vanya and Brackets

标签:

原文地址:http://www.cnblogs.com/superxuezhazha/p/5428141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!