洛谷 P3952 时间复杂度 题解

版权声明:随便转载。 https://blog.csdn.net/zhang14369/article/details/80555706

作者:岸芷汀兰

一、题目:

洛谷原题
题目图片

二、思路:

很显然是一道大模拟题,数据结构用
但坑点比较多,先来看一下我的惨痛经历。
惨痛经历
主要有以下坑点:

  • 字符串的读入;
  • 时间复杂度是取最大值
  • 判错(包括变量的判重,我用了STL中的set

三、代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<set>
#include<algorithm>

using namespace std;
inline int read(void) {
    int x = 0, f = 1; char ch = getchar();
    while (ch<'0' || ch>'9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0'&&ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}

int T;

struct Messege {
    char bianliang;
    int index;
    //int begin, end;
    bool in;
};

inline int turn(char ch[]) {
    int ans = 0;
    for (register int i = 1; i <= strlen(ch + 1); i++) {
        ans = ans * 10 + ch[i] - '0';
    }
    return ans;
}

inline bool is_number(char ch[]) {
    if (ch[1] >= '0'&&ch[1] <= '9')return true;
    return false;
}

int main()
{
    T = read();
    while (T--) {
        set<char>flag; flag.clear();

        stack<Messege>sta;
        int n = read(), index = 0;
        bool ok = true, is_constant = true;
        char str_fuzadu[100];
        scanf("%s", str_fuzadu + 1);
        for (register int i = 1; i <= n; i++) {
            char opt[5];
            scanf("%s", opt + 1);
            if (opt[1] == 'F') {// F
                Messege top;
                char bl[5], st[5], en[5];
                scanf("%s%s%s", bl + 1, st + 1, en + 1);

                if (!ok)continue;

                if(flag.count(bl[1])){ 
                    puts("ERR"); ok = false; continue;
                }
                top.bianliang = bl[1]; flag.insert(bl[1]);

                top.in = true; top.index = 0;

                if (sta.size())top.index = sta.top().index;

                if (sta.size() && !sta.top().in) {
                    top.in = false;
                    sta.push(top);
                    continue;
                }

                if (is_number(st)) {
                    if (is_number(en)) {
                        int start = turn(st), end = turn(en);
                        if (start > end)top.in = false;
                        sta.push(top);
                    }
                    else {
                        is_constant = false;
                        top.index++;
                        sta.push(top);
                    }
                }
                else {
                    if (is_number(en)) {
                        top.in = false;
                        sta.push(top);
                    }
                    else {
                        sta.push(top);
                    }
                }
            }
            else {// E
                if (!ok)continue;
                if (sta.empty()) { 
                    puts("ERR"); ok = false; continue;
                }
                Messege top = sta.top(); sta.pop();
                index = max(index, top.index);
                flag.erase(top.bianliang);
            }

        }

        if (!ok) { continue; }

        if (sta.size()) { 
            puts("ERR"); continue; }

        if (is_constant) {
            if (str_fuzadu[3] == '1')puts("Yes");
            else puts("No");
        }
        else {
            if (str_fuzadu[3] == '1')puts("No");
            else {
                int fuza = 0;
                for (register int i = 1; i <= strlen(str_fuzadu + 1); i++) {
                    if (str_fuzadu[i] >= '0'&&str_fuzadu[i] <= '9')
                        fuza = fuza * 10 + str_fuzadu[i] - '0';
                }
                if (fuza == index)puts("Yes");
                else puts("No");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhang14369/article/details/80555706
今日推荐