线性数据结构--栈

题目列表

今天巩固了栈的相关知识,本篇文章只有栈的相关题目,对于栈的基础学习请看这篇:
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;
}
相关推荐
lightqjx9 分钟前
【算法】数据结构_并查集
数据结构·算法·并查集
小蒋学算法23 分钟前
算法-掉落的方块-线段树
数据结构·算法
Brilliantwxx24 分钟前
【算法从零到千】【8-15】滑动窗口
数据结构·算法
Lucis__25 分钟前
图的高阶算法:从构造最小生成树到求解最短路径问题
数据结构·c++·算法·图论
sugar__salt9 小时前
从栈队列数据结构到JS原型面向对象全解
前端·javascript·数据结构
froyoisle11 小时前
CSP-J 历年复赛 T1 及解析(2019~2025)
数据结构·c++·算法·csp-j·csp·算法竞赛·信息学
喜欢打篮球的普通人12 小时前
LLVM 后端流程与关键数据结构:从 IR 到机器码的入门笔记
java·数据结构·笔记
Misnearch13 小时前
1、数组/字符串
java·数据结构·算法
008爬虫实战录13 小时前
【数据结构】 树、二叉树、完全二叉树,先序遍历、中序遍历、后序遍历
数据结构·算法
AllData公司负责人13 小时前
大模型赋能AllData数据中台,系列升级|通过联合智谱大模型与BiSheng开源项目,建设企业大模型应用开发平台,支持知识库向量检索!
大数据·数据结构·数据库·算法·大模型·向量数据库·智谱ai