E. Vanya and Brackets(Codeforces Round #308 (Div. 2))
来源:互联网 发布:6数字域名 编辑:程序博客网 时间:2024/05/20 02:27
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.
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.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
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).
给你一个表达式,只有乘号和加号,数字都是1到9,要求加一个括号,使得表达式的值最大,问最大是多少。乘号个数<=15。
左括号一定在乘号右边,右括号一定在乘号左边,因为如果不是这样的话,一定可以调整括号的位置使表达式的值增大。这个应该不难想。
于是只要枚举括号的位置然后计算表达式即可。
转载请注明出处:寻找&星空の孩子
题目链接:http://codeforces.com/contest/552/problem/E
#include<stdio.h>#include<iostream>#include<string.h>#include<math.h>using namespace std;const int N = 5005;#define LL __int64char fh[N],s[N]; LL num[N]; int ftop,ntop ,slen; void calculate(){ if(fh[ftop]=='+') num[ntop-1]+=num[ntop],ntop--; else if(fh[ftop]=='-') num[ntop-1]-=num[ntop],ntop--; else if(fh[ftop]=='*') num[ntop-1]*=num[ntop],ntop--; else if(fh[ftop]=='/') num[ntop-1]/=num[ntop],ntop--; ftop--;}void countfuction(int l,int r){ ftop=0;ntop=0; int flagNum=0; LL ans=0; for(int i=0; i<=slen; ++i){ if(i!=slen&&(s[i]>='0'&&s[i]<='9')){ ans=ans*10+s[i]-'0'; flagNum=1; } else{ if(flagNum)num[++ntop]=ans; flagNum=0; ans=0; if(i==slen)break; if(s[i]=='+'||s[i]=='-'){ while(ftop&&fh[ftop]!='(') calculate(); fh[++ftop]=s[i]; } else if(s[i]=='*'&&i==r){ while(ftop&&fh[ftop]!='(') calculate(); ftop--; while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate(); fh[++ftop]=s[i];//printf("# "); } else if(s[i]=='*'||i==l){ while(ftop&&(fh[ftop]=='*'||fh[ftop]=='/')) calculate(); fh[++ftop]=s[i]; if(i==l) fh[++ftop]='('; } } } while(ftop) calculate();}int main(){ while(scanf("%s",s)>0){ LL ans=0; int id[20],k=0; for(int i=strlen(s); i>=0; i--) s[i+2]=s[i]; s[0]='1'; s[1]='*'; slen=strlen(s); s[slen]='*'; s[slen+1]='1'; s[slen+2]='\0'; slen=strlen(s); for(int i=0; i<slen; i++) if(s[i]=='*') id[k++]=i; for(int i=0; i<k-1; i++) for(int j=i+1; j<k; j++){ countfuction(id[i],id[j]); if(num[1]>ans) ans=num[1]; } printf("%I64d\n",ans); }}
或着
#include <bits/stdc++.h>using namespace std;int n;string str;vector<int> pos;stack<char> sc;stack<long long> sn;long long twoResult(long long a, long long b, char c) { return (c == '*') ? a * b : a + b;}void cal() { char t = sc.top(); sc.pop(); long long a = sn.top(); sn.pop(); long long b = sn.top(); sn.pop(); sn.push(twoResult(a, b, t));}long long expression(string s) { for (int i = 0; i < s.length(); i++) { char c = s[i]; if (isdigit(c)) { sn.push(c - '0'); } else if (c == '(') { sc.push(c); } else if (c == ')') { while (sc.top() != '(') cal(); sc.pop(); } else { if (c == '+') { while (!sc.empty() && sc.top() == '*') cal(); sc.push(c); } else sc.push(c); } } while (!sc.empty()) cal(); return sn.top();}int main() { cin >> str; n = str.size(); 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(); long long ans = 0; for (int i = 0; i < len - 1; i++) { // left for (int j = i + 1; j < len; j++) { // right string s = str; s.insert(pos[i] + 1, 1, '('); s.insert(pos[j] + 1, 1, ')'); ans = max(ans, expression(s)); } } cout << ans << endl; return 0;}
- E. Vanya and Brackets(Codeforces Round #308 (Div. 2))
- Codeforces Round #308 (Div. 2) E. Vanya and Brackets
- codeforces #308 E. Vanya and Brackets
- Codeforces Round #280 (Div. 2) E. Vanya and Field
- Codeforces Round #355 (Div. 2)E. Vanya and Balloons
- A. Vanya and Table(Codeforces Round #308 (Div. 2))
- B. Vanya and Books(Codeforces Round #308 (Div. 2))
- C. Vanya and Scales(Codeforces Round #308 (Div. 2))
- D. Vanya and Triangles(Codeforces Round #308 (Div. 2))
- Codeforces Round #288 (Div. 2)E. Arthur and Brackets
- Codeforces Round #223 (Div. 2) E Sereja and Brackets
- Codeforces Round #280 (Div. 2) E. Vanya and Field (数学GCD)
- codeforces#308-E - Vanya and Brackets-枚举+表达式计算
- codeforces 552E Vanya and Brackets(暴力)
- Codeforces 552E - Vanya and Brackets (中缀表达式求值)
- Codeforces 552E Vanya and Brackets
- Codeforces 552E Vanya and Brackets
- CodeForces-552E. Vanya and Brackets
- UVA 1103 Ancient Messages (DFS)
- C#垃圾回收和托管堆及堆栈
- struts2 总结
- [数据结构]埃及分数(暴力,解答树,迭代加深搜索)
- 2015062008 - 后退一步
- E. Vanya and Brackets(Codeforces Round #308 (Div. 2))
- 黑马程序员——着急惹的祸
- 6.20 androidstudio中使用TestCase
- 2015062009 - 端午祭
- SSH整合
- 卷积神经网络CNN
- fatal error C1189: #error : missing -D__STDC_CONSTANT_MACROS / #define __STDC_CONSTANT_MACROS
- 布尔表达式的验证
- Ternary Expression