3.1 栈与括号匹配

3.1 栈与括号匹配_哔哩哔哩_bilibili

 

B2165 括号匹配 - 洛谷

//B2165 括号匹配 链栈版 C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

typedef struct LiNode{
    int data;
    struct LiNode *next;
}LiNode,*LiStack;

//初始化栈
void InitStack(LiStack &L){
    L=(LiNode*)malloc(sizeof(LiNode));
    L->next=NULL;
}

//判断栈是否为空
bool StackEmpty(LiStack L){
    return L->next==NULL;
}

//入栈
void Push(LiStack &L,char e){
    LiNode *s=(LiNode*)malloc(sizeof(LiNode));
    s->data=e;
    s->next=L->next;
    L->next=s;
}

//出栈
void Pop(LiStack &L, char &e){
    if(StackEmpty(L)) return;
    LiNode *s=L->next;
    e=s->data;
    L->next=s->next;
    free(s);
}

//判断是否括号匹配
bool BracketCheck(char str[], int n){
    LiStack S; //声明链栈
    InitStack(S);
    for(int i=0; i<n; i++){
        if(str[i]=='('||str[i]=='['||str[i]=='{'){
            Push(S,str[i]); //扫描到左括号,入栈
        }
        else{
            //若扫描到右括号且栈空(右单身)
            if(StackEmpty(S)) return false;
            char e;
            Pop(S,e); //左括号出栈
            //若类型不匹配
            if(e=='('&&str[i]!=')' ||
               e=='['&&str[i]!=']' ||
               e=='{'&&str[i]!='}') return false;
        }
    }
    return StackEmpty(S); //扫完栈空则匹配,栈不空(左单身)
}
int main(){
    int T; scanf("%d",&T);
    char s[1000005];
    for(int i=0;i<T;i++){
        scanf("%s",s);
        int n=strlen(s);
        if(BracketCheck(s,n)) puts("YES");
        else puts("NO");
    }
    return 0;
}

 

//B2165 括号匹配 数组版 C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>

char data[1000005];
int top;

//初始化栈
void InitStack(){
    top=-1; //初始化栈顶指针
}
//判断栈是否为空
bool StackEmpty(){
    return top==-1;
}
//进栈
void Push(char e){
    data[++top]=e;
}
//出栈
void Pop(char &e){
    e=data[top--];
}

//判断是否括号匹配
bool BracketCheck(char str[], int n){
    InitStack();
    for(int i=0; i<n; i++){
        if(str[i]=='('||str[i]=='['||str[i]=='{'){
            Push(str[i]); //扫描到左括号,入栈
        }
        else{
            //若扫描到右括号且栈空(右单身)
            if(StackEmpty()) return false;
            char e;
            Pop(e); //左括号出栈
            //若类型不匹配
            if(e=='('&&str[i]!=')' ||
               e=='['&&str[i]!=']' ||
               e=='{'&&str[i]!='}') return false;
        }
    }
    return StackEmpty(); //扫完栈空则匹配,栈不空(左单身)
}
int main(){
    int T; scanf("%d",&T);
    char s[1000005];
    for(int i=0;i<T;i++){
        scanf("%s",s);
        int n=strlen(s);
        if(BracketCheck(s,n)) puts("YES");
        else puts("NO");
    }
    return 0;
}

 

//B2165 括号匹配 C++版
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;

//判断是否括号匹配
bool BracketCheck(char str[], int n){
    stack<char> S;
    for(int i=0; i<n; i++){
        if(str[i]=='('||str[i]=='['||str[i]=='{'){
            S.push(str[i]); //扫描到左括号,入栈
        }
        else{
            //若扫描到右括号且栈空(右单身)
            if(S.empty()) return false;
            char e=S.top();
            S.pop(); //左括号出栈
            //若类型不匹配
            if(e=='('&&str[i]!=')' ||
               e=='['&&str[i]!=']' ||
               e=='{'&&str[i]!='}') return false;
        }
    }
    return S.empty(); //扫完栈空则匹配,栈不空(左单身)
}
int main(){
    int T; scanf("%d",&T);
    char s[1000005];
    for(int i=0;i<T;i++){
        scanf("%s",s);
        int n=strlen(s);
        if(BracketCheck(s,n)) puts("YES");
        else puts("NO");
    }
    return 0;
}

 

.

posted @ 2026-09-15 06:36  董晓  阅读(13)  评论(0)    收藏  举报