线性数据结构--栈

题目列表

今天巩固了栈的相关知识,本篇文章只有栈的相关题目,对于栈的基础学习请看这篇:
C++ 栈(stack)的详细基本用法

【模板】栈的操作

来源:【模板】栈的操作

题目


解题思路

这道题非常简单,就是最基本的考察对栈的操作。直接按照题目要求来操作就可以了;

代码实现

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define endl '\n'
const int N=1e6+6;
void solve()
{
     int n;cin>>n;
     stack<int>st;
     for(int i=1;i<=n;i++)
     {
     	string s;cin>>s;
     	if(s[0]=='p'&&s[1]=='u')
     	{
     		int x;cin>>x;
     		st.push(x);
		}
		if(s[0]=='p'&&s[1]=='o')
		{
			if(!st.empty())st.pop();
			else cout<<"Empty"<<endl;
		}
		if(s[0]=='q')
		{
			if(!st.empty())cout<<st.top()<<endl;
			else cout<<"Empty"<<endl;
		}
		if(s[0]=='s')
		{
			cout<<st.size()<<endl;
		}
	 }
}
signed main()
{
	IOS;
	int _=1;
	// cin>>_;
	while(_--)
	solve();
	return 0;
}

点击消除

来源:点击消除

题目


解题思路

这道题用栈来做是最简单的,我们直接让元素依次入栈,如果栈为空,或者栈顶元素与当前要入栈的元素不同就让元素入栈;如果栈不为空并且栈顶元素与当前要入栈的元素相同,那么二者可以被点击消除,我们直接移除栈顶元素即可。这样操作完后,我们看栈是否为空,如果为空说明全消除掉了,就输出0,如果不为空就逆序输出栈中元素。

代码实现

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define endl '\n'
const int N=1e6+6;
void solve()
{
	string s;
	cin>>s;
	stack<char>st;
	for(int i=0;i<s.size();i++)
	{
		if(st.empty()||st.top()!=s[i])
		st.push(s[i]);
		else
		st.pop();
	}
	if(st.size()==0)
	{
		cout<<0;
		return ;
	}
	string s1;
	while(!st.empty())
	{
		char c=st.top();
		s1+=st.top();
		st.pop();
	}
	reverse(s1.begin(),s1.end());
	cout<<s1;
}
signed main()
{
	IOS;
	int _=1;
	// cin>>_;
	while(_--)
	solve();
	return 0;
}

括号的匹配

来源:括号的匹配

题目


解题思路

这道题要注意的点是,括号嵌套顺序以及括号能否相互匹配。我们可以将每个括号转化为数字,并且这个数字是按照正括号嵌套顺序由大到小4 ~ 1,反括号相反-4 ~ -1。那么我们就可以巧妙利用栈来完成这道题:首先我们输入这个字符串并遍历它,首先转化为数字,然后判断数字是否大于0,如果大于0并且栈不空且数字大于栈定元素,说明括号嵌套顺序不对,是不合法的,就不用往下判断了,否则将num入栈;如果数字小于0,并且栈非空且数字的相反数不等于栈顶,则括号有误,不合法,否则如果相同则直接删除栈顶元素。最终如果栈空并且没有不合法的情况输出YES否则输出NO.

代码实现

cpp 复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
#define int long long
#define PII pair<int,int>
#define fi first
#define se second
#define endl '\n'
const int N=1e6+6;
int transform(char c)
{
	if(c=='{')return 4;
	if(c=='[')return 3;
	if(c=='(')return 2;
	if(c=='<')return 1;
	if(c=='>')return -1;
	if(c==')')return -2;
	if(c==']')return -3;
	if(c=='}')return -4;
    return 0;
	
}
void solve()
{
	string s;
	cin>>s;
	stack<int>st;
	bool f=true;
	for(auto c:s)
	{
		int num=transform(c);
		if(num>0)
		{
			if(!st.empty()&&num>st.top())
			{
				f=false;
				break;
			}
			st.push(num);
		}
		else
		{
			if(st.empty()||st.top()!=-num)
			{
				f=false;
				break;
			}
			st.pop();
		}
	}
	if(f&&st.empty())
	cout<<"YES"<<endl;
	else
	cout<<"NO"<<endl;
}
signed main()
{
	IOS;
	int _=1;
	 cin>>_;
	while(_--)
	solve();
	return 0;
}
相关推荐
李昊哲小课2 小时前
Python 数据结构示例
开发语言·数据结构·python
_日拱一卒2 小时前
LeetCode(力扣):只出现一次的数字
java·数据结构·算法
七七肆十九2 小时前
PTA 习题4-7 最大公约数和最小公倍数
数据结构·算法
XiaoHu02072 小时前
C/C++数据结构与算法(第二弹)
java·开发语言·数据结构
NGC_66112 小时前
八大排序对比及实现
数据结构·算法·排序算法
FMRbpm2 小时前
斑马日记2026.3.13
数据结构·算法
仰泳的熊猫11 小时前
题目2194:蓝桥杯2018年第九届真题-递增三元组
数据结构·c++·算法
啊哦呃咦唔鱼13 小时前
LeetCode hot100-15 三数之和
数据结构·算法·leetcode
leluckys13 小时前
算法-链表-二、成对交换两个节点
数据结构·算法·链表