[NOIP2017]时间复杂度

栈还原进入循环前的状态即可。

#include<bits/stdc++.h>
using namespace std;

int sta[105],s2[105],top;
char s[15];
bool b[30],flag;
void read_res(int res){
	int x,y;
	while(res--){
		scanf("%s",s+1);
		if(s[1]=='F'){
			scanf("%s%s%s",s+1,s+1,s+1);
		}
	}
} 
int main(){
	int t,tim,len;
	scanf("%d",&t);
	while(t--){
		scanf("%d%s",&len,s+1);
		tim=0;
		if(s[3]=='n')
			for(int i=5,l=strlen(s+1);i<l;++i){
				tim=tim*10+s[i]-'0';
			}
			
		flag=true;top=0;
		memset(b,0,sizeof(b));
		int ans=0,tot=0;
		while(len--){
			scanf("%s",s+1);
			if(s[1]=='F'){
				int x=0,y=0;
				scanf("%s",s+1);
				int i=s[1]-'a';
				scanf("%s",s+1);
				if(s[1]=='n') x=101;
				else for(int j=1,l=strlen(s+1);j<=l;++j)
					x=x*10+s[j]-'0';
				scanf("%s",s+1);
				if(s[1]=='n') y=101;
				else for(int j=1,l=strlen(s+1);j<=l;++j)
					y=y*10+s[j]-'0';
				
				if(b[i]){
					flag=false;
					read_res(len);
					break;
				}
				b[i]=true;
				sta[++top]=i;
				if(x>y){
					s2[top]=-1-tot;
					tot=-1;
				}
				else if(x<=100&&y==101){
					if(tot!=-1){
						++tot;
						s2[top]=1;
					}
					else s2[top]=0;
				}
				else s2[top]=0;
				ans=max(ans,tot);
			}
			else{
				if(!top){
					flag=false;
					read_res(len);
					break;
				}
				tot-=s2[top];
				b[sta[top--]]=false;
			}
		}
		if(!flag||top>0) printf("ERR\n");
		else if(ans==tim) printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AireenYe/p/15800108.html