uva 673

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
    int n;
    cin>>n;
    cin.ignore();
    while(n--)
    {
        string temp;
        getline(cin,temp);
        if(temp=="")
        {
            cout<<"Yes"<<endl;
        }
        else
        {
            stack<char> s;
            int i;
            for( i=0;i<temp.length();i++)
            {
                if(temp[i]=='('||temp[i]=='[')
                    s.push(temp[i]);
                else
                {
                    if(!s.empty())
                    {
                        char ch=s.top();
                        if((temp[i]==')'&&ch=='(')||(temp[i]==']'&&ch=='['))
                            s.pop();
                        else
                        {
                            s.push(temp[i]);
                        }
                    }
                    else
                    {
                        s.push(temp[i]);
                    }
                }
            }
            if(s.empty())
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
        }
    }
    return 0;
}

 

posted @ 2012-05-03 23:14  open your eyes  阅读(301)  评论(0编辑  收藏  举报