时间复杂度

模拟

用栈维护

注意:

1、就算有语法错误也要将读入都读完。
2、注意防止RE
3、每一层的复杂度要取max

code:

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<stack>
#define int long long 
#define INF 10000000000007
#define maxn 100006 
#define rep(i,a,b) for (int i=(a);i<=(b);++i)
#define dep(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;	 	
char tle[maxn],fx[maxn],fy[maxn];
bool vis[maxn];
int val[maxn],n;
stack<int>ss;
stack<char>s_;
signed main()
{
	 int t;
	 cin>>t;
	 while (t--)
	 {
	 	cin>>n;
	 	cin>>(tle+1);
	 	memset(vis,0,sizeof(vis));
	 	int flag=0,l=0;
	 	memset(val,0,sizeof(val));
	 	rep(i,1,n)
	 	{
	 		char pan,nam;
	 		cin>>pan;
	 		int cnt=0;
	 		if (pan=='E')
	 		{
	 			if (ss.empty()) {flag=1;}
	 			else 
	 			{
					int tmp=ss.top();
					ss.pop();
					if (tmp<0) cnt=0;
					else if (tmp>100000) cnt=val[l+1]+1;
					else cnt=val[l+1];
					char wow=s_.top();
					s_.pop();
					vis[(int)wow]=0;
					val[l+1]=0;
					val[l]=max(val[l],cnt);		
					l--; 
				}
			}
			else 
			{
				l++;
				cin>>nam;
				if (vis[(int)nam]) {flag=1;}
				vis[(int)nam]=1;
				s_.push(nam);
				cin>>(fx+1)>>(fy+1);
				int rfx=0,rfy=0,mul=1;
				dep(j,(int)strlen(fx+1),1) 
				{
					if (fx[j]=='n') {rfx=INF;break;}
					rfx+=(fx[j]-'0')*mul;
					mul*=10;
				}
				mul=1;
				dep(j,(int)strlen(fy+1),1) 
				{
					if (fy[j]=='n') {rfy=INF;break;}
					rfy+=(fy[j]-'0')*mul;
					mul*=10;
				}
				ss.push(rfy-rfx);
			}		
		}
		if (flag||l!=0) {cout<<"ERR"<<endl;continue;}
		if (val[1]==0&&tle[3]=='1') cout<<"Yes"<<endl;
		else
		{
			int mul=1,fina=0;
			dep(i,(int)strlen(tle+1)-1,5) {fina+=mul*(tle[i]-'0'),mul*=10;}
			if (fina==val[1]) cout<<"Yes"<<endl;
			else cout<<"No"<<endl;
		}
	 }
} 
原文地址:https://www.cnblogs.com/bullshit/p/9933592.html