Advertisement
Derga

Untitled

Aug 22nd, 2023
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include <algorithm>
  2. #include <deque>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class CashBox {
  10. private:
  11.     bool reversed = false;
  12.     deque<int> data;
  13.  
  14. public:
  15.     void push(int num) {
  16.         if (reversed) {
  17.             data.push_front(num);
  18.         } else {
  19.             data.push_back(num);
  20.         }
  21.     }
  22.  
  23.     size_t size() {
  24.         return data.size();
  25.     }
  26.  
  27.     int del() {
  28.         int res;
  29.         if (reversed) {
  30.             res = data.back();
  31.             data.pop_back();
  32.         } else {
  33.             res = data.front();
  34.             data.pop_front();
  35.         }
  36.         return res;
  37.     }
  38.    
  39.     void reverse() {
  40.         reversed = !reversed;
  41.     }
  42.  
  43.     void swap(CashBox &other) {
  44.         std::swap(data, other.data);
  45.         std::swap(reversed, other.reversed);
  46.     }
  47.  
  48.     void move_to(CashBox &other) {
  49.         other.push(del());
  50.     }
  51. };
  52.  
  53. class Shop {
  54. private:
  55.     CashBox first;
  56.     CashBox second;
  57.     bool reversed = false;
  58.     bool allOpened = true;
  59.     vector<int> served;
  60.  
  61.     void get_true(int &place) {
  62.         if (reversed) {
  63.             place = (place + 1) % 2;
  64.         }
  65.     }
  66.  
  67.     void reverse() {
  68.         reversed = !reversed;
  69.     }
  70.  
  71. public:
  72.     void push(int num, int place) {
  73.         if (!allOpened) {
  74.             second.push(num);
  75.             return;
  76.         }
  77.         get_true(place);
  78.         if (place == 0) {
  79.             first.push(num);
  80.         } else {
  81.             second.push(num);
  82.         }
  83.     }
  84.  
  85.     void del(int place) {
  86.         if (!allOpened) {
  87.             if (first.size() > 0) {
  88.                 served.push_back(first.del() % 10);
  89.             } else {
  90.                 served.push_back(second.del() % 10);
  91.             }
  92.             return;
  93.         }
  94.         get_true(place);
  95.         if (place == 0) {
  96.             served.push_back(first.del() % 10);
  97.         } else {
  98.             served.push_back(second.del() % 10);
  99.         }
  100.     }
  101.  
  102.     void close(int place) {
  103.         get_true(place);
  104.         allOpened = false;
  105.         if (place == 0) {
  106.             first.reverse();
  107.             first.swap(second);
  108.             reverse();
  109.         } else {
  110.             second.reverse();
  111.         }
  112.     }
  113.  
  114.     void open() {
  115.         size_t commonSize = first.size() + second.size();
  116.         int half = (commonSize + 1) / 2;
  117.         while (first.size() < half) {
  118.             second.move_to(first);
  119.         }
  120.         while (first.size() > half) {
  121.             first.reverse();
  122.             second.reverse();
  123.             first.move_to(second);
  124.             first.reverse();
  125.             second.reverse();
  126.         }
  127.         second.reverse();
  128.         allOpened = true;
  129.     }
  130.  
  131.     void print() {
  132.         for (auto elem : served) {
  133.             cout << elem;
  134.         }
  135.         cout << '\n';
  136.     }
  137. };
  138.  
  139. int main() {
  140.     Shop shop;
  141.     int n;
  142.     cin >> n;
  143.     char ty;
  144.     int cur = 1;
  145.     for (int i = 1; i <= n; i++) {
  146.         cin >> ty;
  147.         if (ty == 'a') {
  148.             shop.push(cur++, 0);
  149.         }
  150.         if (ty == 'b') {
  151.             shop.push(cur++, 1);
  152.         }
  153.         if (ty == 'A') {
  154.             shop.del(0);
  155.         }
  156.         if (ty == 'B') {
  157.             shop.del(1);
  158.         }
  159.         if (ty == '>') {
  160.             shop.close(0);
  161.         }
  162.         if (ty == ']') {
  163.             shop.close(1);
  164.         }
  165.         if (ty == '<' || ty == '[') {
  166.             shop.open();
  167.         }
  168.     }
  169.     shop.print();
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement