首页 > 其他分享 >Codeforces Round #308 (Div. 2) E. Vanya and Brackets

Codeforces Round #308 (Div. 2) E. Vanya and Brackets

时间:2023-04-24 22:36:51浏览次数:35  
标签:308 Vanya Brackets ll pos Note ans expression outputCopy


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.

Examples
inputCopy
3+5*7+8*4
outputCopy
303
inputCopy
2+3*5
outputCopy
25
inputCopy
3*4*5
outputCopy
60
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<bits/stdc++.h>
using namespace std;
#define maxn 500005
#define inf 0x3f3f3f3f
typedef long long ll;
ll qpow(ll a,ll b)
{
    ll ans=1;
    while(b){
        if(b%2)ans=ans*a;
        a=a*a;
        b=b/2;
    }
    return ans;
}
vector<int> pos;
stack<ll>num;
stack<char> ch;

ll cal(string s)
{
    while(!num.empty())num.pop();
    while(!ch.empty())ch.pop();
    for(int i=0;i<s.length();i++){
        char c=s[i];
        if(c>='0'&&c<='9')num.push(c-'0');
        else if(c=='('){
              ch.push(c);
        }
        else if(c==')'){
            while(ch.top()!='('){
                    char x=ch.top();ch.pop();
                    ll a=num.top();num.pop();
                    ll b=num.top();num.pop();
                    if(x=='*')num.push(a*b);
                    else num.push(a+b);
        }
        ch.pop();
    }
        else {
            if(c=='+'){
                while(!ch.empty()&&ch.top()=='*'){
                    char x=ch.top();ch.pop();
                    ll a=num.top();num.pop();
                    ll b=num.top();num.pop();
                    if(x=='*')num.push(a*b);
                    else num.push(a+b);
                }
                ch.push(c);
            }
            else ch.push(c);
        }
}
        while(!ch.empty()){
            char x=ch.top();ch.pop();
            ll a=num.top();num.pop();
            ll b=num.top();num.pop();
            if(x=='*')num.push(a*b);
            else num.push(a+b);
        }
        return num.top();
}
int main()
{
    ios::sync_with_stdio(false);
    string str;
    while(cin>>str){
        int n=str.size();
        pos.clear();
        pos.push_back(-1);
        for(int i=1;i<n;i+=2){
            if(str[i]=='*'){
                pos.push_back(i);
            }
        }
        pos.push_back(n);
        int len=pos.size();
        ll ans=0;
        for(int i=0;i<len-1;i++){
            for(int j=i+1;j<len;j++){
                string s=str;
                s.insert(pos[i]+1,1,'(');
                s.insert(pos[j]+1,1,')');
                ans=max(ans,cal(s));

            }
        }
        cout<<ans<<endl;
    }

}


标签:308,Vanya,Brackets,ll,pos,Note,ans,expression,outputCopy
From: https://blog.51cto.com/u_15657999/6221937

相关文章

  • AP6308PFM 升压型 2.6-6.5V 三节锂电池充电控制集成电路
    AP6308是一款工作于2.7V到6.5V的PFM升压型三节锂电池充电控制集成电路。AP6308采用恒流和准恒压模式(Quasi-CVTM)对电池进行充电管理,内部集成有基准电压源,电感电流检测单元,电池电压检测电路和片外场效应晶体管驱动电路等,具有外部元件少,电路简单等优点。当接通输入电源后,AP6308进入......
  • 每日学习记录20230308_继续PNAS代码解析
    20230308:PNSA代码解析PNAS和YF代码比较特征PNASYF输入数据DDADIA算法线性回归LASSOFA数据Ifwewanttoissueapackage,whatimprovementshouldwedounderlieYF’sprogram?BothDDAandDIAdataaresupported.achangePNAS......
  • 洛谷P1308统计单词数,strtok函数的使用以及对于单词分割的一些思考
    [NOIP2011普及组]统计单词数题目描述一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意......
  • CodeForces - 149D Coloring Brackets(区间DP)
    题目大意:给你一个符合括号规则的字符串,现在要求你将这些括号染色,染色规则如下1.一个括号要么被染成红色,要么被染成蓝色,要么不染2.相邻的括号的颜色不能相同(可以同为无色)3.成对的括号只能有一个被染色问染色的方案数解题思路:0表示不染,1表示红色,2表示蓝色那么成对的括号......
  • POJ - 2955 Brackets(区间dp)
    题目大意:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度解题思路:区间dp,用dp[i][j]表示[i,j]这个区间内有符合规则的最长子串的长度如果str[i]和str[j]能构成()或者[],那么dp[i][j]=dp[i+1][j-1]+2剩下的情况就是dp[i][j]=max(dp[i][j],dp[i][k]+dp[k......
  • HDU - 3081 Marriage Match II(二分图最大匹配 + 并查集)
    题目大意:有n个男生n个女生,现在要求将所有的男女配对,所有的男女都配对的话,算该轮游戏结束了。接着另一轮游戏开始,还是男女配对,但是配对的男女不能是之前配对过的。问这游戏能玩多少轮男女能配对的条件如下1.男女未曾吵架过。2.如果两个女的是朋友,那么另一个女的男朋友可以和......
  • Node.js17或更高版本中出现Error: error:0308010C:digital envelope routines::unsupp
    问题描述我在运行别人的Vue项目的时候报各种错误,提示XXX/node_modules/.bin/vue-cli-service:Permissiondenied权限不足的问题。还有一个问题就是:出现Error:error:0308010C:digitalenveloperoutines::unsupported。在网上也查看了解决办法,没有解决。(我之前在Nodejs官网安装......
  • 《安富莱嵌入式周报》第308期:开源带软硬件安全认证的PLC设计,开源功率计,可靠PID实现,PR2
    周报汇总地址:http://www.armbbs.cn/forum.php?mod=forumdisplay&fid=12&filter=typeid&typeid=104 视频版:https://www.bilibili.com/video/BV1F24y157QE1、ST发布安全认证版PLC设计套件https://www.st.com/en/evaluation-tools/steval-silplc01.html含原理图(新的手册......
  • luogu P3308 [SDOI2014]LIS
    题面传送门涨知识了,第一次知道网络流删边不用全图重跑。首先我们先跑一个暴力dp,出\(f_i\)表示以\(i\)结尾的最长上升子序列长度。然后我们将其按照这个dp值分层,相......
  • Error message "error:0308010C:digital envelope routines::unsupported"
    由于升级Nodejs版本造成的,一般创建项目时为16.7.0版本,然后安装或升级了更高版本,再进行run的时候,会提示。Errormessage"error:0308010C:digitalenveloperoutines::unsu......