线性数据结构--栈

题目列表

今天巩固了栈的相关知识,本篇文章只有栈的相关题目,对于栈的基础学习请看这篇:
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;
}
相关推荐
Mrlxl.cn1 小时前
计算机网络——网络层
c语言·数据结构·计算机网络·考研
寒秋花开曾相惜3 小时前
(学习笔记)4.2 逻辑设计和硬件控制语言HCL(4.2.1 逻辑门&4.2.2 组合电路和HCL布尔表达式)
linux·网络·数据结构·笔记·学习·fpga开发
码完就睡5 小时前
数据结构——哈希表原理与C语言实现总结
数据结构·散列表
山甫aa7 小时前
二叉树遍历----从零开始的数据结构
数据结构·c++·二叉树
北顾笙9807 小时前
day35-数据结构力扣
数据结构·算法·leetcode
cpp_25018 小时前
P2249 【深基13.例1】查找
数据结构·c++·算法·题解·二分·洛谷
山甫aa8 小时前
二叉树算法-----从零开始的算法
数据结构·算法
睡觉就不困鸭9 小时前
第十七天 翻转字符串里的单词
数据结构·算法·哈希算法·散列表
睡觉就不困鸭10 小时前
第十八天 有效的括号
数据结构·算法
浅念-10 小时前
分治算法专题|LeetCode高频经典题目详细题解
数据结构·c++·算法·leetcode·职场和发展·排序·分治