Advertisement
MathQ_

Untitled

Feb 6th, 2023
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct Node {
  4.   int x;
  5.   Node *next = nullptr;
  6.   Node *prev = nullptr;
  7.   explicit Node(int x) : x(x) {
  8.   }
  9. };
  10.  
  11. struct Stack {
  12.   int size = 0;
  13.   Node *begin = nullptr;
  14.   Node *cur = nullptr;
  15.   void PushBack(int x) {
  16.     ++size;
  17.     if (begin == nullptr) {
  18.       cur = new Node(x);
  19.       begin = cur;
  20.     } else {
  21.       auto new_cur = new Node(x);
  22.       cur->next = new_cur;
  23.       new_cur->prev = cur;
  24.       cur = new_cur;
  25.     }
  26.   }
  27.   void PopBack() {
  28.     --size;
  29.     auto new_cur = cur->prev;
  30.     delete cur;
  31.     cur = new_cur;
  32.   }
  33.   int Back() const {
  34.     return cur->x;
  35.   }
  36.   int Size() const {
  37.     return size;
  38.   }
  39. };
  40.  
  41. void Clean(Stack &a) {
  42.   auto cur = a.cur;
  43.   while (cur != nullptr) {
  44.     auto prev = cur->prev;
  45.     delete cur;
  46.     cur = prev;
  47.   }
  48. }
  49.  
  50. int main() {
  51.   Stack t;
  52.   char c;
  53.   int cnt = 0;
  54.   while (std::cin >> c) {
  55.     if (t.Size() > 0 and t.Back() == '(' and c == ')') {
  56.         t.PopBack();
  57.     } else if (c == '(') {
  58.         t.PushBack(c);
  59.     } else {
  60.         cnt++;
  61.     }
  62.   }
  63.   std::cout << cnt + t.Size() << '\n';
  64.   Clean(t);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement