Salvens

A

Jul 27th, 2023
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include <array>
  2. #include <iostream>
  3. #include <vector>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. #define int long long
  9.  
  10. const long long INF = 1e18 + 7;
  11. const int MAXN = 2e5 + 10;
  12. const int N = 2e5;
  13.  
  14. char invert(char c) {
  15.     if (c == ')') {
  16.         return '(';
  17.     }
  18.     if (c == ']'){
  19.         return '[';
  20.     }
  21.     if (c == '}') {
  22.         return '{';
  23.     }
  24. }
  25.  
  26. signed main() {
  27.     ios_base::sync_with_stdio(false);
  28.     cin.tie(nullptr);
  29.     cout.tie(nullptr);
  30.     string s;
  31.     cin >> s;
  32.     stack<char> st;
  33.     for (int i = 0; i < s.size(); ++i) {
  34.         if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
  35.             st.push(s[i]);
  36.         } else {
  37.             if (st.empty() || invert(s[i]) != st.top()) {
  38.                 cout << "no\n";
  39.                 return 0;
  40.             } else {
  41.                 st.pop();
  42.             }
  43.         }
  44.     }
  45.     cout << (st.empty() ? "yes\n": "no\n");
  46. }
Advertisement
Add Comment
Please, Sign In to add comment