括号匹配

· · 个人记录

括号匹配

本人菜鸡一枚,大佬误喷。

先是最简单的

假设一个表达式有英文字母(小写)、运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符。请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”;否则返回“NO”。表达式长度小于255,左圆括号少于20个。

这个很简单,就是数一数左括号和右括号的个数,如果右括号的在某一个位置上比左括号多了就说明不匹配了。

代码如下

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
string s;
long long z,y,l;
int main()
{
    cin>>s;
    l=s.length();
    for(int i=0;i<l;i++)
    {
        if(s[i]=='(')
        {
            z++;
        }
        if(s[i]==')')
        {
            y++;
        }
        if(z<y)
        {
            cout<<"NO";
            return 0;
        }
    }
    if(z!=y)
    {
        cout<<"NO";
        return 0;
    }
    else
    {
        cout<<"YES";
        return 0;
    }
    return 0;
}

在升级一点就是这个

假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[([ ][ ])]等为正确的匹配,[( ])或([ ]( )或 ( ( ) ) )均为错误的匹配。

现在的问题是,要求检验一个给定表达式中的括弧是否正确匹配?

输入一个只包含圆括号和方括号的字符串,判断字符串中的括号是否匹配,匹配就输出 “OK” ,不匹配就输出“Wrong”。输入一个字符串:[([][])],输出:OK。

这个我就模拟了一个栈,如果括号匹配就弹出栈顶,如果匹配错误就直接输出,如果正常就加入栈,最后再判断栈是否为空即可。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
long long z1,y1,z2,y2,l,size;
bool zhong,xiao;
char s1[2600],s[2600];
bool put(char x)
{
    if(s1[size]=='['&&x==']')
    {
        size--;
        return true;
    }
    if(s1[size]=='('&&x==')')
    {
        size--;
        return true;
    }
    if(s1[size]=='['&&x==')')
    {
        return false;
    }
    if(s1[size]=='('&&x==']')
    {
        return false;
    }
    s1[++size]=x;
    return true;
}
int main()
{
    cin>>s;
    l=strlen(s);
    for(int i=0;i<l;i++)
    {
        if(put(s[i])==false)
        {
            cout<<"Wrong";
            return 0;
        }
    }
    if(size!=0)
    {
        cout<<"Wrong";
    }
    else
    {
        cout<<"OK";
    }
    return 0;
}

最后就是终极版

字符串中只含有括号 (),[],<>,{},判断输入的字符串中括号是否匹配。如果括号有互相包含的形式,从内到外必须是<>,(),[],{},例如。输入: [()] 输出:YES,而输入([]),([)]都应该输出NO。

这个涉及到了包含的形式,所以我们果断弄一个函数来判断括号的级别

本人英语不好,函数名就用拼音了,见谅

int ji(char x)
{
    if(x=='<') return 4;
    if(x=='(') return 3;
    if(x=='[') return 2;
    if(x=='{') return 1;
    return 0;
}

最后的return 0 是当栈为空时用的。

然后我们还是像上一个一样,如果括号匹配就弹出栈顶,如果匹配错误就直接输出,如果正常就加入栈但是要判断括号优先级,如果优先级不对也是直接输出,最后再判断栈是否为空即可。

bool put(char x)
{
    if(x=='>')
    {
        if(b[size]=='<')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x==')')
    {
        if(b[size]=='(')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x==']')
    {
        if(b[size]=='[')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x=='}')
    {
        if(b[size]=='{')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(ji(x)>=ji(b[size]))
    {
        b[++size]=x;
        return true;
    }
    else
    {
        return false;
    }
}

然后全部代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
char a[260],b[260];
int n,l,size;
bool c;
int ji(char x)
{
    if(x=='<') return 4;
    if(x=='(') return 3;
    if(x=='[') return 2;
    if(x=='{') return 1;
    return 0;
}
bool put(char x)
{
    if(x=='>')
    {
        if(b[size]=='<')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x==')')
    {
        if(b[size]=='(')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x==']')
    {
        if(b[size]=='[')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(x=='}')
    {
        if(b[size]=='{')
        {
            size--;
            return true;
        }
        else return false;
    }
    if(ji(x)>=ji(b[size]))
    {
        b[++size]=x;
        return true;
    }
    else
    {
        return false;
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        size=0;
        c=false;
        cin>>a;
        l=strlen(a);
        for(int j=0;j<l;j++)
        {
            if(!put(a[j]))
            {
                cout<<"NO"<<endl;
                c=true;
                break;
            }
        }
        if(!c)
        {
            if(size==0)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}