Advertisement
Korotkodul

2_A

Oct 18th, 2023 (edited)
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <string>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::stack;
  8. using std::string;
  9.  
  10. int main() {
  11.   stack<int> bank;
  12.   string str;
  13.   cin >> str;
  14.   int len = str.size();
  15.   int bal = 0;
  16.   bool ok = true;
  17.   for (int i = 0; i < len; ++i) {
  18.     char lt = str[i];
  19.     char pre = '*';
  20.     if (!bank.empty()) {
  21.       pre = bank.top();
  22.     }
  23.     if (lt == ')') {
  24.       bal--;
  25.       if (pre == '(') {
  26.         bank.pop();
  27.       } else {
  28.         ok = false;
  29.         break;
  30.       }
  31.     } else if (lt == ']') {
  32.       bal--;
  33.       if (pre == '[') {
  34.         bank.pop();
  35.       } else {
  36.         ok = false;
  37.         break;
  38.       }
  39.     } else if (lt == '}') {
  40.       bal--;
  41.       if (pre == '{') {
  42.         bank.pop();
  43.       } else {
  44.         ok = false;
  45.         break;
  46.       }
  47.     } else {
  48.       bal++;
  49.       bank.push(lt);
  50.     }
  51.   }
  52.   if (bal != 0) {
  53.     ok = false;
  54.   }
  55.   if (ok) {
  56.     cout << "YES\n";
  57.   } else {
  58.     cout << "NO\n";
  59.   }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement