lifengsen • 2个月前
using namespace std;
typedef pair PCI; const int N=110; int tt;
PCI stk[N];
int get_number(string str) {
int res=0;
for(int c=0; c<str.size(); c++)
res=res*10+str[c]-'0';
return res;
}
int get_time(string str) {
if(str=="O(1)")
return 0;
int t=str.find('^');
string num=str.substr(t+1);
num.erase(num.end()-1);
return get_number(num);
}
bool has(char c) {
for(int i=1; i<=tt; i++)
if (stk[i].first==c)
return true;
return false;
}
int get_cmp(string x, string y) {
if(x=="n")
{
if(y=="n")
return 0;
return -1;
}
if(y=="n")
return 1;
int a=get_number(x),b=get_number(y);
if(a<=b)
return 0;
return -1;
}
int main() {
int T;
scanf("%d", &T);
while(T--)
{
int n;
string str;
cin>>n>>str;
int tm=get_time(str);
int max_cmp=0;
bool error=false;
tt=0;
string line;
getline(cin,line);
for(int i=0; i<n; i++)
{
getline(cin,line);
if(!error)
if(line == "E")
if(tt)
tt--;
else
error=true;
else
{
stringstream sin(line);
string F,i,x,y;
sin>>F>>i>>x>>y;
if(has(i[0]))
error=true;
else
{
int cmp=get_cmp(x, y);
if(!tt)
stk[++tt]={i[0],cmp};
else
{
int computation=-1;
if(stk[tt].second>=0 && cmp>=0)
computation=stk[tt].second+cmp;
stk[++tt]={i[0],computation};
}
max_cmp=max(max_cmp, stk[tt].second);
}
}
}
if(tt)
error=true;
if(error)
cout<<"ERR\n";
else if(tm==max_cmp)
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}
评论: