括号配对

小明今年上大学,在大学里发现有很多同学都女朋友,两人整天都在一起腻歪,小明看到后感觉很孤单,现在,给你一行括号序列,你来判断一下其中的括号是否配对。

Input

多组输入,每一组第一行输入一个数T(0${<}$N${\leq}$100),表示有T组测试数据。后面的T行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[", "]", "(", ")" 四种字符  

 

Output

每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No。

Sample Input

 
3 [(]) (]) ([[]()]) 

Sample Output

 
No No Yes 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<string>
using namespace std;
int main()
{
    int t;
    while(cin>>t){
            while(t--)
    {
        string s;
        cin>>s;
        stack<char>sta;
        if(s[0]==']'||s[0]==')'){
            cout<<"No"<<endl;
        }
        else{
            int n;
            n=s.length();
            sta.push(s[0]);
            for(int i=1;i<n;i++){
                if(sta.empty())
                sta.push(s[i]);
                else{
                    if((sta.top()=='('&&s[i]==')')||(sta.top()=='['&&s[i]==']')){
                        sta.pop();
                    }
                    else{
                        sta.push(s[i]);
                    }                
                }                    
            }
            if(sta.empty()) cout<<"Yes"<<endl;
            else cout<<"No"<<endl; 
        }
    }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值