当前位置:网站首页>括号匹配问题(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;
}
边栏推荐
- leetcode:1200. 最小绝对差
- Who is the "conscience" domestic brand?
- MFC宠物商店信息管理系统
- Learning notes 5 - high precision map solution
- AtCoder Beginner Contest 258「ABCDEFG」
- Should the dependency given by the official website be Flink SQL connector MySQL CDC, with dependency added
- Write double click event
- 请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
- @Jsonadapter annotation usage
- AD20 制作 Logo
猜你喜欢
Pseudo class elements -- before and after
学习笔记4--高精度地图关键技术(下)
Have you learned to make money in Dingding, enterprise micro and Feishu?
AtCoder Beginner Contest 254「E bfs」「F st表维护差分数组gcd」
《天天数学》连载58:二月二十七日
5G NR系统架构
How to judge that the thread pool has completed all tasks?
《微信小程序-基础篇》小程序中的事件与冒泡
WorkManager学习一
isEmpty 和 isBlank 的用法区别
随机推荐
App各大应用商店/应用市场网址汇总
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
【SWT组件】内容滚动组件 ScrolledComposite
Ad20 make logo
【观察】跨境电商“独立站”模式崛起,如何抓住下一个红利爆发时代?
Glide advanced level
> Could not create task ‘:app:MyTest. main()‘. > SourceSet with name ‘main‘ not found. Problem repair
GO项目实战 — Gorm格式化时间字段
La vue latérale du cycle affiche cinq demi - écrans en dessous de cinq distributions moyennes
Personal website construction tutorial | local website environment construction | website production tutorial
《通信软件开发与应用》课程结业报告
Completion report of communication software development and Application
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
Secteur non technique, comment participer à devops?
Usage differences between isempty and isblank
Universal double button or single button pop-up
Workmanager learning 1
php解决redis的缓存雪崩,缓存穿透,缓存击穿的问题
手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
Workmanager Learning one