zoukankan      html  css  js  c++  java
  • CodeForces

    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 to 9, 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).

    Source

     
    解题:暴力瞎搞,注意int溢出,傻逼逼的把栈写成int了。。。哎吸取教训
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 vector<int>pos;
     5 stack<LL>num;
     6 stack<char>op;
     7 LL calc(const string &str) {
     8     while(!num.empty()) num.pop();
     9     while(!op.empty()) op.pop();
    10     for(int i = 0,slen = str.length(); i < slen; ++i) {
    11         if(str[i] == ')') {
    12             while(op.top() != '(') {
    13                 LL tmp = num.top();
    14                 num.pop();
    15                 if(op.top() == '*') num.top() *= tmp;
    16                 else if(op.top() == '+') num.top() += tmp;
    17                 op.pop();
    18             }
    19             op.pop();
    20             continue;
    21         }
    22         if(isdigit(str[i])) num.push(str[i] - '0');
    23         else if(str[i] == '+' && !op.empty() && op.top() == '*') {
    24             while(!op.empty() && op.top() == '*') {
    25                 LL tmp = num.top();
    26                 num.pop();
    27                 num.top() *= tmp;
    28                 op.pop();
    29             }
    30             op.push(str[i]);
    31         } else op.push(str[i]);
    32     }
    33     while(!op.empty()) {
    34         LL tmp = num.top();
    35         num.pop();
    36         if(op.top() == '*') num.top() *= tmp;
    37         else if(op.top() == '+') num.top() += tmp;
    38         op.pop();
    39     }
    40     return num.top();
    41 }
    42 int main() {
    43     string str;
    44     cin>>str;
    45     pos.push_back(-1);
    46     int slen = str.length();
    47     for(int i = 1; i < slen; i += 2)
    48         if(str[i] == '*') pos.push_back(i);
    49     pos.push_back(slen);
    50     slen = pos.size();
    51     LL ret = INT_MIN;
    52     for(int i = 0; i+1 < slen; ++i)
    53         for(int j = i+1; j < slen; ++j) {
    54             string s = str;
    55             s.insert(pos[i]+1,1,'(');
    56             s.insert(pos[j]+1,1,')');
    57             ret = max(ret,calc(s));
    58         }
    59     cout<<ret<<endl;
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    RHEL 6.3安装(超级详细图解教程)[转载]
    CentOS下设置vimrc,添加文件注释信息以及设置tab 键为4 格
    Centos 设置时区和时间以及增加中文输入法
    虚拟机上CentOS6.5 无法上网的解决方法
    LoadRunner 11安装及测试环境搭建
    LR11录制回放出现中文乱码以及录制时一直跳到360浏览器的解决方法
    第 3 章 变量和表达式
    第 2 章 编写 C# 程序
    第 1 章 C# 简介
    jQuery Mobile的学习时间bottonbutton的事件学习
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4660756.html
Copyright © 2011-2022 走看看