NYOJ---002 括号匹配问题

问题描述
括号匹配就是随即输入一些括号判断括号是否匹配,这个在数据结构就曾经做过实验,不过那样的时间复杂度对于竞赛来说,达不到需求。
先显示一下用数据结构方法做的括号匹配,完全的栈的应用:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct {
    char str[MAXSIZE];
    int top;
}SeqStack;
void InitStack(SeqStack &S) //构造一个空栈S
{
    S.top=-1;
}
int StackEmpty(SeqStack &S)  //判断栈S是否为空栈,若为空栈返回值为真,反之为假
{
    if(S.top==-1)
        return 1;
    else 
        return 0;
}
int StackFull(SeqStack &S)  //判栈S栈满时返回值为真,反之为假
{
    if(S.top==MAXSIZE-1)
    {
        return 1;
    }else
    {return 0;}
}
int Push(SeqStack &S,char x)  //将x置入S栈新栈顶
{   
    S.str[++S.top]=x;
    if(S.top<=MAXSIZE-1) return 1;
    else return 0;

}
int Pop(SeqStack &S,char &x)   //将栈S的栈顶元素弹出,放到x中,栈顶元素减一,栈顶指针移动
{
    if(S.top!=-1)
    {
        x=S.str[S.top--];
        return 1;
    }else{
    return 0;
    }

}

int GetTop(SeqStack &S,char &x)  //将栈S的栈顶元素弹出,放到x中,但栈顶指针保持不变
{
    if(S.top!=-1)
    {
        x=S.str[S.top];
        S.top--;
        return 1;
    }else
        return 0;
}
int Match(char x,char y)
{
    if(x=='('&&y==')') return 1;
    else if(x=='['&&y==']') return 1;
    else if(x=='{'&&y=='}') return 1;
    else return 0;
}
int BracketMatch(char str [])  //str[]中为输入的字符串利用堆栈技术来检查该字符串中的括号是否匹配
{
    SeqStack S;char x;
    InitStack(S);
    for(int i=0;str[i]!='\0';i++)
    {
        switch(str[i])
        {
        case '(':
        case '{':
        case '[': Push(S,str[i]);break;   //将S置入新的栈顶   
        case ')':
        case '}':
        case ']': 
            if(S.top==-1){
                        printf("右边括号存在多余\n");
                     return 0;
                    }
                Pop(S,x);

                    if(!Match(x,str[i])) 
                  {
                  printf("左边括号不匹配!\n");
                  return 0;}

        }

    }
    if(StackEmpty(S))
    {printf("匹配成功!\n");return 1;}
        else
        {   printf("左边字符串多余!\n"); return 0;}


}

int main()
{
    char c[100];
    gets(c);
    BracketMatch(c);
    return 0;
}

对于本题的思想,就是输入一个判断一个,大括号中括号和小括号的ASCLL码要么相差1,要么相差2.所以对于输入的元素,如果相差2个或者1个就”退栈“,将指针后退。

#include<stdio.h>
int main(){
    char c,s[100001],*p;
    int t;
    scanf("%d\n",&t);
    while(t--){
        *s=getchar();                       //读取一个字符 
        p=s+1;                              //将p的指针地址变为s[1] 
        while((c=getchar()!='\n'))          //将c不断地输入!! 
        {
            if((*(p-1)==(c-1))||(*(p-1)==(c-2)))    //判断是否为右边的括号,若为右边的括号则 
            {                            
                    p--;                            //p--,
                }
            else
              {
                 *p++=c;                    //若是左边的括号则*p=c,p++; 
              }
        }
        if(p==s)                            //若p与s的地址相同则清空了
            printf("Yes\n");        
            else 
            printf("No\n");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值