题目传送门
1 /*
2 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式
3 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数
4 岛娘的代码实在难懂啊~
5 */
6 /************************************************
7 * Author :Running_Time
8 * Created Time :2015-8-16 14:29:49
9 * File Name :K.cpp
10 ************************************************/
11
12 #include <cstdio>
13 #include <algorithm>
14 #include <iostream>
15 #include <sstream>
16 #include <cstring>
17 #include <cmath>
18 #include <string>
19 #include <vector>
20 #include <queue>
21 #include <deque>
22 #include <stack>
23 #include <list>
24 #include <map>
25 #include <set>
26 #include <bitset>
27 #include <cstdlib>
28 #include <ctime>
29 using namespace std;
30
31 #define lson l, mid, rt << 1
32 #define rson mid + 1, r, rt << 1 | 1
33 typedef long long ll;
34 const int MAXN = 1e3 + 10;
35 const int INF = 0x3f3f3f3f;
36 const int MOD = 1e9 + 7;
37 char str[MAXN];
38
39 int main(void) { //ZOJ 3829 Known Notation
40 int T; scanf ("%d", &T);
41 while (T--) {
42 scanf ("%s", str);
43 int x = 0, len = strlen (str);
44 for (int i=0; i<len; ++i) {
45 if (str[i] == '*') x++;
46 }
47 if (x == 0) {
48 puts ("0"); continue;
49 }
50 int ans = max (x + 1 - (len - x), 0); int n = ans;
51 for (int i=0; i<len; ++i) {
52 if (str[i] == '*') {
53 if (n <= 1) n++, ans++;
54 else n--;
55 }
56 else n++;
57 }
58 printf ("%d
", ans);
59 }
60
61 return 0;
62 }