[noip2017]时间复杂度

[传送门](https://www.luogu.org/problemnew/show/P3952)

模拟题,用栈来存循环

开一个桶存变量有没有出现过,退栈时清除,然后愉快的算答案就行了

note: 数据里有$for:~i=n ~ to ~n $的东西,要特判掉。。。

还有就是如果循环没进去,后面的都不作数,这个我一开始没注意。。

#include<bits/stdc++.h>
using namespace std;
#define maxn 300
char s[maxn];
int T,n,x,sta[maxn],top,tot,hs[maxn],hs_sta[maxn];
int main(){
    scanf("%d",&T);
    while(T--){
		memset(hs,0,sizeof hs);
		scanf("%d",&n);x=0;top=0;
		char ch=getchar();int flag=0;
		while(ch!='
'){
			if(ch=='n') flag=1;
			ch=getchar();
			if(isdigit(ch)) x=x*10+ch-'0';
		}
		if(!flag) x=0;
		int ERR=0,ans=0;
		for(int l=1;l<=n;l++){
			memset(s,0,sizeof s);
			int err=0;tot=0;s[0]='a';
			while(s[tot]!='
')s[++tot]=getchar();
			if(ERR) continue;
			int a1=0,a2=0,isn=0;
			for(int i=1;i<=tot;i++){
				if(isdigit(s[i])&&!err){
					while(isdigit(s[i])) a1=a1*10+s[i]-'0',i++;
					err=1;
				}
				if(isdigit(s[i])&&err==1){
					while(isdigit(s[i])) a2=a2*10+s[i]-'0',i++;
				}
				if(s[i]=='n'){
					if(err==1) isn=2;
					else isn=1;
				}
			}
			if(s[5]=='n'&&s[7]=='n') {sta[++top]=0,hs_sta[top]=s[3];continue;}
			if(a2<a1&&isn==0) {sta[++top]=-1,hs_sta[top]=s[3];continue;}
			if(isn==1){sta[++top]=-1;hs_sta[top]=s[3];continue;}
			if(isn==0&&s[1]=='F') sta[++top]=0,hs_sta[top]=s[3];
			if(isn==2) sta[++top]=1,hs_sta[top]=s[3];
			int res=0;for(int i=1;i<=top;i++) {if(sta[i]==-1) break;res+=sta[i];}ans=max(ans,res);
			if(s[1]=='E') {hs[hs_sta[top]]=0;top--;continue;}
			if(hs[(int)s[3]]) {puts("ERR");ERR=1;}
			hs[(int)s[3]]=1;
		}
		if(top!=0&&!ERR) {puts("ERR");ERR=1;continue;}
		if(ERR) continue;
		if(ans==x) puts("Yes");
		else puts("No");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/hbyer/p/9637347.html