Advertisement
Josif_tepe

Untitled

Feb 17th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     cin >> n;
  10.     for(int T = 0; T < n; T++) {
  11.   string o;
  12.   cin>>o;
  13.   stack<char>p;
  14.         int ne_balansirana = 1;
  15.   for(int y=0; y<o.size(); y+=1){
  16.       if(o[y] == '(' or o[y] == '{' or o[y] == '[') {
  17.           p.push(o[y]);
  18.       }
  19.       else {
  20.           if(o[y] == ')' and  !p.empty() and p.top() == '(') {
  21.               p.pop();
  22.           }
  23.           else if(o[y] == '}' and !p.empty() and p.top() == '{') {
  24.               p.pop();
  25.           }
  26.           else if(o[y] == ']' and !p.empty() and p.top() == '[') {
  27.               p.pop();
  28.           }
  29.           else {
  30.               cout << "NO" << endl;
  31.               ne_balansirana = 0;
  32.               break;
  33.           }
  34.       }
  35.            
  36.     }
  37.  
  38.         if(ne_balansirana == 0) {
  39.             continue;
  40.         }
  41.  
  42.     if(p.empty()) {
  43.         cout << "YES" << endl;
  44.     }
  45.     else {
  46.         cout << "NO" << endl;
  47.     }
  48.    
  49.     }
  50.    
  51.    
  52.     return 0;
  53. }
  54. // ]]][[[[
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement