当前位置:网站首页>Classic application of stack -- bracket matching problem
Classic application of stack -- bracket matching problem
2022-07-06 16:13:00 【Handsome black cat Sheriff】
Bracket matching is a classic problem in stack applications .
Given one only includes (
,)
,{
,}
,[
,]
String , Determines whether the string is valid .
Valid string needs to meet :
1、 Opening parentheses must be closed with closing parentheses of the same type .
2、 The left parenthesis must be closed in the correct order .
Note that an empty string can be considered a valid string .
Sequence stack core code and algorithm
typedef struct { // Sequential stack structure
char data[MAXSIZE];
int top;
}sqstack;
void initstack(sqstack &s) // Initialization sequence stack
bool push(sqstack &s,char e) //push operation
bool pop(sqstack &s,char &e) //pop operation
bool stackempty(sqstack s) // Sentenced to empty
bool chack(char str[],int length) // Match code
{
sqstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{') // hold (,[,{ The left half push To the stack
{
push(s,str[i]);
}
else {
if(stackempty(s)) // First determine whether there are elements to be paired in the stack
return 0;
char topelem;
pop(s,topelem); //pop The element at the top of the stack is paired with it
//cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s); // Finally, after matching, check whether there is any remaining in the stack
}
Sequence stack to achieve complete code
#include <iostream>
#define MAXSIZE 20
using namespace std;
typedef struct {
char data[MAXSIZE];
int top;
}sqstack;
void initstack(sqstack &s) // initialization
{
s.top=-1;
}
bool push(sqstack &s,char e) //push operation
{
if(s.top==MAXSIZE-1)
return 0;
s.top=s.top+1;
s.data[s.top]=e;
return 1;
}
bool pop(sqstack &s,char &e) //pop operation
{
if(s.top==-1)
return 0;
e=s.data[s.top];
s.top=s.top-1;
return 1;
}
bool stackempty(sqstack s) // Empty operation
{
if(s.top==-1)
return 1;
else return 0;
}
bool chack(char str[],int length) // Match operation
{
sqstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{')
{
push(s,str[i]);
}
else {
if(stackempty(s))
return 0;
char topelem;
pop(s,topelem);
//cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s);
}
int main() {
char str[]={'{','[','(',')','(',')',']','}'};
int length=sizeof(str)/sizeof(str[0]); // Here is judgment str The length of the array
cout<<length<<endl;
if(chack(str,length))
cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout <<"over\n";
return 0;
}
Chain stack to achieve complete code
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct lnode{ // Chain stack structure
char data;
struct lnode *next;
}lnode,*linkstack;
bool initstack(linkstack &s) // Initializing the chain stack ( surface )
{
s=(lnode*)malloc(sizeof(lnode));
if(s==NULL)
return 0;
s->next=NULL;
return 1;
}
bool push(linkstack &s,char e) //push( Similar to chain meter insertion )
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(p==NULL)
return 0;
p->data=e;
p->next=s->next;
s->next=p;
return 1;
}
bool pop(linkstack &s,char &e) //pop( Similar to list deletion )
{
if(s->next==NULL)
return 0;
lnode *p;
p=s->next;
e=s->next->data;
s->next=p->next;
free(p);
return 1;
}
bool stackempty(linkstack s) // Sentenced to empty
{
if(s->next==NULL)
return 1;
else return 0;
}
bool chack(char str[],int length) // matching algorithm
{
linkstack s;
initstack(s);
cout<<"initstack"<<endl;
for(int i=0;i<length;i++)
{
//cout<<str[i]<<endl;
if(str[i]=='(' ||str[i]=='[' ||str[i]=='{')
{
push(s,str[i]);
}
else {
if(stackempty(s))
return 0;
char topelem;
pop(s,topelem);
cout<<topelem<<" "<<str[i]<<endl;
if(str[i]==')'&&topelem!='(')
return 0;
if(str[i]==']'&&topelem!='[')
return 0;
if(str[i]=='}'&&topelem!='{')
return 0;
}
}
return stackempty(s);
}
int main() {
char str[]={'{','[','(',')','(',')',']','}'};
int length=sizeof(str)/sizeof(str[0]);
cout<<length<<endl;
if(chack(str,length))
cout<<"yes"<<endl;
else cout<<"no"<<endl;
cout <<"over\n";
return 0;
}
边栏推荐
- The concept of C language array
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- Penetration test (8) -- official document of burp Suite Pro
- C language must memorize code Encyclopedia
- Sword finger offer II 019 Delete at most one character to get a palindrome
- [exercise-5] (UVA 839) not so mobile (balance)
- JS call camera
- 【练习-7】Crossword Answers
- Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
- 第 300 场周赛 - 力扣(LeetCode)
猜你喜欢
Codeforces Round #801 (Div. 2)A~C
QWidget代码设置样式表探讨
Quick to typescript Guide
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Borg maze (bfs+ minimum spanning tree) (problem solving report)
628. Maximum product of three numbers
Write web games in C language
1005. Maximized array sum after K negations
Read and save zarr files
Suffix expression (greed + thinking)
随机推荐
Candy delivery (Mathematics)
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
最全编程语言在线 API 文档
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
(POJ - 3258) River hopper (two points)
[exercise-3] (UVA 442) matrix chain multiplication
If you want to apply for a programmer, your resume should be written like this [essence summary]
栈的经典应用—括号匹配问题
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Nodejs crawler
双向链表—全部操作
Common configuration files of SSM framework
Codeforces Round #798 (Div. 2)A~D
pytorch提取骨架(可微)
[exercise-7] (UVA 10976) fractions again?! (fraction split)
QWidget代码设置样式表探讨
AcWing——第55场周赛
读取和保存zarr文件
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
Sanic异步框架真的这么强吗?实践中找真理