时间复杂度

模拟

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int t,n,o,st[150],S[150],SS[150];
char O[150],type;
short int fl[100];
struct DM{
	char ForE;
	char z;
	int a,b;
	char st[100],ed[100];
	#define z(x) H[x].z
	#define a(x) H[x].a
	#define b(x) H[x].b
	#define E(x) H[x].ForE
}H[150];
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();	
	}
	return x*f;
}
inline void begin(int x){
	int l1;
	if(H[x].st[0]=='n') a(x)=10000;
	else{
		int yy=0;
		l1=strlen(H[x].st)-1;
		for(register int i=0;i<=l1;i++){
			yy=yy*10+H[x].st[i]-'0';
		} 
		a(x)=yy;
	}
	if(H[x].ed[0]=='n') b(x)=10000;
	else{
		int yy=0;
		l1=strlen(H[x].ed)-1;
		for(register int i=0;i<=l1;i++){
			yy=yy*10+H[x].ed[i]-'0';
		} 
		b(x)=yy;
	}
}
inline int judgeO(){
	if(O[2]=='1') return 1;
	else{
		int x=0;
		for(register int i=4;;i++){
			if(O[i]==')') break;
			x=x*10+O[i]-'0';
		}
		return x+1;
	}
}
inline int ERR(){
    int ING=0;
	for(register int i=1;i<=n;i++){
		if(E(i)=='F') ING++;
		if(E(i)=='E') ING--;
		if(ING<0) return 1; 
	}
	if(ING) return 1;    
	return 0;
}
inline int judge1ERR(){
    for(int i='a'-'0';i<='z'-'0';i++){
    	if(fl[i]>1) return 1;
    }
    return 0;
}
inline int work(){
	int ING=0,maxo=0,ans=0,NcN=0;
	for(register int i=1;i<=n;i++){
	   	if(E(i)=='F'){
   		   fl[z(i)-'0']++;
           if(judge1ERR()) return 2;
	   	   ING++;
	   	   S[ING]=z(i)-'0';
	   	   st[ING]=1;
           if(a(i)!=10000&&b(i)!=10000){
           	 if(a(i)>b(i)){
           	 	int ING2=0;
           	 	for(register int j=i+1;j<=n;j++){
           	 		if(ING2==0&&E(j)=='E'){
           	 			i=j;
						fl[S[ING]]--;
						ING--;
						break;
           	 		}
           	 		if(E(j)=='F'){
           	 		   fl[z(j)-'0']++;
           	 		   if(judge1ERR()) return 2;
					   ING2++;
           	 		   SS[ING2]=z(j)-'0';
           	 		}
           	 		if(E(j)=='E'){	
				        fl[SS[ING2]]--;
           	 		   ING2--;	
           	 		} 
           	 	}
           	 }
			ans=1;
			continue;	 
           }
		   if(a(i)==10000&&b(i)!=10000){
		   	   int ING2=0;
           	 	for(register int j=i+1;j<=n;j++){
           	 		if(E(j)=='E'&&ING2==0){
           	 			i=j;
						fl[S[ING]]--;
						ING--;
						break;	
           	 		}
           	 		if(E(j)=='F'){
           	 		   fl[z(j)-'0']++;
           	 		   if(judge1ERR()) return 2;
					   ING2++;
           	 		   SS[ING2]=z(j)-'0';
           	 		}
           	 		if(E(j)=='E'){
						fl[SS[ING2]]--;	
           	 		    ING2--;	
           	 		} 
           	 	}
           	 ans=1;
			 continue;	
		   }
		   if(a(i)==10000&&b(i)==10000) continue;
		   NcN++;
		   st[ING]=10000;
		   maxo=max(NcN,maxo);  	
	   	}
	   	else{
		   fl[S[ING]]--;
	   	   if(st[ING]==10000){
	   	   	NcN--;ING--;
	   	   }
		   else ING--; 	
	   	}
	}
	if(o!=1){
		if(maxo==o-1) return 0;	
		else return 1;
	}
	if(o==1){
		if(maxo==0&&ans==1) return 0;
		else return 1;
	}
	return 1;
}
int main(){
	t=read();
	while(t--){
	  n=read(); scanf("%s",O);
	  o=judgeO();
	  memset(fl,0,sizeof(fl));
	  for(register int i=1;i<=n;i++){
	  	scanf("%s",&E(i));
	  	if(E(i)=='F'){
	  		scanf("%s",&z(i));
	  		scanf("%s%s",&H[i].st,&H[i].ed);
		    begin(i);
	    }
	  }
	  if(ERR()==1) printf("ERR\n");
      else{
      	if(work()==2) printf("ERR\n");
      	if(work()==1) printf("No\n");
        if(work()==0) printf("Yes\n");
      } 
	}
	return 0;
}
/*
18 O(n^8)
F a 76 n
F b 34 n
F c 16 n
F d 2 n
F e 5 n
F f 87 n
F g 8 n
F h 3 n
F i n 17
E
E
E
E
E
E
E
E
E*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值