1355:字符串匹配問題(strs)

【題目描述】

字符串中只含有括號 (),[],<>,{},判斷輸入的字符串中括號是否匹配。如果括號有互相包含的形式,從內到外必須是<>,(),[],{},例如。輸入: [()] 輸出:YES,而輸入([]),([)]都應該輸出NO。

【輸入】

第一行爲一個整數n,表示以下有多少個由括好組成的字符串。接下來的n行,每行都是一個由括號組成的長度不超過255的字符串。

【輸出】

在輸出文件中有n行,每行都是YES或NO。

【輸入樣例】

5
{}{}<><>()()[][]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{{}}{{}}<<>><<>>(())(())[[]][[]]
{<>}{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]
><}{{[]}<<<>><<>>>((<>))(())[[(<>)]][[]]

【輸出樣例】

YES
YES
YES
YES
NO
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define sf(a) scanf("%d\n",&a)
#define pf(a) printf("%.2lf\n",a)
#define pi acos(-1.0)
#define E 1e-8
#define ms(a) memset(a,0,sizeof a)
#define rep(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int idata=2000+5;

int i,j,k;
int judge,flag;
int n,m,t,ans;
int maxx=0,minn=inf;
int cnt,len,sum;
stack<int>s;
priority_queue<ll, vector<ll>,less<ll> >q;
char *ch;

void clear(stack<int> &temp)
{
    stack<int>empty;
    swap(empty,temp);
    return;
}
int main()
{
    sf(t);
    while(t--)
    {
        ch=new char [260];
        cin>>ch;
        len=strlen(ch);
        flag=0;
        clear(s);
        for(i=0;i<len;i++)
        {
            if(s.empty()==true)
            {
                if(ch[i]=='{')
                    s.push(4);
                else if(ch[i]=='<')
                    s.push(1);
                else if(ch[i]=='(')
                    s.push(2);
                else if(ch[i]=='[')
                    s.push(3);
                else
                    flag=1;
            }
            else if(s.empty()==false)
            {
                if(ch[i]=='{')
                {
                    if(s.top()<4)
                        flag=1;
                    else
                        s.push(4);
                }
                else if(ch[i]=='[')
                {
                    if(s.top()<3)
                        flag=1;
                    else
                        s.push(3);
                }
                else if(ch[i]=='(')
                {
                    if(s.top()<2)
                        flag=1;
                    else
                        s.push(2);
                }
                else if(ch[i]=='<')
                {
                    s.push(1);
                }
                else if(ch[i]=='>')
                {
                    if(s.top()!=1)
                        flag=1;
                    else
                        s.pop();
                }
                else if(ch[i]==')')
                {
                    if(s.top()!=2)
                        flag=1;
                    else
                        s.pop();
                }
                else if(ch[i]==']')
                {
                    if(s.top()!=3)
                        flag=1;
                    else
                        s.pop();
                }
                else if(ch[i]=='}')
                {
                    if(s.top()!=4)
                        flag=1;
                    else
                        s.pop();
                }
            }

            if(flag)
            {
                puts("NO");
                break;
            }
        }
        if(flag)
            continue;
        else
        {
            if(s.empty()==true)
            cout<<"YES"<<endl;
            else if(s.empty()==false)
            cout<<"NO"<<endl;
        }
        delete [] ch;

    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章