当前位置:网站首页>括号匹配问题(STL)
括号匹配问题(STL)
2022-07-05 10:19:00 【允谦呀】
括号匹配问题(STL)
前面写过一个手写栈的实现方式,原理是一样的,现在发一个使用STL写的简洁方式
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
bool pp(string str,int length){
stack<char> s;
for(int i = 0; i < length; i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{'){
s.push(str[i]); //如果是左括号 进栈
}
else{
if(s.empty())
return false;
char topElem;
topElem = s.top();
s.pop();
if(str[i] == ')' && topElem != '(')
return false;
if(str[i] == ']' && topElem != '[')
return false;
if(str[i] == '}' && topElem != '{')
return false;
}
}
return s.empty();//判断是否为空栈
}
int main(){
string str;
cin >> str;
if(pp(str,str.size()))
cout << "匹配成功" << endl;
else
cout << "匹配失败" << endl;
return 0;
}
边栏推荐
- SQL Server 监控统计阻塞脚本信息
- leetcode:1200. 最小绝对差
- AtCoder Beginner Contest 258「ABCDEFG」
- Atcoder beginer contest 254 "e BFS" f st table maintenance differential array GCD "
- Shortcut keys for vscode
- Activity enter exit animation
- 微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
- Z-blog template installation and use tutorial
- 学习笔记5--高精地图解决方案
- mongoDB副本集
猜你喜欢
SAP UI5 ObjectPageLayout 控件使用方法分享
Usage differences between isempty and isblank
What is the most suitable book for programmers to engage in open source?
Learning II of workmanager
《微信小程序-基础篇》小程序中的事件与冒泡
字节跳动面试官:一张图片占据的内存大小是如何计算
5g NR system architecture
Universal double button or single button pop-up
mongoDB副本集
Learning Note 6 - satellite positioning technology (Part 1)
随机推荐
[可能没有默认的字体]Warning: imagettfbbox() [function.imagettfbbox]: Invalid font filename……
微信小程序触底加载与下拉刷新的实现
Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
一个可以兼容各种数据库事务的使用范例
官网给的这个依赖是不是应该为flink-sql-connector-mysql-cdc啊,加了依赖调
C语言活期储蓄账户管理系统
ConstraintLayout官方提供圆角ImageFilterView
AtCoder Beginner Contest 258「ABCDEFG」
Flink CDC cannot monitor MySQL logs. Have you ever encountered this problem?
AD20 制作 Logo
伪类元素--before和after
各位大佬,我测试起了3条线程同时往3个mysql表中写入,每条线程分别写入100000条数据,用了f
Learning notes 5 - high precision map solution
How to judge that the thread pool has completed all tasks?
Glide advanced level
How can non-technical departments participate in Devops?
What is the most suitable book for programmers to engage in open source?
Golang应用专题 - channel
How to plan the career of a programmer?
ByteDance Interviewer: how to calculate the memory size occupied by a picture