NOIP2017 时间复杂度 大模拟

本文作者MiserWeyte

再写一道大模拟题。

由于是限时写的,相当于考场代码,乱的一批。

题目链接:P3952 时间复杂度


先记几个教训:

  • 字符串形式的数字比较大小老老实实写函数,字典序都搞错几次了
  • 栈空的时候不但pop()会RE,top()访问栈顶也会RE
  • 数字以字符形式读入要考虑位数超过一位

思路:

1.我用的是在线做法。

2.使用结构体存储一个循环体,包括变量名、起始与结束、是否与n有关、是否能够进入(这一点麻烦了,在栈里推入是否与n有关以及是否能够进入就行了)

3.使用一个变量存储当前进入的层数(只记含n的),当压入一个“与n有关”时++,当弹出一个“与n有关”时--,取最大值

4.使用一个bool存储当前循环是否能进入。当弹出一个不能进入的结构体时取消“不能进入”的状态(如果已在“不能进入”的状态,结构体不会被记作“不能进入”)

源码:

扫描二维码关注公众号,回复: 7851820 查看本文章
//MiserWeyte is now "mzWyt"
#include <bits/stdc++.h>
using namespace std;
int n, tgt, rel, curr = 0, maxx;
bool used[30];
string tar;
bool err, notin;
struct sts{
    char tpe, nam;
    string sta, end; 
    bool use, nin;
};
bool le(string a, string b){
    if(a=="n" || b=="n" ) return false;
    int numa = atoi(a.c_str());
    int numb = atoi(b.c_str());
    return numa > numb;
}
stack <sts> s;
void init(){
    err = false;
    memset(used, 0, sizeof(used));
    curr = 0;
    maxx = 0;
    notin = false;
    while(s.size()) s.pop();
}
int main(){
    int t;
    cin >> t;
    while(t--){
        init();
        cin >> n >> tar;
        tgt = 0;
        if(tar[2] != '1'){
            for(int i=0; i<tar.length(); i++){
                if(tar[i] >= '0' && tar[i] <= '9'){
                    tgt *= 10;
                    tgt += tar[i] - '0';
                }
            }
        }
        for(int i=0; i<n; i++){
            sts temp;
            cin >> temp.tpe;
            if(temp.tpe == 'F'){
                cin >> temp.nam ;
                cin>> temp.sta; 
                cin>> temp.end;
                temp.use = false;
                temp.nin = false;
                if(used[temp.nam - 'a']){
                    err = true;
//                  break;
                }
                used[temp.nam - 'a'] = true;
                if(notin){
                    s.push(temp);
                }
                else if(temp.sta == "n" && temp.end == "n") s.push(temp);
                else if(temp.sta != "n" && temp.end == "n"){
                    temp.use = true;
                    curr ++;
                    maxx = max(maxx, curr);
                    s.push(temp);
                }
                else if((temp.sta == "n" && temp.end != "n") || le(temp.sta, temp.end)){
                    notin = true;
                    temp.nin = true;
                    s.push(temp);
                }
                else s.push(temp);
            }
            else{
                if(s.empty()){
                    err = true;
                    continue;
                } 
                if(s.size() && s.top().use && !s.top().nin) curr --;
//              cout << s.top().use;
                if(s.size() && s.top().nin) notin = false;
                used[s.top().nam - 'a'] = false;
                if(s.size()) s.pop();
                
            }
//          cout << "curr" << curr << endl;
//          if(notin) cout << "NOTIN" << endl;
        }
        if(s.size()) err = true;
        if(err) cout << "ERR\n";
        else{
            if(maxx == tgt) cout << "Yes\n";
            else cout << "No\n";
//          cout << curr << endl;
        }
    }
} 

猜你喜欢

转载自www.cnblogs.com/miserweyte/p/11856077.html