基础数据结构1

作业介绍

#include <bits/stdc++.h>
using namespace std;
int T,n;
stack<unsigned long long>stk;
int main(){
	cin>>T;
	while(T--){
		while(!stk.empty())stk.pop();
		cin>>n;
		for(int i=1;i<=n;i++){
			string str;
			unsigned long long x;
			cin>>str;
			if(str=="push"){
				cin>>x;
				stk.push(x);
			}
			else if(str=="pop"){
				if(!stk.empty()){
					stk.pop();
				}
				else {
					cout<<"Empty"<<endl;
				}
			}
			else if(str=="query"){
				if(!stk.empty()){
					cout<<stk.top()<<endl;
				}
				else {
					cout<<"Anguei!"<<endl;
				}
			}
			else{
				cout<<stk.size()<<endl;
			}
		}
	}
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
stack<char>stk;
int main(){
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++){
		if(s[i]=='(')stk.push(s[i]);
		else if(s[i]==')'){
			if(stk.empty()){
				cout<<"NO"<<endl;
				return 0;
			}
			else{
				stk.pop();
			}
		}
	}
	if(stk.empty()){
		cout<<"YES"<<endl;
	}
	else cout<<"NO"<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
	int T;
	cin >> T;
	while (T--) {
		string s;
		cin >> s;
		stack<int>stk;
		int flag = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '(' || s[i] == '[')stk.push(s[i]);
			else if (s[i] == ')') {
				if (!stk.empty() && stk.top() == '(')stk.pop();
				else {
					flag = 1;
					break;
				}
			} else if (s[i] == ']') {
				if (!stk.empty() && stk.top() == '[')stk.pop();
				else {
					flag = 1;
					break;
				}
			}
		}
		if(!stk.empty() || flag==1){
			cout<<"No"<<endl;
		}
		else cout<<"Yes"<<endl;
	}

	return 0;
}

题目

认领作业后才可以查看作业内容。
状态
正在进行…
题目
17
开始时间
2026-5-17 0:00
截止时间
2026-5-25 23:59
可延期
24 小时