UVA 12902 Reverse Polish Notation

//跟wyr学的//其实是贪心//题解稍后补上

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #include<stack>
11 #include<string>
12 #define LL long long
13
14 const int MAXN=0;
15 const int MAXM=0;
16 const int INF=2000000000;
17
18 using namespace std;
19
20 int T;
21 char s[100007];
22
23 int solve(){
24     int len=strlen(s);
25     int MIN=INF;
26     int now=0;
27     int flag=0;
28     for (int i=0;i<len;i++){
29             if (s[i]==‘a‘)
30                 now++;
31             else
32                 now--;
33             if (now==MIN) flag=0;
34             if (now<MIN){
35                     MIN=now;
36                     flag=1;
37             }
38     }
39     if (MIN>=1) return now-1;
40     if (MIN==now) return 1+abs(now);
41     return 1+now-MIN*2-flag;
42 }
43
44 int main(){
45     scanf("%d",&T);
46     for (int cas=1;cas<=T;cas++){
47             scanf("%s",s);
48             int ans=solve();
49             if (s[0]==‘+‘ && s[1]==‘a‘ && s[2]==‘a‘){
50                     s[0]=‘a‘;
51                     s[1]=‘a‘;
52                     s[2]=‘+‘;
53                     ans=min(ans,solve()+2);
54             }
55             printf("Case %d: %d\n",cas,ans);
56     }
57     return 0;
58 }
59 /*
60 4
61 a
62 a+a
63 +aa
64 aa++++a
65 */

时间: 2024-08-09 12:52:52

UVA 12902 Reverse Polish Notation的相关文章

leetcode-Evaluate the value of an arithmetic expression in Reverse Polish Notation

leetcode 逆波兰式求解 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

LeetCode150 Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation.  (Medium) Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

Evaluate Reverse Polish Notation

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Example ["2", "1", "+", "3", "*"] -> ((2

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

Evaluate Reverse Polish Notation——LeetCode

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

[Leetcode] evaluate reverse polish notation 计算逆波兰表达式

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are+,-,*,/. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -> (

Evaluate Reverse Polish Notation(堆栈)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g