Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- // freopen("input.txt","r",stdin);
- // freopen("output.txt","w",stdout);
- stack<char>s;
- string str;
- int i,n,j,k;
- cin>>n;
- for(k=1;k<=n;k++)
- {
- if(k!=1)
- getline(cin,str);
- else{
- getchar();
- getline(cin,str);
- }
- if(str.size()==0)
- cout<<"Yes"<<endl;
- else if(str[0]==')' || str[0]=='}' || str[0]==']')
- cout<<"No"<<endl;
- else
- {
- for(j=0; j<str.size(); j++)
- {
- if(str[j]=='(')
- {
- s.push('(');
- }
- else if(str[j]=='{')
- {
- s.push('{');
- }
- else if(str[j]=='[')
- {
- s.push('[');
- }
- else if(str[j]==')')
- {
- if(s.empty()==0 && s.top()== '(' )
- s.pop();
- else
- {
- s.push(')');
- break;
- }
- }
- else if(str[j]=='}')
- {
- if(s.empty()==0 && s.top()== '{')
- s.pop();
- else
- {
- s.push('}');
- break;
- }
- }
- else if(str[j]==']')
- {
- if(s.empty()==0 && s.top()== '[')
- s.pop();
- else
- {
- s.push(']');
- break;
- }
- }
- }
- if(s.empty()==1)
- cout<<"Yes"<<endl;
- else
- cout<<"No"<<endl;
- while(s.empty()!=1){
- s.pop();
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement