[Luogu] 时间复杂度

https://www.luogu.org/problemnew/show/P3952

考场上输出的是 "YES" "NO"

++ ,如果不是亲身经历,打死我我都不信

思路:

  递归模拟

  细节挺多

#include <bits/stdc++.h>

using namespace std;
const int oo = 999999999;


int T, L, Maxpow;
char use[200];
bool vis[200], flag;
int last;

inline int get() {
    char c[5];
    scanf("%s", c);
    int len = strlen(c);
    int ret = 0;
    if(len == 1) return c[0] == 'n' ? oo : c[0] - '0';
    else for(int i = 0; i < len; i ++) ret = ret * 10 + c[i] - '0';
    return ret;
}

void Go_on() {
    int jsF(0), jsE(0);
    while(jsE <= jsF) {
        L --;
        char H; cin >> H;
        if(H == 'E') {jsE ++; vis[use[last --]] = 0; if(jsE > jsF) return ;} 
        else {
            char Int; int x, y; 
            cin >> Int; x = get(); y = get();
            if(vis[Int]) {flag = 1; return ;} 
            else {vis[Int] = 1; use[++ last] = Int;}
            jsF ++;
        }
    }
}

void Work(int floor_pow, int floor) { // 到达当前层时的最大次数 
    while(L) {
        if(flag) return ;
        L --;
        char H; cin >> H;
        if(H == 'F') {
            char Int; cin >> Int; int x = get(), y = get();
            if(vis[Int]) {flag = 1; return ;} 
            else {vis[Int] = 1; use[++ last] = Int;}
            if(x != oo && y == oo) {
                Maxpow = max(Maxpow, floor_pow + 1);
                Work(floor_pow + 1, floor + 1);
            } else {
                if((x == oo && y != oo) || (x > y)) {Go_on();}
                else Work(floor_pow, floor + 1);
            }
        } else {
            vis[use[last --]] = 0;
            if(floor != 1) return ;    
        }
    }
    
}

inline void Read_over() {
    while(L) {L --; char H; cin >> H; if(H == 'F') {cin >> H; H = get(); H = get();}}
}

int main() {
    cin >> T;
    while(T --) {
        cin >> L;
        char tim[10];
        scanf("%s", tim);
        memset(vis, 0, sizeof vis); last = 0;
        int len = strlen(tim);
        if(L % 2) {cout << "ERR" << endl; Read_over(); continue ;}
        Maxpow = 0; last = 0; flag = 0;
        Work(0, 1);
        if(L) Read_over();
        if(last || flag) {cout << "ERR" << endl; continue ;}
        int Pow;
        if(tim[2] == '1') Pow = 0;
        else if(len == 6) Pow = tim[4] - '0';
        else if(len == 7) Pow = (tim[4] - '0') * 10 + tim[5] - '0';
        else if(len == 8) Pow = ((tim[4] - '0') * 10 + tim[5] - '0') * 10 + tim[6] - '0';
        if(Pow == Maxpow) cout << "Yes" << endl;
        else cout << "No" << endl;
    }   
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/shandongs1/p/8982329.html