当前位置:网站首页>Bracket matching -- [implementation of one-dimensional array]
Bracket matching -- [implementation of one-dimensional array]
2022-04-23 20:35:00 【Anonymous&】
Parentheses matching 【 One dimensional array implementation 】
#include<iostream>
#include<string>
#include<stack>
using namespace std;
string st;
bool isLeft(char c)// Determine whether it is an open bracket
{
if(c=='('||c=='{'||c=='[') return true;
else return false;
}
bool isSame(char c1,char c2) // Determine whether parentheses are of the same type
{
if(c1=='('&&c2!=')') return false;
if(c1=='{'&&c2!='}') return false;
if(c1=='['&&c2!=']') return false;
return true;
}
bool isValid(string s)
{
stack<char> b;
for(int i =0;i<s.length();i++)
{
if( isLeft(s[i]) ) b.push(s[i]);// If it's left parenthesis , Direct stack
else
{
if(b.size()==0) return false;// Scan to right parenthesis , But the stack is empty , Description: the match was unsuccessful
char top = b.top();
if(!isSame(top,s[i])) return false;// If the current stack top element and the currently scanned parentheses are not of the same type, it indicates that the matching is not successful
else
b.pop();
}
}
if(b.size()==0) return true;// End of scan , The stack is empty, indicating that the brackets match successfully
else return false;
}
int main(){
cin>>st;
if(isValid(st))
cout<<"bracket is valid"<<endl;
else
cout<<"bracket is not valid"<<endl;
return 0;
}
版权声明
本文为[Anonymous&]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210547369363.html
边栏推荐
- Easy to use nprogress progress bar
- LeetCode 20、有效的括号
- 16MySQL之DCL 中 COMMIT和ROllBACK
- What is the difference between a host and a server?
- Es error: request contains unrecognized parameter [ignore_throttled]
- Use of node template engine
- LeetCode 232、用栈实现队列
- 2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
- The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
- Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
猜你喜欢
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
vulnhub DC:1渗透笔记
Tensorflow 2 basic operation dictionary
Es error: request contains unrecognized parameter [ignore_throttled]
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Plato Farm元宇宙IEO上线四大,链上交易颇高
The ODB model calculates the data and outputs it to excel
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
随机推荐
bounding box iou
Es index (document name) fuzzy query method (database name fuzzy query method)
LeetCode 20、有效的括号
打新债中签以后怎么办,网上开户安全吗
LeetCode 1337、矩阵中战斗力最弱的 K 行
Confusion about thread blocking after calling the read () method of wrapper flow
Recognition of high-speed road signs by Matlab using alexnet
Some basic configurations in interlij idea
Leetcode 709, convert to lowercase
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
LeetCode 542、01 矩阵
How to do after winning the new debt? Is it safe to open an account online
Leetcode 20. Valid parentheses
Vulnhub DC: 1 penetration notes
Scrapy教程 - (2)寫一個簡單爬蟲
ArcGIS js api 4. X submergence analysis and water submergence analysis
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
vulnhub DC:1渗透笔记
LeetCode 1351、统计有序矩阵中的负数
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte