【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; }