CodeForces #308 Div2 E(552E Vanya and Brackets)

2024-06-15 19:08

本文主要是介绍CodeForces #308 Div2 E(552E Vanya and Brackets),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

E. Vanya and Brackets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
3+5*7+8*4
output
303
input
2+3*5
output
25
input
3*4*5
output
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 <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <stack>#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read()
{char c = getchar();while (c < '0' || c > '9') c = getchar();int x = 0;while (c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}return x;
}void Print(int a)
{if(a>9)Print(a/10);putchar(a%10+'0');
}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()
{
//    fread;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));}}printf("%I64d\n",ans);}return 0;
}





这篇关于CodeForces #308 Div2 E(552E Vanya and Brackets)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1064324

相关文章

web前端不可错过的开发工具–Adobe Brackets(开源、简洁强大的HTML、CSS和JavaScript集成开发环境)

Adobe Brackets是一个开源的基于HTML/CSS/JavaScript开发,运行在native shell上的集成开发环境。该项目由Adobe创建和维护,根据MIT许可证发布。提供Windows和OS X平台支持。 Brackets的特点是简约、快捷,没有很多的视图或者面板,它的核心目标是减少在开发过程中那些效率低下的重复性工作,例如浏览器刷新,修改元素的样式,搜索功能等等。

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee

Codeforces April Fools Day Contest 2013

2013年愚人节的坑题。。。 A. Mysterious strings time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Input The input contains a sin

[Codeforces 451B] Sort the Array (实现)

Codeforces - 451B 给定一个序列,其中每个数都不相同 问是否能在翻转其中一段后,整个序列变得单调递增 实现题 首先设一个 B B数组为 AA数组排序后的结果 由于只能翻转一个区间,那么我假装 A是满足要求的 找到最小的 A[l]≠B[l] A[l] \ne B[l],最大的 A[r]≠B[r] A[r] \ne B[r], 翻转的区间将会是 [l,r

[Codeforces 451A] Game With Sticks (博弈)

Codeforces - 451A N根横向木棍,M根纵向木棍组成了一个网格图 每次可以选择一个交点,去掉所有通过这个交点的木棍 两个人交替进行这个游戏,问最后谁能胜利 每次选择的一个交点,必然去掉了一根横向木棍和纵向木棍 所以每次 N和 M都减一 当其中有一个为 0的时候,就是先手必败态 所以只和 N、M中较小的那个的奇偶性有关 #pragma comment(link

[Codeforces 166B] Polygons (点在凸多边形内)

Codeforces - 166B 判断任意多边形 B是否严格在凸多边形 A内部 点在凸多边形内部试板题 如果 B的所有顶点在 A内,则 B在 A内 由于 A的顶点有 105 10^5个,B的顶点有 104 10^4 个 所以不能用 (n) \mathcal{O}(n)的暴力判断 有一个 (logn) \mathcal{O}(logn) 的二分做法 基本原理是用

[Codeforces - Gym10081D (NEERC)] Distribution in Metagonia (构造+数的拆分)

Codeforces - Gym10081D (NEERC) 给定一个数,将其变成若干个数的和 如果这些数有因子,那么只能是 2或 3 并且这些数两两不能除尽 构造题 如果这个数为 1,那么答案就为 1 若不为 1,那么如果他是奇数 那么我减去最大的一个 3次幂转化为一个偶数, 再不断地提出 2,变成一个奇数,循环直到其变为 1 所以这个数 N=3a1+2b1(3a2

[Codeforces - Gym100801H (NEERC)] Hash Code Hacker (字符串构造)

Codeforces - Gym100801H (NEERC) 给定一个字符串hash,为 ∑i=0len−1str[i]×31len−1−i \displaystyle\sum_{i=0}^{len-1} str[i]\times 31^{len-1-i} 求 K K个长度不超过 1000的字符串,使得他们的 hash值相等 其中每个 hash值是 32位有符号整数,K≤1000K\l

Codeforces Round 953 (Div. 2 ABCDEF题) 视频讲解

A. Alice and Books Problem Statement Alice has n n n books. The 1 1 1-st book contains a 1 a_1 a1​ pages, the 2 2 2-nd book contains a 2 a_2 a2​ pages, … \ldots …, the n n n-th book contains