2021-10-12每日刷题打卡

2021-10-12每日刷题打卡

奥本赛——栈

[1355:字符串匹配问题(strs)](信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn))

【题目描述】

字符串中只含有括号(),[],<>,{},判断输入的字符串中括号是否匹配。如果括号有互相包含的形式,从内到外必须是<>,(),[],{},例如。输入: [()] 输出:YES,而输入([]),([)]都应该输出NO

【输入】

第一行为一个整数nn,表示以下有多少个由括好组成的字符串。接下来的nn行,每行都是一个由括号组成的长度不超过255255的字符串。

【输出】

在输出文件中有nn行,每行都是YESNO

【输入样例】

5 
{}{}<><>()()[][] 
{{}}{{}}<<>><<>>(())(())[[]][[]] 
{{}}{{}}<<>><<>>(())(())[[]][[]] 
{<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]] 
><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]`

【输出样例】`
YES 
YES 
YES 
YES 
NO

要注意这里有个特殊条件,“从内到外必须是<>,(),[],{}”,也就是说,不是遇到左括号我们就直接入栈,而是要判断一下栈顶元素,如果为空就直接插入,如果栈不为空就看栈顶元素是谁,比如我们要入栈的元素是’(‘,就要看看顶端的元素,如果是’(‘,’[‘,’{‘就能入栈,如果是’<'就应该报错。以此类推。如果我们遍历到右括号就要看看顶端元素是不是对应的左括号,如果不是或者栈为空就报错。遍历完后要对栈进行判断,如果栈为空就输出yes,如果不为空就要输出no,同时清空栈,防止对下一次遍历出现影响

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include<math.h>
#include <set> 
#include <iomanip>
#include<unordered_map>
#include<stack>

int main()
{
	stack<char>sta;
	string str;
	vector<string>v;
	int num;
	cin >> num;
	while (num--)
	{
		bool b = true;
		cin >> str;
		for (int i = 0; i < str.size(); i++)
		{
			if (str[i] == '<')
			{
				if (sta.size() != 0)
				{
					if (sta.top() == '{' || sta.top() == '[' || sta.top() == '(' || sta.top() == '<')
					{
						sta.push(str[i]);
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
				else
				{
					sta.push(str[i]);
				}
			}
			else if (str[i] == '(')
			{
				if (sta.size() != 0)
				{
					if (sta.top() == '{' || sta.top() == '[' || sta.top() == '(')
					{
						sta.push(str[i]);
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
				else
				{
					sta.push(str[i]);
				}
			}
			else if (str[i] == '[')
			{
				if (sta.size() != 0)
				{
					if (sta.top() == '{' || sta.top() == '[')
					{
						sta.push(str[i]);
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
				else
				{
					sta.push(str[i]);
				}
			}
			else if (str[i] == '{')
			{
				if (sta.size() != 0)
				{
					if (sta.top() == '{')
					{
						sta.push(str[i]);
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
				else
				{
					sta.push(str[i]);
				}
			}
			else if (str[i] == ')')
			{
				if (sta.empty())
				{
					b = false;
					v.push_back("NO");
					break;
				}
				else
				{
					if (sta.top() == '(')
					{
						sta.pop();
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
			}
			else if (str[i] == ']')
			{
				if (sta.empty())
				{
					b = false;
					v.push_back("NO");
					break;
				}
				else
				{
					if (sta.top() == '[')
					{
						sta.pop();
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
			}
			else if (str[i] == '>')
			{
				if (sta.empty())
				{
					b = false;
					v.push_back("NO");
					break;
				}
				else
				{
					if (sta.top() == '<')
					{
						sta.pop();
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
			}
			else if (str[i] == '}')
			{
				if (sta.empty())
				{
					b = false;
					v.push_back("NO");
					break;
				}
				else
				{
					if (sta.top() == '{')
					{
						sta.pop();
					}
					else
					{
						b = false;
						v.push_back("NO");
						break;
					}
				}
			}
		}
		if (b)
		{
			if (!sta.empty())
			{

				v.push_back("NO");
			}
			else
			{
				v.push_back("YES");
			}
		}
		while (sta.size() != 0)
		{
			sta.pop();
		}
	}
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << endl;
	}


	return 0;
}

[1354:括弧匹配检验](信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn))

【题目描述】

假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())[([ ][ ])]等为正确的匹配,[( ])([ ]( )( ( ) ) )均为错误的匹配。

现在的问题是,要求检验一个给定表达式中的括弧是否正确匹配?

输入一个只包含圆括号和方括号的字符串,判断字符串中的括号是否匹配,匹配就输出 “OK” ,不匹配就输出“Wrong”。输入一个字符串:[([][])],输出:OK

【输入】

输入仅一行字符(字符个数小于255255)。

【输出】

匹配就输出 “OK” ,不匹配就输出“Wrong”。

【输入样例】

[(])

【输出样例】
Wrong

这种题已经说腻了,用栈存储,遇到’(‘和’['就入栈,遇到右括号就判断栈是否为空,如果为空就输出Wrong然后结束程序,如果不为空就判断栈顶元素是不是对应的左括号,如果是就弹出顶端元素。遍历完后判断栈内是否为空,为空就输出OK,不为空就输出Wrong。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include<math.h>
#include <set> 
#include <iomanip>
#include<unordered_map>
#include<stack>

int main()
{
	stack<char>sta;
	string str;
	cin >> str;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '(' || str[i] == '[')
		{
			sta.push(str[i]);
		}
		else if (str[i] == ')')
		{
			if (sta.empty())
			{
				cout << "Wrong" << endl;
				return 0;
			}
			else
			{
				if (sta.top() == '(')
				{
					sta.pop();
				}
				else
				{
					cout << "Wrong" << endl;
					return 0;
				}
			}
		}
		else if (str[i] == ']')
		{
			if (sta.empty())
			{
				cout << "Wrong" << endl;
				return 0;
			}
			else
			{
				if (sta.top() == '[')
				{
					sta.pop();
				}
				else
				{
					cout << "Wrong" << endl;
					return 0;
				}
			}
		}
	}
	if (!sta.empty())
	{
		cout << "Wrong" << endl;
	}
	else
	{
		cout << "OK" << endl;
	}

	return 0;
}

[1357:车厢调度(train)](信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn))

这题和力扣上的946. 验证栈序列一样,我放一起讲

【题目描述】

有一个火车站,铁路如图所示,每辆火车从A驶入,再从B方向驶出,同时它的车厢可以重新组合。假设从A方向驶来的火车有nn节(n≤1000n≤1000),分别按照顺序编号为11,22,33,…,nn。假定在进入车站前,每节车厢之间都不是连着的,并且它们可以自行移动到B处的铁轨上。另外假定车站C可以停放任意多节车厢。但是一旦进入车站C,它就不能再回到A方向的铁轨上了,并且一旦当它进入B方向的铁轨,它就不能再回到车站C。img

负责车厢调度的工作人员需要知道能否使它以a1a1,a2a2,…,anan的顺序从B方向驶出,请来判断能否得到指定的车厢顺序。

【输入】

第一行为一个整数nn,其中n≤1000n≤1000,表示有nn节车厢,第二行为nn个数字,表示指定的车厢顺序。

【输出】

如果可以得到指定的车厢顺序,则输出一个字符串“YES”,否则输出“NO”(注意要大写,不包含引号)。

【输入样例】

5 5 4 3 2 1

【输出样例】
YES

要验证正确的出栈序列,想了半天,觉得让它出栈入栈的操作一起进行。准备一个栈sta,一个vector容器v,准备两个数num和math,先接收num,然后用math和for循环把出栈顺序全部存v里,再把math调为1,通过while遍历,当math大于num时结束遍历,每次math和v[0]比较,如果不相同就把math入栈。如果相同就通过迭代器和erase函数把v[0]删除(后来发现其实可以直接拿栈来替代vector容器,一样的效果而且更简便),再判断栈是否为空,这里不用for判断而是用while,如果为空就什么事也不发生,如果不为空就进入while开始遍历,每次把栈sta的顶端元素和v[0]比较,如果相同就删除v[0]而且把sta栈顶元素弹出,如果不相等就break结束内部while循环。每次循环math++。当math>num结束遍历后,再用while遍历sta,当sta为空时结束遍历,每次遍历判断栈顶元素和v[0],如果相同就删除v[0]而且把sta栈顶元素弹出,如果不相等就输出NO同时return 0结束程序。当sat为空时结束遍历,输出YES。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<list>
#include<string>
#include<map>
#include<math.h>
#include <set> 
#include <iomanip>
#include<unordered_map>
#include<stack>

int main()
{
	stack<int>sta;
	vector<int>v;
	int num, math;
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		cin >> math;
		v.push_back(math);
	}
	math = 1;
	while (math <= num)
	{
		if (math == v[0])
		{
			vector<int>::iterator it = v.begin();
			v.erase(it);
			while (sta.size() != 0)
			{
				if (sta.top() == v[0])
				{
					sta.pop();
					vector<int>::iterator it = v.begin();
					v.erase(it);
				}
				else
				{
					break;
				}
			}
		}
		else
		{
			sta.push(math);
		}
		math++;
	}
	while (v.size() != 0)
	{
		if (v[0] == sta.top())
		{
			sta.pop();
			vector<int>::iterator it = v.begin();
			v.erase(it);
		}
		else
		{
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值