【NOIP2017 DAY1T2】 时间复杂度

【题目链接】

           点击打开链接

【算法】

        其实这就是一道模拟题啦!

        在判error和计算时间复杂度时,我们需要用栈这种数据结构

【代码】

          这题的代码还是有些难写的,写的时候一定要有条理!

           

#include<bits/stdc++.h>
using namespace std;
#define MAXL 100
const int INF = 2e9;

int T,n;
char opt[MAXL+10],value[MAXL+10];
int l[MAXL+10],r[MAXL+10];

template <typename T> inline void read(T &x) {
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar())  x = (x << 3) + (x << 1) + c - '0';
    x *= f;	
}
template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');	
}
template <typename T> inline void writeln(T x) {
    write(x);
    puts("");	
}

inline int get() {
    int ret = 0;
    bool b = true;
    getchar();
    char c = getchar();
    while (c != ')') {
        if (c == '^') b = false;
        if (isdigit(c)) ret = (ret << 3) + (ret << 1) + c - '0';
        c = getchar();
    } 
    getchar();
    if (b) return 0;
		else return ret;	
}

inline void getstr(int pos) {
		char c = getchar();
		while (c != 'F' && c != 'E') c = getchar();
		opt[pos] = c;
}

inline void getinfo(int pos) {
    int i,len;
    char tx[10],ty[10];
    getstr(pos);
    if (opt[pos] == 'E') return;
    scanf(" %c %s %s",&value[pos],tx+1,ty+1);
    if (tx[1] != 'n') {
        len = strlen(tx+1);
        for (i = 1; i <= len; i++) l[pos] = (l[pos] << 3) + (l[pos] << 1) + tx[i] - '0';
    } else
    		l[pos] = INF;
    if (ty[1] != 'n') {
    		len = strlen(ty+1);
    		for (i = 1; i <= len; i++) r[pos] = (r[pos] << 3) + (r[pos] << 1) + ty[i] - '0';
  	} else
  			r[pos] = INF;
  	getchar();
}

inline bool error() {
    int i,top=0;
    static int stk[MAXL+10];
    static bool used[26];
    memset(used,0,sizeof(used));
    for (i = 1; i <= n; i++) {
        if (opt[i] == 'F') {
            if (used[value[i]-'a']) return true;
            used[value[i]-'a'] = true;
            stk[++top] = i;
        } else {
            if (!top) return true;
            used[value[stk[top]]-'a'] = false;
          	--top;
        }
    }	
    return top != 0;
}

inline void solve() {
    int i,top=0,c,tmp,ans=0;
    static int stk[MAXL+10];
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    read(n);
    c = get();	
    for (i = 1; i <= n; i++) getinfo(i);
    if (error()) {
        puts("ERR");
        return;
    } 
    for (i = 1; i <= n; i++) {
        if (opt[i] == 'F') {
        		tmp = stk[top];
        		if (l[i] > r[i]) tmp = -1;
        		else if (r[i] - l[i] > 1000 && stk[top] != -1) ++tmp;
        		stk[++top] = tmp;
        		ans = max(ans,tmp);
				} else 
						--top;
    }
    if (ans == c) puts("Yes");
    else puts("No");
}

int main() {
    
    read(T);
    while (T--) solve();
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值