当前位置:网站首页>括号匹配问题(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;
}
边栏推荐
- Timed disappearance pop-up
- Ad20 make logo
- How did automated specification inspection software develop?
- 请问大佬们 有遇到过flink cdc mongdb 执行flinksql 遇到这样的问题的么?
- Swift uses userdefaults and codable to save an array of class objects or structure instances
- 《天天数学》连载58:二月二十七日
- LiveData 面试题库、解答---LiveData 面试 7 连问~
- @Serializedname annotation use
- 手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
- 沟通的艺术III:看人之间 之倾听
猜你喜欢
学习笔记4--高精度地图关键技术(下)
Fluent generates icon prompt logo widget
In the year of "mutual entanglement" of mobile phone manufacturers, the "machine sea tactics" failed, and the "slow pace" playing method rose
Workmanager Learning one
AtCoder Beginner Contest 258「ABCDEFG」
Blockbuster: the domestic IDE is released, developed by Alibaba, and is completely open source!
AD20 制作 Logo
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
5G NR系统架构
随机推荐
The Alipay in place function can't be found, and the Alipay in place function is offline
[vite] 1371 - develop vite plug-ins by hand
@Jsonadapter annotation usage
[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
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
isEmpty 和 isBlank 的用法区别
Learning Note 6 - satellite positioning technology (Part 1)
How did automated specification inspection software develop?
Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
WorkManager學習一
mongoDB副本集
TSQL–标示列、GUID 、序列
How does redis implement multiple zones?
Window下线程与线程同步总结
Golang应用专题 - channel
【Vite】1371- 手把手开发 Vite 插件
@SerializedName注解使用
StaticLayout的使用详解
Applet image height adaptation and setting text line height
一个程序员的职业生涯到底该怎么规划?