时间复杂度

题目链接:https://www.luogu.com.cn/problem/P3952

题意:

模拟,求循环时间复杂度

思路:

首先读入是最重要的,

共有l行,所以循环l次

由于输入格式,所以先输出opt,如果是F,那说明后面还有3个,变量用char存,后面俩个用string存就可以

发现题目给的整数范围是小于100的,而n的ascii是等于110的,于是把两个都转为int比较就可以

任务1:判断是否err:1.cnte是否等于cntf 2.变量ch是否在同一循环中出现过

1好办,2需要我们用栈+set来存,当碰到e后,将栈顶变量出栈并且在set中抹除它

任务2:判断时间复杂度是否正确

用count来存当前的1n循环的次数,用res存整个程序中最大1n循环次数

由于当x>y时进不去,所以它之后读入的若干个f和e都没用

用tm栈来判断最近的是否是1~n循环

注意的是,如果er那么不能直接break,因为每个程序一定要读入l行,直接跳出就读不了l行了

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define endl "\n"
#define fi first
#define se second
//#pragma GCC optimize(3)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const ll llmax=LLONG_MAX;
const int maxn=1e5+5;
const int mod=1e9+7;

void solve(){
	int l;string s;cin>>l;
	cin>>s;
	bool er=false;
	bool ok=true;
	set<char>st;
	stack<char>stk;
	int count=0;
	int cnte=0,cntf=0;
	int res=0;
	stack<bool>tm;
	int dixiao=0,counte=0;
	for(int i=1;i<=l;i++){
		char opt;cin>>opt;
//		cout<<"opt "<<opt<<endl;
		if(opt=='F'){
			cntf++;
			char ch;cin>>ch;
//			cout<<"ch "<<ch<<endl;
			if(st.count(ch)){
				er=true;
			}else{
				st.insert(ch);
				stk.push(ch);
			}
			
			string str1,str2;cin>>str1>>str2;
			int x=0,y=0;
			for(int i=0;i<str1.size();i++){
				if(isdigit(str1[i])){
					x=x*10+(str1[i]-'0');
				}
			}
			if(x==0)x=110;
			for(int i=0;i<str2.size();i++){
				if(isdigit(str2[i])){
					y=y*10+(str2[i]-'0');
				}
			}
			if(y==0)y=110;
//			cout<<"str1 "<<str1<<endl;
//			cout<<"str2 "<<str2<<endl;
//			cout<<"x "<<x<<endl;
//			cout<<"y "<<y<<endl;
		if(ok){
			if(x<=y){
				if(y==110){
					if(x!=110){
						count++;
						tm.push(true);
					}else{
						tm.push(false);
					}
				}else{
					tm.push(false);
				}
			}else{
				ok=false;
			}
	}else{
		dixiao++;
	}
		}else{
			cnte++;
			if(!ok)counte++;
			if(counte==dixiao+1){
				ok=true;
				dixiao=0;
				counte=0;
			}
//			cout<<"res "<<res<<endl;
			res=max(res,count);
			if(stk.size()){
			st.erase(stk.top());
			stk.pop();}
			if(tm.size()&&tm.top()==true)count--;
			if(tm.size())tm.pop();
		}
	}
//	cout<<"cntf "<<cntf<<endl;
//	cout<<"cnte "<<cnte<<endl;
	if(cnte!=cntf)er=true;
	if(er||stk.size()){
		cout<<"ERR"<<endl;
		return;
	}
	bool ans=false;
	if(s=="O(1)"&&res==0)ans=true;
	else{
		int f=0;
		for(int i=4;;i++){
			if(!isdigit(s[i]))break;
			f=f*10+(s[i]-'0');	
		}
//		cout<<"f "<<f<<endl
		if(f==res)ans=true;
	}
	if(ans)cout<<"Yes"<<endl;else cout<<"No"<<endl;
}

signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	int T;cin>>T;
	
	while(T--){
	solve();
	}
	
	return 0;
}


posted @ 2025-03-08 10:12  Marinaco  阅读(17)  评论(0)    收藏  举报
//雪花飘落效果