zoukankan      html  css  js  c++  java
  • P3952 [NOIP2017 提高组] 时间复杂度

    这题开始想用循环做,WA了三发,想得太简单了,没有想到有这种情况的嵌套:

    F ...
    	F ...
    	E
    	F ...
    	E
    E
    

    所以改用递归,dfs()返回下一层的时间复杂度(下一层所有并列的F中复杂度的最大情况

    注意:有一个特殊情况就是在出现F i x y当x比y要大的时候,这个循环(不管他里面有没有嵌套)的复杂度一定是1,所以计算完某一个F循环的所有子循环的复杂度以后,需要特判一下是不是要加到F的复杂度上去

    #include<iostream>
    #include<vector>
    #include<string>
    #include<map>
    
    using namespace std;
    
    int idx = 0;
    vector<string> v;
    map<string, int> table;
    
    #define ERR -2
    
    void get(string &val, string &l, string &r, string &x, int idx){
        while(idx < x.size() && x[idx] == ' ') idx ++;
        while(idx < x.size() && x[idx] != ' ') val += x[idx ++];
        while(idx < x.size() && x[idx] == ' ') idx ++;
        while(idx < x.size() && x[idx] != ' ') l += x[idx ++];
        while(idx < x.size() && x[idx] == ' ') idx ++;
        while(idx < x.size() && x[idx] != ' ') r += x[idx ++];
    }
    
    int dfs(int u){
        vector<int> comp; // 存一个本层所有for的复杂度序列
        string cur_val; 
        int E = 0;
        int F = 0;
        while(idx < v.size()){
            string t = v[idx];
            if(t[0] == 'F'){
                if(!E && F){
                    int ans = dfs(u + 1);
                    if(ans == ERR) return ans;
                    if(~comp.back()) comp.back() += max(ans, 0);
                    continue;
                }
                E = 0;
                F = 1;
                string val, l, r;
                get(val, l, r, t, 1);
                if(table[val]) return ERR;
                table[val] = 1;
                cur_val = val;
                if(l == r) comp.push_back(0);
                else if(l == "n") comp.push_back(-1);
                else if(r == "n") comp.push_back(1);
                else if(stoi(l) <= stoi(r)) comp.push_back(0);
                else comp.push_back(-1);
            }else{
                if(!F){
                    if(u) break;
                    else return ERR;
                }
                E = 1;
                F = 0;
                table[cur_val] = 0;
            }
            idx ++;
        }
        
        if(!E) return ERR;
        int maxv = 0;
        for(int i = 0; i < comp.size(); i ++) maxv = max(maxv, comp[i]);
        
        return maxv;
    }
    
    int main(){
        int t;
        cin >> t;
        
        while(t --){
            int n;
            string o;
            cin >> n >> o;
            
            getchar();
            
            v.clear();
            for(int i = 0; i < n; i ++){
                string t;
                getline(cin, t);
                v.push_back(t);
            }
            
            idx = 0;
            table.clear();
            int res = dfs(0);
            
            if(res == ERR){
                puts("ERR");
                continue;
            }
    
            int num = 0;
            if(o[2] == 'n')
                for(int i = 4; i < o.size() && o[i] != ')'; i ++)
                    num = num * 10 + o[i] - '0';
                    
            if(num == res) puts("Yes");
            else puts("No");
        }
        
        return 0;
    }
    
  • 相关阅读:
    C he 指针
    typedef 与 define
    (转)ubuntu中安装man手册查看函数原型
    .9 赫夫曼编码
    .8 AVL树
    PowerDesigner使用技巧
    C#基础
    NET框架设计
    Sql Server 执行计划及Sql查询优化
    SQL SERVER函数浅析
  • 原文地址:https://www.cnblogs.com/tomori/p/15186491.html
Copyright © 2011-2022 走看看