栈——字符串匹配问题

这题还是写了一上午才过了的,原谅我的技术和水平

#include<bits/stdc++.h>
using namespace std;
char a[]={'{','[','(','<','}',']',')','>'},s[1000];
int b[1000],c[1000],n,m,num,len;
int main()
{
    cin>>n;
    while(n--)
    {
        int top=0;
        bool flag=true;
        cin>>s;
        int    len=strlen(s);
        for(int j=0;j<len;j++)
        {
            for(int k=0;k<8;k++)
            if(s[j]==a[k])
            {
                b[j]=k;
                break;
            }
        }
        for(int i=0;i<len;i++)
        {
            if(b[i]<=3)
            {
                if(top==0||b[i]>=c[top])
                {
                    top++;
                    c[top]=b[i];
                }
                else
                {
                    flag=false;
                    break;
                }
            }
            else if(b[i]>=4)
            {
                if(top>0&&b[i]==c[top]+4)
                top--;
                else
                {
                    flag=false;
                    break;
                }
            }
        }
    if(top)
    cout<<"NO"<<"\n";
    else if(flag==1)
    cout<<"YES"<<"\n";
    else
    cout<<"NO"<<"\n";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/5t2y0/p/9543032.html
今日推荐