当前位置:网站首页>括号匹配问题(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;
}
边栏推荐
- How to plan the career of a programmer?
- 《剑来》语句摘录(七)
- What are the top ten securities companies? Is it safe to open an account online?
- Coneroller执行时候的-26374及-26377错误
- Apple 5g chip research and development failure? It's too early to get rid of Qualcomm
- 如何判断线程池已经执行完所有任务了?
- @Jsonadapter annotation usage
- Click the picture in the mobile browser and the picture will not pop up
- A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
- 分享.NET 轻量级的ORM
猜你喜欢
Universal double button or single button pop-up
《微信小程序-基础篇》小程序中的事件与冒泡
非技術部門,如何參與 DevOps?
How does redis implement multiple zones?
> Could not create task ‘:app:MyTest.main()‘. > SourceSet with name ‘main‘ not found.问题修复
一个程序员的职业生涯到底该怎么规划?
Workmanager learning 1
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
重磅:国产IDE发布,由阿里研发,完全开源!
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
随机推荐
Learning notes 5 - high precision map solution
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
Qt实现json解析
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
Golang应用专题 - channel
AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
[dark horse morning post] Luo Yonghao responded to ridicule Oriental selection; Dong Qing's husband Mi Chunlei was executed for more than 700million; Geely officially acquired Meizu; Huawei releases M
分享.NET 轻量级的ORM
AD20 制作 Logo
WorkManager的学习二
QT implements JSON parsing
flink cdc不能监听mysql日志,大家遇到过这个问题吧?
How to judge that the thread pool has completed all tasks?
5G NR系统架构
Learning Note 6 - satellite positioning technology (Part 1)
What are the top ten securities companies? Is it safe to open an account online?
横向滚动的RecycleView一屏显示五个半,低于五个平均分布
Events and bubbles in the applet of "wechat applet - Basics"
flex4 和 flex3 combox 下拉框长度的解决办法
C language QQ chat room small project [complete source code]