大模拟,但是主要看思路,思路简单了会很好写。
考虑到有无效循环这种东西,我们开个\(int:\text{For}=0,\text{Vor}=0,\text{IN}=1\)来记录,遇到\(F\)就\(++\),遇到\(E\)就\(--\)
开个\(\text{Vor}\)记个当前有效循环次数。如果遇到无法进入的循环,关掉\(\text{IN}\),记录一下\(\text{Pos}=\text{For}\)(因为最多只会有一个无法进入的循环起作用)
对于循环记个变量名(用数组)。
#define _GLIBCXX_USE_CXX11_ABI 0
#include<bits/stdc++.h>
using namespace std;
const int N=200;
char x;
int Var[30],V[N],Jud[N];
inline void Clear(){do x=getchar();while(x!='\n'&&x!='\r');}
int main(){
#ifndef ONLINE_JUDGE
freopen("A.in","r",stdin);
#endif
int T;scanf("%d",&T);
while(T--)
{
int Typ=0,L=0,ERR=0,For=0;string O="";
int Vor=0,IN=1,Pos=0,Ans=0;memset(Var,0,sizeof(Var));
memset(Jud,0,sizeof(Jud)); memset(V,0,sizeof(V));
x=getchar();while(x=='\n'||x=='\r') x=getchar();
while(x>='0'&&x<='9') L=(L<<3)+(L<<1)+x-'0',x=getchar();
x=getchar();while(x!='\n'&&x!='\r') O+=x,x=getchar();
for(int i=0;i<(int)O.size();i++)
if('0'<=O[i]&&O[i]<='9') Typ=(Typ<<3)+(Typ<<1)+O[i]-'0';
if(O=="O(1)") Typ=0;
while(L--)
{
if(ERR) {Clear(); continue;}
x=getchar();while(x=='\n'||x=='\r') x=getchar();
if(x=='F')
{
x=getchar(),x=getchar();For++;
if(Var[x-'a']) {ERR=1,Clear(); continue;}
Var[x-'a']=1,V[For]=x-'a';string A="",B="";
x=getchar(),x=getchar();while(x!=' ') A+=x,x=getchar();
x=getchar();while(x!='\n'&&x!='\r') B+=x,x=getchar();
if(A[0]!='n'&&B[0]=='n'&&IN) Vor++,Jud[For]=1;
else if((A.size()>B.size()||((A.size()==B.size()&&A>B))||(A[0]=='n'&&B[0]!='n'))&&IN) IN=0,Pos=For;
}
else
{
Ans=max(Ans,Vor);Var[V[For]]=0;
if(Jud[For]==1) Vor--,Jud[For]=0;For--;
if(Pos&&For<Pos) IN=1,Pos=0;
}
if(For<0) {ERR=1,Clear();continue;}
}
if(For) puts("ERR");
if(!For&&Ans==Typ) puts("Yes");
if(!For&&Ans!=Typ) puts("No");
}
}