zoukankan      html  css  js  c++  java
  • 【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
    ");
    68             continue;
    69         }
    70         if (cpx[2] == '1' && cpx[3] == ')') {
    71             if (!ans) printf("Yes
    ");
    72             else printf("No
    ");
    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
    ");
    78             else printf("No
    ");
    79         }
    80     }
    81     return 0;
    82 }
    AC代码
  • 相关阅读:
    20190710-汉诺塔算法
    20190705-Python数据驱动之DDT
    20190621-N皇后
    还在为Excel合并单元格导致的各种问题烦恼吗?这里一起解决
    Excel基础:开始菜单之对齐方式,那些被遗忘的实用功能
    Excel中身份证号码如何分段显示,难倒小编,有什么好方法吗?
    制作这样的Excel注水图表,让老板另眼相看,坐等升职加薪
    Excel高手都会的Shift快捷键7个用法,让工作效率翻倍
    Excel答粉丝问:批量将单元格内容转为批注
    Excel基础:开始菜单之字体的华丽转身
  • 原文地址:https://www.cnblogs.com/Mr94Kevin/p/9812771.html
Copyright © 2011-2022 走看看