zoukankan      html  css  js  c++  java
  • Vanya and Brackets

    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]=='
    ') 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
    ",ans);
        return 0;
    }
    View Code

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

    然后因为乘号至多有15个,所以暴力一遍就行了。

  • 相关阅读:
    HTML + CSS短标题(二,三,四文字长度)两端对齐的方式
    转载 ----MAC 上搭建lua
    转载 -- 基于原生JS与OC方法互相调用并传值(附HTML代码)
    转载 ---资深HR告诉你:我如何筛选简历与选择人员的
    转载 OSX开发推荐书籍列表
    IOS --支付宝SDK 分解讲解
    IOS -RSA加密及解密
    ios --转载 使用SMSSDK实现短信验证:
    ios --转载 在mac上安装让rvm及cocoa pods详解
    简单说明Python中的装饰器的用法
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5428141.html
Copyright © 2011-2022 走看看