当前位置:网站首页>括号匹配问题(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 write high-quality code?
isEmpty 和 isBlank 的用法区别
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
How can non-technical departments participate in Devops?
非技术部门,如何参与 DevOps?
Ad20 make logo
Learning note 4 -- Key Technologies of high-precision map (Part 2)
ConstraintLayout的流式布局Flow
Who is the "conscience" domestic brand?
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
随机推荐
Activity enter exit animation
What are the top ten securities companies? Is it safe to open an account online?
NCP1342芯片替代料PN8213 65W氮化镓充电器方案
Dedecms website building tutorial
IDEA新建sprintboot项目
Solution of ellipsis when pytorch outputs tensor (output tensor completely)
vscode的快捷键
What is the most suitable book for programmers to engage in open source?
leetcode:1200. 最小绝对差
[论文阅读] KGAT: Knowledge Graph Attention Network for Recommendation
The most complete is an I2C summary
非技術部門,如何參與 DevOps?
Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
Secteur non technique, comment participer à devops?
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
驱动制造业产业升级新思路的领域知识网络,什么来头?
Timed disappearance pop-up
How did automated specification inspection software develop?
Interview: is bitmap pixel memory allocated in heap memory or native
DDOS攻击原理,被ddos攻击的现象