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 */