Advertisement
apl-mhd

balance

Apr 14th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <stack>
  6. using namespace std;
  7. int main(int argc, char **argv)
  8. {
  9.    
  10.     int t;
  11.     cin>>t;
  12.     getchar();
  13.    
  14.     while(t--){
  15.    
  16.     string expression;
  17.     char ch, cTop;  
  18.     cin>>expression;
  19.     stack <char> myStack;
  20.     for(int i=0; i<expression.size(); i++){
  21.        
  22.         ch = expression[i];
  23.        
  24.     if(ch == '(' ||  ch == '['){
  25.            
  26.             myStack.push(ch);
  27.  
  28.         }
  29.     else if(ch == ')' ||  ch == ']' ){
  30.            
  31.            if(!myStack.empty()){
  32.             cTop = myStack.top();
  33.            
  34.             if(ch == ')' && cTop == '(')
  35.                 myStack.pop();
  36.             else if(ch == ']' && cTop == '[')
  37.                myStack.pop();
  38.             else
  39.                
  40.                 myStack.push(ch);
  41.            }
  42.            else
  43.                 myStack.push(ch);
  44.         }
  45.     }
  46.    
  47.    
  48.     if(myStack.empty())
  49.         printf("Yes\n");
  50.     else
  51.         printf("No\n");
  52.  
  53.  
  54. }
  55.                
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement