【模拟】洛谷_3952 时间复杂度

5 篇文章 0 订阅

题意

给出几个语句,判断它们的时间复杂度是否符合,如果是的话输出‘Yes’,不然输出’No’,编译错误的话输出‘ERR’。

思路

比较长的模拟。
用一个check判断时间复杂度和是否有编译错误,最后比较输出。

代码

#include<stack>
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int T, N, ans1, ans2;
string o;
string code[101];

int sread(int &x, string s) {//查找串中数字
    int result = 0, l = s.size();
    while ((s[x] < '0' || s[x] > '9') && x < l) {
        if (s[x] == 'n') {
            x++;
            return 10000;//N
        }
        x++;
    }
    while (s[x] >= '0' && s[x] <= '9') {
        result = result * 10 + s[x] - 48;
        x++;
    }
    return result;
}

int geto() {//求出时间复杂度
    int x = 3;
    if (o[2] == 'n') return sread(x, o);
    else return 0;
}

int check() {
    int result = 0, now = 0, flag = -1, k, a, b, x;
    stack<int> z;
    bool ins[26] = {0}, con[26] = {0};//判断是否在栈里,以及是否对循环有贡献
    for (int i = 1; i <= N; i++) {
        if (code[i][0] == 'F') {
            k = code[i][2] - 97;
            if (ins[k]) return -1;//重名,编译错误
            z.push(k);
            ins[k] = 1;
            x = 4;
            a = sread(x, code[i]);
            b = sread(x, code[i]);
            if (b - a > 1000) {
                if (flag == -1) {//flag记录第一个没有贡献的变量
                    con[k] = 1;
                    now++;//记录当前循环复杂度
                    result = max(now, result);
                }
            }
            if (a > b && flag == -1) flag = k;//b>a,无贡献
        }
        else {
            if (!z.size()) return -1;//多E,编译错误
            k = z.top();
            z.pop();
            ins[k] = 0;
            if (flag == k) flag = -1;
            if (con[k]) {
                con[k] = 0;
                now--;
            }
        }
    }
    if (z.size()) return -1;
    return result;
}

int main() {
    scanf("%d", &T);
    for (; T; T--) {
        scanf("%d ", &N);
        getline(cin, o);
        ans1 = geto();
        for (int i = 1; i <= N; i++)
            getline(cin, code[i]);
        ans2 = check();
        if (ans2 == -1) puts("ERR");
        else {
            if (ans1 == ans2) puts("Yes");
            else puts("No");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值