document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4. using namespace std;
  5. /*
  6. Rules of failing
  7. 1.extra \'(\' or \'[\'
  8. 2.extra \')\' or \']\'
  9. 3.extra notation
  10. 4.miss match like \'(\' to \']\' or \'[\' to \')\'
  11. */
  12.  
  13. int main()
  14. {
  15.     int n;
  16.     cin>>n;
  17.     for(int i=0;i<n;i++){
  18.         string s,temp;
  19.         cin>>s;
  20.         stack<string> stk;
  21.         bool correct = true;
  22.  
  23.         for(int i=0;i<s.length();i++){
  24.             if(s.empty()){
  25.                 break;
  26.             }
  27.             else if(s[i]==\'(\' || s[i]==\'[\'){
  28.                 temp=s[i];
  29.                 stk.push(temp);
  30.             }
  31.             else if(s[i]==\')\' && !stk.empty() && stk.top()=="("){ // notice!! if stk.empty(), then there\'s no stk.top()
  32.                 stk.pop();
  33.             }
  34.             else if(s[i]==\']\' && !stk.empty() && stk.top()=="["){
  35.                 stk.pop();
  36.             }
  37.             else{
  38.                 correct = false;
  39.                 break;
  40.             }
  41.         }
  42.         if(!stk.empty()){ //check if there are extra \'(\' or \'[\'
  43.             correct = false;
  44.         }
  45.         cout<<((correct)? "Yes":"No")<<endl;
  46.     }
  47.     return 0;
  48. }
');