时间复杂度

题目

在这里插入图片描述

思路(meiyou

考场30分暴力

正解:
  • 思路为先全部读入,再一起处理
  • Cal_comp函数:计算时间复杂度,返回值为x的幂数,为-1则程序错误
  • Get_comp函数:读入所给的时间复杂度
  • Get_num函数:读入x,y的值,为n则为无穷大

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
#define N 105
int t,l,ci_fang;
string read_comp,code[N];

int Get_num(int &x,string s){ // 因为要连续读入x,y两个值,而第二个y的值得起始位置不好判断,所以&x 
    int len = s.size(),ret = 0;
    while(s[x]<'0' || s[x]>'9' && x<len){
        if(s[x] == 'n'){
            x++;
            return 100000;
        }
        x++;
    }
    while(s[x]>='0' && s[x]<='9'){
        ret *= 10;
        ret += (s[x]-'0');
        x++;
    }
    return ret;
}
int Get_comp(){
    int x = 3;
    if(read_comp[2] == 'n') return Get_num(x,read_comp);
    return 0;
}
bool used[30],pos[30];
int Cal_comp(){
    memset(used,0,sizeof(used));
    memset(pos,0,sizeof(pos));
    int ret = 0,now = 0,x;
    int flag = -1;
    stack <int> s;
    for(int i=1; i<=l; i++){
        if(code[i][0] == 'F'){
            int bl = code[i][2] - 'a';; // save bian liang
            if(used[bl])	return -1;
            s.push(bl);
            used[bl] = true;
            x = 4;
            int a = Get_num(x,code[i]);
            int b = Get_num(x,code[i]);
            if(b-a > 1000 && flag == -1){ // n
                now++;
                ret = max(ret,now);
                pos[bl] = true;
            }else if(a>b && flag == -1){
                flag = bl;
            }
        }else if(code[i][0] == 'E'){
            if(s.empty()) return -1;
            int bl = s.top();
            s.pop();used[bl] = false;
            if(flag == bl) flag = -1;
            if(pos[bl]){
                pos[bl] = false;
                now--;
            }
        }
    }
    if(s.size()) return -1;
    return ret;
}
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d ",&l);
        getline(cin,read_comp);
        int target_comp = Get_comp(); // read_comp : O(n^read_comp)
        for(int i=1; i<=l; i++)
            getline(cin,code[i]);
        int compare_comp = Cal_comp();
        if(compare_comp == -1) puts("ERR");
        else{
            if(compare_comp == target_comp)
                puts("Yes");
            else
                puts("No");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值