当前位置:网站首页>括号匹配问题(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;
}
边栏推荐
- [observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
- StaticLayout的使用详解
- The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
- Have you learned to make money in Dingding, enterprise micro and Feishu?
- GO项目实战 — Gorm格式化时间字段
- 《微信小程序-基础篇》小程序中的事件与冒泡
- isEmpty 和 isBlank 的用法区别
- 非技术部门,如何参与 DevOps?
- 橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布
- 想请教一下,十大券商有哪些?在线开户是安全么?
猜你喜欢
![C language QQ chat room small project [complete source code]](/img/4e/b3703ac864830d55c824e1b56c8f85.png)
C language QQ chat room small project [complete source code]

Apple 5g chip research and development failure? It's too early to get rid of Qualcomm

字节跳动面试官:一张图片占据的内存大小是如何计算

Who is the "conscience" domestic brand?

Workmanager Learning one

到底谁才是“良心”国产品牌?

风控模型启用前的最后一道工序,80%的童鞋在这都踩坑

学习笔记5--高精地图解决方案

Constrained layout flow

重磅:国产IDE发布,由阿里研发,完全开源!
随机推荐
Glide conclusion
【黑马早报】罗永浩回应调侃东方甄选;董卿丈夫密春雷被执行超7亿;吉利正式收购魅族;华为发布问界M7;豆瓣为周杰伦专辑提前开分道歉...
Workmanager learning 1
重磅:国产IDE发布,由阿里研发,完全开源!
App各大应用商店/应用市场网址汇总
In wechat applet, after jumping from one page to another, I found that the page scrolled synchronously after returning
【SWT组件】内容滚动组件 ScrolledComposite
mongoDB副本集
《通信软件开发与应用》课程结业报告
[vite] 1371 - develop vite plug-ins by hand
How do programmers live as they like?
IDEA新建sprintboot项目
Dedecms website building tutorial
SAP ui5 objectpagelayout control usage sharing
How can PostgreSQL CDC set a separate incremental mode, debezium snapshot. mo
"Everyday Mathematics" serial 58: February 27
Shortcut keys for vscode
The most complete is an I2C summary
Ad20 make logo
C language QQ chat room small project [complete source code]