Advertisement
anasretdinov

Bstack

May 18th, 2021
2,908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     #define ll long long
  2.     #include <iostream>
  3.     using namespace std;
  4.     #include <vector>
  5.     #include <stack>
  6.     int main() {
  7.         string s; cin >> s;
  8.         stack<char> st;
  9.         for (auto i: s){
  10.             if (i == '(' || i == '[' || i == '{'){
  11.                 st.push(i);
  12.             }
  13.             else{
  14.                 if (st.empty()){
  15.                     cout << "NO";
  16.                     return 0;
  17.                 }
  18.                 if ((i == ']' && st.top() != '[') || (i == ')' && st.top() != '(') || (i == '}' && st.top() != '{')) {
  19.                     cout << "NO";
  20.                     return 0;
  21.                 }
  22.                 st.pop();
  23.             }
  24.         }
  25.         if (st.size() > 0){
  26.             cout << "NO";
  27.         }
  28.         else cout << "YES";
  29.     }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement