Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int x;
- Node *next = nullptr;
- Node *prev = nullptr;
- explicit Node(int x) : x(x) {
- }
- };
- struct Stack {
- int size = 0;
- Node *begin = nullptr;
- Node *cur = nullptr;
- void PushBack(int x) {
- ++size;
- if (begin == nullptr) {
- cur = new Node(x);
- begin = cur;
- } else {
- auto new_cur = new Node(x);
- cur->next = new_cur;
- new_cur->prev = cur;
- cur = new_cur;
- }
- }
- void PopBack() {
- --size;
- auto new_cur = cur->prev;
- delete cur;
- cur = new_cur;
- }
- int Back() const {
- return cur->x;
- }
- int Size() const {
- return size;
- }
- };
- void Clean(Stack &a) {
- auto cur = a.cur;
- while (cur != nullptr) {
- auto prev = cur->prev;
- delete cur;
- cur = prev;
- }
- }
- int main() {
- Stack t;
- char c;
- int cnt = 0;
- while (std::cin >> c) {
- if (t.Size() > 0 and t.Back() == '(' and c == ')') {
- t.PopBack();
- } else if (c == '(') {
- t.PushBack(c);
- } else {
- cnt++;
- }
- }
- std::cout << cnt + t.Size() << '\n';
- Clean(t);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement