Advertisement
Nick90

Untitled

May 19th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. #include <queue>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. char getMirrorParanthesis(char one) {
  10.  
  11.     char sign = NULL;
  12.  
  13.     switch (one) {
  14.         case '{': sign = '}'; break;
  15.         case '(': sign = ')'; break;
  16.         case '[': sign = ']'; break;
  17.         case '}': sign = '{'; break;
  18.         case ')': sign = '('; break;
  19.         case ']': sign = '['; break;
  20.         case ' ': sign = ' '; break;
  21.     }
  22.  
  23.     return sign;
  24.  
  25. }
  26.  
  27. int main() {
  28.  
  29.     string line;
  30.     getline(cin, line);
  31.  
  32.     stack<char> left;
  33.     queue<char> right;
  34.  
  35.     for (int i = 0; i < line.size(); i++) {
  36.  
  37.         char paranthesis = line[i];
  38.  
  39.         if (i < line.size() / 2) {
  40.             left.push(paranthesis);
  41.         } else {
  42.             right.push(paranthesis);
  43.         }
  44.     }
  45.  
  46.     bool balanced = true;
  47.  
  48.     if (left.size() == right.size()) {
  49.         while (left.size() && right.size()) {
  50.             char leftSign = left.top(); left.pop();
  51.             char rightSign = right.front(); right.pop();
  52.             if (getMirrorParanthesis(leftSign) != rightSign) {
  53.                 balanced = false;
  54.                 break;
  55.             }
  56.         }
  57.     } else {
  58.         balanced = false;
  59.     }
  60.  
  61.     cout << (balanced ? "YES" : "NO") << endl;
  62.  
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement