正题
题目链接:https://www.luogu.org/problemnew/show/P3952
题目大意
比较复杂就直接给截图了
解题思路
开一个栈来维护序列,因为它保证合法的程序中一个变量不会相互嵌套所以就拿变量的字母当做每个循环的下标即可。然后对于输入的x,yx,yx,y有五种情况
- x=N,y=N(x≤y)x=\mathbb{N},y=\mathbb{N}(x\leq y)x=N,y=N(x≤y)压入栈中,不做特殊处理
- x=n,y=nx=n,y=nx=n,y=n同上
- x=N,y=N(x>y)x=\mathbb{N},y=\mathbb{N}(x>y)x=N,y=N(x>y)使内部嵌套的循环不做处理
- x=n,y=Nx=n,y=\mathbb{N}x=n,y=N同上
- x=N,y=nx=\mathbb{N},y=nx=N,y=n目前时间复杂度加一
然后特判一下ERRERRERR就好了
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
stack<int> q;
int T,n,w,none,b[30],times,ans,use[30];
bool fuck;
char c;
int read()
{int num=0;while((c=getchar())!=' '&&c!='\n'){if(c=='n') {c=getchar();return -1;}else num=num*10+c-'0';}return num;
}
int main()
{scanf("%d",&T);while(T--){while(!q.empty()) q.pop();memset(b,0,sizeof(b));memset(use,0,sizeof(use));fuck=none=times=ans=0;scanf("%d ",&n);char c;scanf("O(%c",&c);if(c!='n') w=0;else scanf("%c%d",&c,&w);scanf(")");for(int i=1;i<=n;i++){scanf("\n%c",&c);if(c=='E'){if(fuck) continue;if(q.empty()) {fuck=1;continue;}if(q.top()==none) none=0;if(!none){if(b[q.top()]) times--;b[q.top()]=0;use[q.top()]=0;}q.pop();}if(c=='F'){scanf(" %c ",&c);c-='a'-1;int x=read(),y=read();if(fuck) continue;if(use[c]) {fuck=1;continue;}q.push(c);if(none) continue;use[c]=1;if(x==-1&&y!=-1) none=c;if(x!=-1&&y!=-1&&y<x) none=c;if(x!=-1&&y==-1) times++,b[c]=1;ans=max(ans,times);}}if(!q.empty()||fuck) printf("ERR\n");else if(w!=ans) printf("No\n");else printf("Yes\n");}
}