【NOIP2017】时间复杂度

本题在洛谷上的链接:https://www.luogu.org/problemnew/show/P3952


 

堪称史上最恶心的模拟题!花了我几乎一天时间才调出来。。。

思路没啥好说的,开栈模拟即可,细节说一说。

1、每次处理一个新的程序时,将所有该初始化的都初始化,这点估计都能想到。

2、关于读入语句,可以一次读一行,也可以分开读,建议分成三个变量读,我用的一次读一行,坑死了!不过趁机学了一下c++关于读入行的知识,cin.getline(char*,int)用来操作字符数组,需要指定读入字符串大小,getline(istream,string)用来操作字符串,从指定流中读取一行。非常坑的一点,cin读字符串的时候,并不会读换行符,如果在此之后紧接着用getline读了一行,会什么也读不到,需要手动用getchar()把换行符杀掉。

3、如果遇到语法错误,别急着跳出,还要把剩下的读完。这个坑了我好久,最后把读入的全输出才发现。。。

剩下的自己看代码吧,估计也没耐心看,还是自己写慢慢调最靠谱。。。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <stack>
 6 
 7 using namespace std;
 8 
 9 int vis[130];
10 
11 stack<char> s, s2, s3;
12 
13 int main() {
14     int t, l;
15     string cpx, com;
16     cin >> t;
17     while (t--) {
18         int flag = 1, now = 0, ans = 0;
19         memset(vis, 0, sizeof(vis));
20         while (!s.empty()) s.pop();
21         while (!s2.empty()) s2.pop();
22         while (!s3.empty()) s3.pop();
23         cin >> l >> cpx;
24         getchar();
25         for (int i = 1; i <= l; ++i) {
26             getline(cin, com);
27             if (!flag) continue;
28             if (com[0] == 'E') {
29                 if (s.empty()) {
30                     flag = 0;
31                     continue;
32                 }
33                 else {
34                     if (s3.empty()) {
35                         if (!s2.empty() && s.top() == s2.top()) s2.pop();
36                         else --now;
37                     } else if (s3.top() == s.top()) s3.pop();
38                     vis[(int)s.top()] = 0;
39                     s.pop();
40                 }
41             } else {
42                 char x = com[2];
43                 if (vis[(int)x]) {
44                     flag = 0;
45                     continue;
46                 }
47                 else {
48                     vis[(int)x] = 1;
49                     s.push(x);
50                 }
51                 int a = 0, b = 0, p = 4;
52                 if (com[p] == 'n') a = 10005, ++p;
53                 else while (com[p] != ' ')
54                     a = a * 10 + com[p++] - '0';
55                 ++p;
56                 if (com[p] == 'n') b = 10005;
57                 else while (com[p])
58                     b = b * 10 + com[p++] - '0';
59                 if (a > b) s3.push(x);
60                 if (a < b && a < 10005 && b < 10005 && s3.empty())
61                     s2.push(x);
62                 if (a < b && b == 10005 && s3.empty()) ++now;
63                 ans = max(ans, now);
64             }
65         }
66         if (!flag || !s.empty()) {
67             printf("ERR\n");
68             continue;
69         }
70         if (cpx[2] == '1' && cpx[3] == ')') {
71             if (!ans) printf("Yes\n");
72             else printf("No\n");
73         } else {
74             int aans = 0, p = 4;
75             while (cpx[p] != ')')
76                 aans = aans * 10 + cpx[p++] - '0';
77             if (aans == ans) printf("Yes\n");
78             else printf("No\n");
79         }
80     }
81     return 0;
82 }
AC代码

 

转载于:https://www.cnblogs.com/Mr94Kevin/p/9812771.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值