SHARE
TWEET

Untitled

a guest Apr 21st, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.     string s;
  8.     cin >> s;
  9.     bool is_ok = true;
  10.     stack <char> v;
  11.    
  12.    
  13.     for (char c : s){
  14.         if ( c == '(' || c == '[' || c == '{'){
  15.             v.push(c);
  16.         }
  17.         else {
  18.             if (v.size() == 0){
  19.                 is_ok = false;
  20.                 break;
  21.             }
  22.             char top = v.top();
  23.             if ( (c == ')' && top == '(') || (c == ']' && top == '[') || (c == '}' && top == '{'))
  24.                 v.pop();
  25.             else {
  26.                 is_ok = false;
  27.                 break;
  28.             }
  29.         }
  30.     }
  31.    
  32.     if (v.size() == 0) {
  33.         if (is_ok)
  34.             cout << "OK" << endl;
  35.         else
  36.             cout << "NOT OK" << endl;
  37.     }
  38.     else {
  39.         cout << "NOT OK" << endl;
  40.     }
  41.     return 0;
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top