【NOIP2017提高组 day1】时间复杂度

题目

在这里插入图片描述
在这里插入图片描述


题解

–是一道恶心的模拟题
首先可以用一个栈存正在运行中的循环
用isv数组判断变量是否重名
要注意不能进入的循环虽然不会影响答案,但是要经过标记后也存入站内(为了判错)
还要注意如果一个O(n)的循环如果在一个不能进入的循环内,答案是不会增加的


代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=105;

int t;
int l;
string o,a;
struct hehe{
	int a;
	int b;
	int c;
}stack[MAXN];
bool isu[MAXN],flag;
int top,ans,maxx,sum;

int read(int &i){
	if(a[i]=='n'){
		i+=2;
		return -1;
	}
	int now=0;
	while(a[i]>='0'&&a[i]<='9'){
		now*=10;
		now+=a[i]-'0';
		i++;
	}
	i++;
	return now;
}

void add(){
	if(top>l/2||isu[a[2]-'a']){
		flag=1;
		return ;
	}
	stack[++top].a=a[2]-'a';
	stack[top].b=0;
	stack[top].c=0;
	isu[stack[top].a]=1;
	int b=4;
	int x=read(b),y=read(b);
	if((x==-1&&y!=-1)||(x!=-1&&y!=-1&&x>y)){
		sum++;
		stack[top].b=1;
	}
	if(x!=-1&&y==-1&&!sum){
		ans++;
		stack[top].c=1;
		maxx=max(maxx,ans);
	}
}

void out(){
	if(!top){
		flag=1;
		return ;
	}
	if(!sum&&stack[top].c)
		ans--;
	if(stack[top].b)
		sum--;
	isu[stack[top--].a]=0;
}

int read2(){
	int i=4;
	int now=0;
	while(o[i]>='0'&&o[i]<='9'){
		now*=10;
		now+=o[i]-'0';
		i++;		
	}
	return now;
}

bool ok(){
	if(o.length()==4&&!maxx)
		return 1;
	if(o.length()!=4&&read2()==maxx)
		return 1;
	return 0;
}

int main(){
//	freopen("complexity.in","r",stdin);
//	freopen("complexity.out","w",stdout);
	cin>>t;
	while(t--){
		cin>>l;
		getchar();
		getline(cin,o);
		top=0;
		flag=0;
		ans=0;
		maxx=0;
		sum=0;
		memset(isu,0,sizeof(isu));
		for(int i=1;i<=l;i++){
			getline(cin,a);
			if(flag)
				continue;
			if(a[0]=='F')
				add();
			else
				out();
		}
		if(top)
			flag=1;
		if(flag){
			printf("ERR\n");
			continue;
		}
		if(ok())
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值