CodeForces-552E. Vanya and Brackets
来源:互联网 发布:js文件代码的执行顺序 编辑:程序博客网 时间:2024/05/18 02:17
/*CF e题 给定一个表达式,只添加一对括号,使得这个表达式的值最大* 由于乘号比较少,枚举括号的位置就行;* 左括号的位置一定在乘号的右边,右括号的位置一定在乘号的左边,* 这样才能使得表达式的值最大,否则移动括号一定能找到比较大的值*/#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}stack<ll> num;stack<ll> num2;stack<char> ch;ll calc(string & s){ while(num.size())num.pop(); while(num2.size())num2.pop(); while(ch.size())ch.pop(); int n=s.size(); bool have=0; int k1,k2; for(int i=0;i<n;i++) { if(s[i]=='(') { have=1; k1=i; ++i; while(s[i]!=')') { if(s[i]=='*') { ch.push(s[i]); } else if(s[i]>='0' && s[i]<='9') { if(ch.size() && ch.top()=='*') { ll t1 = s[i]-'0'; ll t2 = num.top(); num.pop(); ch.pop(); num.push(t1*t2); } else num.push(s[i]-'0'); } ++i; } k2=i; break; } } ll tmp=0; while(num.size()) { tmp+=num.top(); num.pop(); } string t="."; if(have)s.replace(k1,k2-k1+1,t); //cout<<s<<endl; n=s.size(); for(int i=0;i<n;i++) { if(s[i]=='*')ch.push(s[i]); else if((s[i]>='0' && s[i]<='9') || s[i]=='.') { if(ch.size() && ch.top()=='*') { ll t1; if(s[i]!='.')t1 = s[i]-'0'; else t1 = tmp; ll t2 = num.top(); num.pop(); ch.pop(); num.push(t1*t2); } else num.push(s[i]=='.'? tmp:s[i]-'0'); } } ll res=0; while(num.size()) { res+=num.top(); //cout<<num.top()<<endl; num.pop(); } //cout<<"res=="<<res<<endl; return res;}vector<int> vec;int main(){ string s,t; cin>>s; t=s; vec.clear(); int n=s.size(); vec.push_back(-1); for(int i=1;i<n;i+=2) { if(s[i]=='*') { vec.push_back(i); } } vec.push_back(n); n=vec.size(); ll ans=calc(s); for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { s=t; s.insert(vec[i]+1,1,'('); s.insert(vec[j]+1,1,')'); //cout<<s<<endl; ans=max(ans,calc(s)); } } cout<<ans<<endl; return 0;}
0 0
- Codeforces 552E Vanya and Brackets
- Codeforces 552E Vanya and Brackets
- CodeForces-552E. Vanya and Brackets
- codeforces 552 E Vanya and Brackets
- codeforces 552 E. Vanya and Brackets
- 【39.29%】【codeforces 552E】Vanya and Brackets
- codeforces 552E Vanya and Brackets(暴力)
- Codeforces 552E - Vanya and Brackets (中缀表达式求值)
- codeforces #308 E. Vanya and Brackets
- E. Vanya and Brackets(Codeforces Round #308 (Div. 2))
- Codeforces Round #308 (Div. 2) E. Vanya and Brackets
- codeforces#308-E - Vanya and Brackets-枚举+表达式计算
- cf #308 E. Vanya and Brackets (暴力枚举)
- CodeForces 492E Vanya and Field
- CodeForces #280 E Vanya and Field
- CodeForces #308 Div2 E(552EVanya and Brackets)
- Codeforces 508E Arthur and Brackets
- codeforces 508 E. Arthur and Brackets
- Git的使用
- java并发编程之CountDownLatch
- Bash shell. Metacharacters
- 2015062603 - 你的位置
- C结构体的用法详解
- CodeForces-552E. Vanya and Brackets
- swift 类型转换
- 华为机试—去掉最值剩下的个数
- Certificates, Indentifiers & Profiles
- 利用矩阵的迹巧妙解决矩阵的求导问题
- Google Map for iOS SDK 开发之沿街道渲染轨迹
- poj3414有向图的搜索BFS+路径记录
- 2015062604 - 智者
- UITextField 弹出输入面板后,点击其他地方使其消失