线性数据结构--栈

题目列表

今天巩固了栈的相关知识,本篇文章只有栈的相关题目,对于栈的基础学习请看这篇:
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;
}
相关推荐
zander25811 分钟前
114. 二叉树展开为链表
数据结构·链表·深度优先
小肝一下1 小时前
3. 单链表
c语言·数据结构·c++·算法·leetcode·链表·dijkstra
tachibana21 小时前
hot100 前 K 个高频元素(347)
java·数据结构·算法·leetcode
延凡科技15 小时前
多场景落地复盘:端边云架构无人机智能巡检系统设计与实践
大数据·数据结构·人工智能·科技·架构·无人机·能源
皓月斯语20 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
春日见21 小时前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
萌动的小火苗21 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索21 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana221 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
星恒随风1 天前
C++ STL 栈详解:stack 的使用、经典题目与简单模拟实现
开发语言·数据结构·c++·笔记·学习