- #include<iostream>
- #include<cstdio>
- using namespace std;
- //ifstream cin ("c.in");
- //ofstream cout ("c.out");
- const int MAXSIZE=200;
- class Sqstack
- {
- private:
- char elem[MAXSIZE];
- int top;
- public:
- Sqstack(){top=0;}
- ~Sqstack(){};
- void SetEmpty(){top=0;}//制棧爲空
- int IsEmpty();
- void push(char e);
- char pop();
- char Getpop();
- };
- int Sqstack::IsEmpty()
- {
- if(top==0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void Sqstack::push(char e)
- {
- top++;
- elem[top]=e;
- }
- char Sqstack::pop()
- {
- char x=elem[top];
- top--;
- return x;
- }
- char Sqstack::Getpop()
- {
- return elem[top];
- }
- int main()
- {
- int t;
- string a;
- cin>>t;
- int correct = 1;
- getchar();//吸收回車
- while(t--)
- {
- Sqstack stk;
- correct=1;
- getline(cin,a);
- for(int i=0;i<a.length();i++)
- {
- if(a[i]=='('||a[i]=='[')
- {
- stk.push(a[i]);
- }
- else if(a[i]==')')
- {
- if(stk.Getpop()!='('||stk.IsEmpty())
- {
- correct=0;
- break;
- }
- stk.pop();
- }
- else if(a[i]==']')
- {
- if(stk.Getpop()!='['||stk.IsEmpty())
- {
- correct=0;
- break;
- }
- stk.pop();
- }
- }
- if(!stk.IsEmpty())
- {
- correct=0;
- }
- if(correct)
- {
- cout<<"Yes"<<endl;
- }
- else
- {
- cout<<"No"<<endl;
- }
- }
- }