Guest User

Untitled

a guest
Nov 19th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4.  
  5. char s[130];
  6. char stack[130];
  7.  
  8. int main(){
  9.    //freopen("input.txt","r",stdin);
  10.    int t;
  11.    char c;
  12.    scanf("%d%*c", &t);
  13.    for (int i=0; i<t; i++){
  14.        gets(s);
  15.        int n = strlen(s);
  16.        if (n==0){
  17.            printf("Yes\n");
  18.            continue;
  19.        }
  20.        int nstack=0;
  21.        bool win=true;
  22.        for (int j=0; j<n; j++){
  23.            c=s[j];
  24.            if (c=='[' || c=='(')
  25.                stack[nstack++]=c;
  26.            else
  27.                if (nstack==0){
  28.                    printf("No\n");
  29.                    win=false;
  30.                    break;
  31.                }
  32.                else{
  33.                    char last=stack[nstack-1];
  34.                    if (c==')' && last=='[' || c==']' && last=='('){
  35.                        printf("No\n");
  36.                        win=false;
  37.                        break;
  38.                    }
  39.                    else
  40.                        nstack--;
  41.                }
  42.        }
  43.        if (win){
  44.            if (nstack==0)
  45.                printf("Yes\n");
  46.            else
  47.                printf("No\n");
  48.        }
  49.    }
  50.    return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment