贪心+模拟 ZOJ 3829 Known Notation

 

题目传送门

 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\n", ans);
59     }
60 
61     return 0;
62 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4734944.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值