简单模拟题目。
使用 $set$ 去重, $stack$ 统计,再加上一些细节就好了。
#include<bits/stdc++.h>
using namespace std;
const int N=10001;
struct Node {
char name;
int type,level;
};
int str2int(string s) {
int ret=0,i=0;
while(s[i]>='0'&&s[i]<='9') {
ret=ret*10+s[i]-'0';
i++;
}
return ret;
}
void splitstr(string s, char &ch, int &st, int &ed) {
stringstream ss;
string n1,n2;
ss<<s.substr(2);
ss>>ch;
ss>>n1;
if(n1[0]=='n') st=N;
else st=str2int(n1);
ss>>n2;
if(n2[0]=='n') ed=N;
else ed=str2int(n2);
}
int main() {
int ntests;
cin>>ntests;
while(ntests--) {
int len,exp,level=0,cnt=0,ans=0;
string expr,line;
stack<Node> stk;
set<char> vname;
int ERR=0;
cin>>len>>expr; cin.get();
if(expr[2]=='1') exp=0;
else exp=str2int(expr.substr(4));
for(int i=0;i<len;i++) {
getline(cin,line);
if(line[0]=='F') {
char vn; int st,ed,type;
splitstr(line,vn,st,ed);
if(vname.count(vn)>0) ERR=2;
vname.insert(vn);
if(st>ed)
type=0,level=0;
else if(st<ed&&ed==N)
type=1,level++;
else type=2;
stk.push((Node){vn,type,level});
} else if(line[0]=='E') {
if(stk.empty()) {ERR=1; continue;}
Node cur=stk.top(); stk.pop();
vname.erase(cur.name);
if(cur.type==0) cnt=0;
else if(cur.type==1) {
level=cur.level-1;
cnt=max(cnt,cur.level);
}
if(stk.empty()) ans=max(ans,cnt);
}
}
if(!stk.empty()) ERR=1;
if(ERR>0) cout<<"ERR"<<endl;
else if(ans==exp) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}