Advertisement
Guest User

146-bracket

a guest
Feb 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n; scanf("%d",&n);
  6.     while(n--){
  7.         stack<char> S;
  8.         char a[100005];
  9.         scanf(" %s",a);
  10.         int l=strlen(a);
  11.         for(int i=0;i<l;i++){
  12.             if(S.empty() && !isalpha(a[i])) S.push(a[i]);
  13.             else{
  14.                 if(a[i]=='(' || (a[i]==')' && S.top()!='(')) S.push(a[i]);
  15.                 else if(a[i]==')' && S.top()=='(') S.pop();
  16.                 else if(a[i]=='[' || (a[i]==']' && S.top()!='[')) S.push(a[i]);
  17.                 else if(a[i]==']' && S.top()=='[') S.pop();
  18.                 else if(a[i]=='{' || (a[i]=='}' && S.top()!='{')) S.push(a[i]);
  19.                 else if(a[i]=='}' && S.top()=='{') S.pop();
  20.             }
  21.         }
  22.         if(S.empty()) printf("Yes\n");
  23.         else printf("No\n");
  24.     }
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement