【题解】P3952 时间复杂度

做个总结,顺便谈一谈我个人对着道题的做法,希望有所帮助语言不会过于晦涩难懂或过于繁杂

首先有两点要注意

 - **多组数据中首先一定要保证所有数据都清零了**
 
 - **如果中间有一套循环无法运行它下面嵌套再多循环也无济于事**
 
 - **循环结束则意味着变量被销毁**
 
 那么**可能**会有以下几个问题
 
 1. 不会特殊的读入方式
 
 其实并不影响你读取时间复杂度,在代码中可以逐字符读入,读取时间复杂度中逐字符没有检测到读入n而读到数字1那么便可以判为常数复杂度
 
 2. 不清楚时间复杂度的具体概念
 
 这个问题不是主观上的,还是简单解释一下,打个很简单的比方,一个循环为一个点A,那么它内部嵌套的循环是另一个点B,当B的复杂度为n时从B向A连一条权值为1的边,当B复杂度为1时连一条权值为0的边,当B无法运行时不连接边。然后时间复杂度就是从根节点出发的最长路径,当然实际做题的时候并不用建图
 
 3. 忽略了特殊情况
 
 必须要注意F i 2 1, F i n n 都是合法的情况
 
 那么我们用栈来存储循环,当变量重复,调用空栈,结束后栈内有数据时判error即可
 
 个人代码里大量使用逐字符读入如果不习惯最好阅读一下他人的代码,推荐题解第二篇
 

  ```cpp
//2018/12/9->NHDR233->AtHM->luoguP3952
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<stack>
#define LL long long
using namespace std;
inline int in() {
    static int n, ch;
    n = 0; ch = getchar();
    while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) n = n*10+ch-'0', ch = getchar();
    return n;
}
inline void smax(int& x, int y) {if (x < y) x = y;}
struct node{
    int I, base;
};
stack <node> sta;
char ENTER[10];
bool bol[128], end, err, lock;
int getO() {
    int n = 0; char ch; bool flag = false;
    ch = getchar();
    while (!isdigit(ch)) {
        if (ch == 'n') flag = true;
        ch = getchar();
    }
    while (isdigit(ch)) {
        n = n*10+ch-'0';
        ch = getchar();
    }
    if (!flag) return 0;
    else return n;
}
void getlines() {
    node temp;
    temp.base = 0;
    int ch = getchar(), fir = 0, sec = 0;
    while (!isalpha(ch)) ch = getchar();
    if (ch == 'E') {
        end = true;
        return;
    }else{
        getchar();
        ch = getchar();
        if (bol[ch]) {
            err = true;
        }
        bol[ch] = true;
        temp.I = ch;    
        getchar();
        ch = getchar();
        if (ch == 'n') {
            temp.base = 2;
            getchar();
        }
        while (isdigit(ch)) {
            fir = fir*10+ch-'0'; ch = getchar();
        }
        ch = getchar();
        if (ch == 'n') {
            if (temp.base == 0) temp.base = 1;
            else temp.base = 0;
        }
        while (isdigit(ch)) {
            sec = sec*10+ch-'0'; ch = getchar();
        }
        if (temp.base == 0 and fir > sec) temp.base = 2;
        sta.push(temp);
    }
}
int main() {
    int T = in();
    while (T--) {
        memset(bol, 0, sizeof(bol));
        err = false; lock = false;       
        int lines = in();
        int O = getO();
        int temO = 0, maxn = 0, cnt = 0;
        for (int i = 1; i <= lines; ++i) {
            end = false;
            getlines();
            if (end and sta.empty()) {
                err = true;
                continue;
            }else if (!end) {
                node temp = sta.top();
                if (temp.base == 1 and !lock) {
                    temO++;
                    smax(maxn, temO);
                }
                if (temp.base == 2) {
                    lock = true;
                    cnt++;
                }
            }else if (end) {
                node temp = sta.top();
                sta.pop();
                bol[temp.I] = false;
                if (temp.base == 2) --cnt;
                if (!lock and temp.base == 1) --temO;
                if (!cnt) lock = false;
            }
        }
        if (sta.size()) {
            err = true;
            while (!sta.empty()) sta.pop();
        }
        if (err) {
            puts("ERR");
            continue;
        }
        if (maxn == O) puts("Yes");
        else puts("No");
    }
}

```

猜你喜欢

转载自www.cnblogs.com/NHDR233/p/11246695.html
今日推荐