题目
题解
–是一道恶心的模拟题
首先可以用一个栈存正在运行中的循环
用isv数组判断变量是否重名
要注意不能进入的循环虽然不会影响答案,但是要经过标记后也存入站内(为了判错)
还要注意如果一个O(n)的循环如果在一个不能进入的循环内,答案是不会增加的
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=105;
int t;
int l;
string o,a;
struct hehe{
int a;
int b;
int c;
}stack[MAXN];
bool isu[MAXN],flag;
int top,ans,maxx,sum;
int read(int &i){
if(a[i]=='n'){
i+=2;
return -1;
}
int now=0;
while(a[i]>='0'&&a[i]<='9'){
now*=10;
now+=a[i]-'0';
i++;
}
i++;
return now;
}
void add(){
if(top>l/2||isu[a[2]-'a']){
flag=1;
return ;
}
stack[++top].a=a[2]-'a';
stack[top].b=0;
stack[top].c=0;
isu[stack[top].a]=1;
int b=4;
int x=read(b),y=read(b);
if((x==-1&&y!=-1)||(x!=-1&&y!=-1&&x>y)){
sum++;
stack[top].b=1;
}
if(x!=-1&&y==-1&&!sum){
ans++;
stack[top].c=1;
maxx=max(maxx,ans);
}
}
void out(){
if(!top){
flag=1;
return ;
}
if(!sum&&stack[top].c)
ans--;
if(stack[top].b)
sum--;
isu[stack[top--].a]=0;
}
int read2(){
int i=4;
int now=0;
while(o[i]>='0'&&o[i]<='9'){
now*=10;
now+=o[i]-'0';
i++;
}
return now;
}
bool ok(){
if(o.length()==4&&!maxx)
return 1;
if(o.length()!=4&&read2()==maxx)
return 1;
return 0;
}
int main(){
// freopen("complexity.in","r",stdin);
// freopen("complexity.out","w",stdout);
cin>>t;
while(t--){
cin>>l;
getchar();
getline(cin,o);
top=0;
flag=0;
ans=0;
maxx=0;
sum=0;
memset(isu,0,sizeof(isu));
for(int i=1;i<=l;i++){
getline(cin,a);
if(flag)
continue;
if(a[0]=='F')
add();
else
out();
}
if(top)
flag=1;
if(flag){
printf("ERR\n");
continue;
}
if(ok())
printf("Yes\n");
else
printf("No\n");
}
return 0;
}