D1166:括号匹配

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D1166:括号匹配相关的知识,希望对你有一定的参考价值。

描述
假设表达式中只包含三种括号:圆括号、方括号和花括号,它们可相互嵌套,如([{}])或({[][()]})等均为正确的格式,而{[]})}或{[()]或([]}均为不正确的格式.

输入一串括号
如果输入的右括号多余,输出:Extra right brackets
如果输入的左括号多余, 输出:Extra left brackets
如果输入的括号不匹配,输出:Brackets not match
如果输入的括号匹配,输出:Brackets match
输入
{{{{)))
输出
Brackets not match
样例输入

{([)]}

样例输出

Brackets not match

 

思路:运用栈结构,左右括号输入时进行判定,并把匹配括号弹出,如果是左括号输入,如果为右括号弹出相应左括号,如果没有匹配括号就输入,计算剩余括号,如果只有右括号数为0,那么左括号剩余,只有左括号为0,那么右括号剩余,左右括号数都不为0,,那么括号不匹配。

 

 

代码:

 

#include
#include
using namespace std;
class Seqstack
{
        private:
                int top;
                char data[20];
        public:
                Seqstack(){top=-1;}
                void Push(char x){data[++top]=x;}
                void Pop(){top--;}
                char Gettop(){if(top!=-1)return data[top];}
                void Match(char a[],int i);
};
void Seqstack::Match(char a[],int i)
{
        int j,m=0,n=0;
        for(j=0;j<</SPAN>i;j++)
        {
                if(top!=-1)//第二个括号开始输入
                {
                        if(a[j]==‘(‘||a[j]==‘[‘||a[j]==‘{‘)
                        {
                                Push(a[j]);//如果为左括号输入
                        }
                        else if(a[j]==‘)‘||a[j]==‘]‘||a[j]==‘}‘)
                        {
                                if(Gettop()==‘(‘&&a[j]==‘)‘)
                                {
                                        Pop();

                            }
                                else if(Gettop()==‘[‘&&a[j]==‘]‘)
                                {
                                        Pop();
                                }
                                else if(Gettop()==‘{‘&&a[j]==‘}‘)
                                {
                                        Pop();
                                }//如果有相应左括号匹配右括号弹出
                                else
                                {
                                        Push(a[j]);
                                }//如果没有匹配的左括号输入
                        }
                }
                else if(top==-1)
                {
                        Push(a[j]);
                }//第一个括号输入
        }
        if(top==-1)
        {
                cout<<"Brackets match"<<endl;
        }//如果栈为空,括号匹配
        else
        {
                while(top!=-1)
                {
                        if(Gettop()==‘(‘||Gettop()==‘[‘||Gettop()==‘{‘)
                        {
                                m++;
                                Pop();
                        }//计算左括号数
                        else
                        {
                                n++;
                                Pop();
                        }//计算右括号数
                }
                if(m==0)
                {
                        cout<<"Extra right brackets"<<endl;
                }//如果左括号数为0,右括号剩余
                else if(n==0)
                {
                        cout<<"Extra left brackets"<<endl;
                }//如果右括号数为0,左括号剩余
                else
                {
                        cout<<"Brackets not match"<<endl;
                }//否则不匹配
        }
}
int main()
{
        Seqstack s;
        char a[20];
        cin>>a;
        int i=strlen(a);
        s.Match(a,i);
        return 0;
}

 

以上是关于D1166:括号匹配的主要内容,如果未能解决你的问题,请参考以下文章

用栈检测括号匹配

Java 栈 如何实现括号匹配

1153.括号匹配

如何用正则表达式匹配括号中的内容,不包含括号

vscode如何更改括号匹配后的颜色?

jzyzoj 栈——P1148:括号匹配加强版