Advertisement
habur331

Untitled

Feb 18th, 2022
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. template<class T>
  8. class ICircularBoundedQueue {
  9. public:
  10.  
  11.     virtual void offer(T value) = 0; // insert an element to the rear of the queue
  12.     // overwrite the oldest elements
  13.     // when the queue is full
  14.     virtual T poll() = 0; // remove an element from the front of the queue
  15.     virtual T peek() = 0; // look at the element at the front of the queue
  16.     // (without removing it)
  17.     virtual void flush() = 0; // remove all elements from the queue
  18.     virtual bool isEmpty() = 0; // is the queue empty?
  19.     virtual bool isFull() = 0; // is the queue full?
  20.     virtual int size() = 0; // number of elements
  21.     virtual int capacity() = 0; // maximum capacity
  22. };
  23.  
  24. template<class T>
  25. class ArrayCircularBoundedQueue : public virtual ICircularBoundedQueue<T>
  26. {
  27. private:
  28.     const size_t CAPACITY;
  29.     T* arr_;
  30.     T default_value_;
  31.     int index_front_;
  32.     int index_rear_;
  33.  
  34. public:
  35.     explicit ArrayCircularBoundedQueue(size_t capacity, T default_value) :CAPACITY(capacity), arr_(new T[CAPACITY]), default_value_(default_value), index_front_(-1),
  36.         index_rear_(-1)
  37.     {}
  38.  
  39.     void offer(T value) override
  40.     {
  41.         if (index_front_ == -1 && index_rear_ == -1)
  42.         {
  43.             arr_[0] = value;
  44.             index_front_ = 0;
  45.             index_rear_ = 0;
  46.         }
  47.         else
  48.         {
  49.             index_rear_ = (index_rear_ + 1) % CAPACITY;
  50.  
  51.             if (index_rear_ == index_front_)
  52.                 index_front_ = (index_front_ + 1) % CAPACITY;
  53.  
  54.             arr_[index_rear_] = value;
  55.         }
  56.     }
  57.     T poll() override
  58.     {
  59.         if (index_front_ == -1 && index_rear_ == -1)
  60.             return default_value_;
  61.  
  62.         T val = arr_[index_front_];
  63.  
  64.         if (index_front_ == index_rear_)
  65.         {
  66.             index_front_ = -1;
  67.             index_rear_ = -1;
  68.         }
  69.         else index_front_ = (index_front_ + 1) % CAPACITY;
  70.  
  71.         return val;
  72.     }
  73.     T peek() override
  74.     {
  75.         if (index_front_ == -1 && index_rear_ == -1)
  76.             return default_value_;
  77.  
  78.         return arr_[index_front_];
  79.     }
  80.     void flush() override
  81.     {
  82.         index_front_ = -1;
  83.         index_rear_ = -1;
  84.     }
  85.     bool isEmpty() override
  86.     {
  87.         return (index_front_ == -1 && index_rear_ == -1);
  88.     }
  89.     bool isFull() override
  90.     {
  91.         return abs(index_front_ - index_rear_) == (CAPACITY - 1);
  92.     }
  93.     int size() override
  94.     {
  95.         if (index_front_ == -1 && index_rear_ == -1)
  96.             return 0;
  97.  
  98.         if (index_front_ <= index_rear_)
  99.             return index_rear_ - index_front_ + 1;
  100.  
  101.         return CAPACITY - index_front_ + index_rear_ + 1;
  102.  
  103.     }
  104.     int capacity() override
  105.     {
  106.         return CAPACITY;
  107.     }
  108.  
  109.     ArrayCircularBoundedQueue& operator=(ArrayCircularBoundedQueue queue) noexcept
  110.     {
  111.         if (this == &queue)
  112.             return *this;
  113.  
  114.         copy(queue.arr_, queue.arr_ + queue.capacity(), this->arr_);
  115.         this->index_front_ = queue.index_front_;
  116.         this->index_rear_ = queue.index_rear_;
  117.         this->default_value_ = queue.default_value_;
  118.  
  119.         return *this;
  120.     }
  121. };
  122.  
  123. template<class T>
  124. class IBoundedStack
  125. {
  126. public:
  127.     virtual void push(T value) = 0; // push an element onto the stack
  128.     // remove the oldest element
  129.         // when if stack is full
  130.     virtual T pop() = 0; // remove an element from the top of the stack
  131.     virtual T top() = 0; // look at the element at the top of the stack
  132.     // (without removing it)
  133.     virtual void flush() = 0; // remove all elements from the stack
  134.     virtual bool isEmpty() = 0; // is the stack empty?
  135.     virtual bool isFull() = 0; // is the stack full?
  136.     virtual int size() = 0; // number of elements
  137.     virtual int capacity() = 0; // maximum capacity
  138. };
  139.  
  140. template<class T>
  141. class BoundedStack :public IBoundedStack<T>
  142. {
  143. private:
  144.     const size_t CAPACITY;
  145.     const T DEFAULT_VALUE;
  146.     ArrayCircularBoundedQueue<T> main_queue_;
  147.     ArrayCircularBoundedQueue<T> duplicate_queue_;
  148.  
  149. public:
  150.     BoundedStack(size_t capacity, T default_value) :CAPACITY(capacity), DEFAULT_VALUE(default_value), main_queue_(CAPACITY, default_value), duplicate_queue_(CAPACITY, default_value)
  151.     {}
  152.  
  153.     void push(T value) override
  154.     {
  155.         main_queue_.offer(value);
  156.     }
  157.     T pop() override
  158.     {
  159.         if (main_queue_.isEmpty())
  160.             return DEFAULT_VALUE;
  161.  
  162.         while (main_queue_.size() > 1)
  163.         {
  164.             duplicate_queue_.offer(main_queue_.poll());
  165.         }
  166.  
  167.         T returnValue = main_queue_.poll();
  168.  
  169.         main_queue_ = duplicate_queue_;
  170.         duplicate_queue_.flush();
  171.  
  172.         return returnValue;
  173.     }
  174.     T top() override
  175.     {
  176.         T returnValue = pop();
  177.         main_queue_.offer(returnValue);
  178.         return returnValue;
  179.     }
  180.     void flush() override
  181.     {
  182.         main_queue_.flush();
  183.     }
  184.     bool isEmpty() override
  185.     {
  186.         return main_queue_.isEmpty();
  187.     }
  188.     bool isFull() override
  189.     {
  190.         return main_queue_.isFull();
  191.     }
  192.     int size() override
  193.     {
  194.         return main_queue_.size();
  195.     }
  196.     int capacity() override
  197.     {
  198.         return CAPACITY;
  199.     }
  200. };
  201.  
  202. template<class T>
  203. class ISet {
  204. public:
  205.     virtual void add(T item) = 0; // add item in the set
  206.     virtual void remove(T item) = 0; // remove an item from a set
  207.     virtual bool contains(T item) = 0; // check if a item belongs to a set
  208.     virtual int size() = 0; // number of elements in a set
  209.     virtual bool isEmpty() = 0; // check if the set is empty
  210. };
  211.  
  212. template<class T>
  213. class EntrySet
  214. {
  215. public:
  216.     T data;
  217.     bool existence;
  218.  
  219.     EntrySet() : existence(false)
  220.     {}
  221.  
  222.     EntrySet(T data) : existence(true)
  223.     {
  224.         this->data = data;
  225.     }
  226.  
  227.     EntrySet& operator=(const EntrySet& entry) noexcept
  228.     {
  229.         if (this == &entry)
  230.             return *this;
  231.  
  232.         this->data = entry.data;
  233.         this->existence = entry.existence;
  234.  
  235.         return *this;
  236.     }
  237.  
  238.     bool operator==(const EntrySet& entry)
  239.     {
  240.         if (existence == false && entry.existence == false)
  241.             return true;
  242.  
  243.         return (existence == entry.existence) && (data == entry.data);
  244.     }
  245.  
  246.     bool operator!=(const EntrySet& entry)
  247.     {
  248.         if (existence == false && entry.existence == false)
  249.             return false;
  250.  
  251.         return (existence != entry.existence) || data != entry.data;
  252.     }
  253.  
  254.     bool operator==(const int& data)
  255.     {
  256.         return existence && (this->data == data);
  257.     }
  258. };
  259.  
  260. template<class T>
  261. class DoubleHashSet : public virtual ISet<T>
  262. {
  263. private:
  264.     EntrySet<T>* arr_;
  265.     int size_{};
  266.     const int CAPACITY = 379;
  267.     const int prime_num = 37;
  268.     /*const int CAPACITY = 2027;
  269.     const int prime_num = 103;*/
  270.     //1993
  271.     const EntrySet<T> DEFAULT_ENTRY;
  272.  
  273.     int hash_func_1(T item)
  274.     {
  275.         auto hash_ = hash<T>();
  276.         int ha = hash_(item);
  277.         return abs(ha);
  278.     }
  279.     int hash_func_2(T item, int hash1)
  280.     {
  281.         return (prime_num - abs(hash1) % prime_num);
  282.     }
  283.  
  284.     bool findIndex(T item, int* index = nullptr)
  285.     {
  286.         int hash1 = hash_func_1(item);
  287.         int hash2 = hash_func_2(item, hash1);
  288.         int hash_code;
  289.         for (int j = 1; j < CAPACITY; j++)
  290.         {
  291.             hash_code = (hash1 + j * hash2) % CAPACITY;
  292.             if (arr_[hash_code] != DEFAULT_ENTRY && arr_[hash_code] == item)
  293.             {
  294.                 if (index != nullptr)
  295.                     *index = hash_code;
  296.                 return true;
  297.             }
  298.         }
  299.         return false;
  300.     }
  301. public:
  302.     DoubleHashSet() :size_(0), DEFAULT_ENTRY(EntrySet<T>())
  303.     {
  304.         arr_ = new EntrySet<T>[CAPACITY];
  305.     }
  306.  
  307.     DoubleHashSet(const DoubleHashSet& set)
  308.     {
  309.         this->size_ = set.size_;
  310.         this->arr_ = new EntrySet<T>[CAPACITY];
  311.         std::copy(set.arr_, set.arr_ + set.CAPACITY, this->arr_);
  312.     }
  313.  
  314.     ~DoubleHashSet()
  315.     {
  316.         delete[] arr_;
  317.     }
  318.  
  319.     void add(T item) override
  320.     {
  321.         int index;
  322.  
  323.         if (size() == CAPACITY)
  324.             throw overflow_error("Set is full");
  325.  
  326.         int hash1 = hash_func_1(item);
  327.         int hash2 = hash_func_2(item, hash1);
  328.         int hash_code;
  329.         for (int j = 1; j < CAPACITY; j++)
  330.         {
  331.             hash_code = (hash1 + j * hash2) % CAPACITY;
  332.             if (arr_[hash_code] == DEFAULT_ENTRY)
  333.             {
  334.                 size_++;
  335.                 arr_[hash_code] = EntrySet<T>(item);
  336.                 return;
  337.             }
  338.         }
  339.     }
  340.     void remove(T item) override
  341.     {
  342.         int hash1 = hash_func_1(item);
  343.         int hash2 = hash_func_2(item, hash1);
  344.         int hash_code;
  345.  
  346.         for (int j = 1; j < CAPACITY; j++)
  347.         {
  348.             hash_code = (hash1 + j * hash2) % CAPACITY;
  349.             if (arr_[hash_code] == item)
  350.             {
  351.                 size_--;
  352.                 arr_[hash_code] = DEFAULT_ENTRY;
  353.                 return;
  354.             }
  355.         }
  356.     }
  357.     bool contains(T item) override
  358.     {
  359.         return findIndex(item);
  360.     }
  361.     int size() override
  362.     {
  363.         return size_;
  364.     }
  365.     bool isEmpty() override
  366.     {
  367.         return size_ == 0;
  368.     }
  369.  
  370.     operator string() const
  371.     {
  372.         ostringstream ans;
  373.         for (int i = 0; i < CAPACITY; i++)
  374.             if (arr_[i].existence != false)
  375.                 ans << arr_[i].data << " ";
  376.         return ans.str();
  377.     }
  378.  
  379.     DoubleHashSet& operator= (const DoubleHashSet& set)
  380.     {
  381.         if (this == &set)
  382.             return *this;
  383.  
  384.         delete[] arr_;
  385.  
  386.         this->size_ = set.size_;
  387.         this->arr_ = new EntrySet<T>[CAPACITY];
  388.         std::copy(set.arr_, set.arr_ + set.CAPACITY, this->arr_);
  389.  
  390.         return *this;
  391.     }
  392. };
  393.  
  394. void parse_line(string& line, string& command, string& parameter)
  395. {
  396.     const size_t space_index = line.find(' ');
  397.  
  398.     if (space_index != string::npos)
  399.     {
  400.         command = line.substr(0, space_index);
  401.         parameter = line.substr(space_index + 1);
  402.     }
  403.     else
  404.     {
  405.         command = line.substr(0);
  406.         parameter = "";
  407.     }
  408. }
  409.  
  410. int main()
  411. {
  412.     int n, k;
  413.     cin >> n >> k;
  414.  
  415.     DoubleHashSet<string> Set;
  416.     BoundedStack<DoubleHashSet<string>> StatusSet(k, DoubleHashSet<string>());
  417.     bool flag = true;
  418.  
  419.     string line, command, parameter;
  420.     getline(cin, line);
  421.  
  422.     for (int i = 0; i < n; i++)
  423.     {
  424.         getline(cin, line);
  425.  
  426.         parse_line(line, command, parameter);
  427.  
  428.         if (command == "NEW")
  429.         {
  430.             string second_name = parameter;
  431.  
  432.             if (second_name[second_name.size() - 1] == '/')
  433.                 second_name = second_name.substr(0, second_name.size() - 1);
  434.             else second_name = second_name.append("/");
  435.  
  436.             if (!(Set.contains(second_name) || Set.contains(parameter)))
  437.             {
  438.                 Set.add(parameter);
  439.                 StatusSet.push(Set);
  440.             }
  441.             else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  442.         }
  443.  
  444.         if (command == "REMOVE")
  445.         {
  446.             if (Set.contains(parameter))
  447.             {
  448.                 Set.remove(parameter);
  449.                 StatusSet.push(Set);
  450.             }
  451.             else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  452.         }
  453.  
  454.         if (command == "UNDO")
  455.         {
  456.             int undo_parameter = 1;
  457.             if (!parameter.empty())
  458.                 undo_parameter = stoi(parameter, nullptr, 10);
  459.  
  460.             if ((!flag && (undo_parameter == StatusSet.size())) || (undo_parameter >= k) || undo_parameter > StatusSet.size())
  461.             {
  462.                 cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  463.             }
  464.             else
  465.             {
  466.                 while (undo_parameter--)
  467.                 {
  468.                     StatusSet.pop();
  469.                 }
  470.                 Set = StatusSet.top();
  471.             }
  472.         }
  473.  
  474.         if (command == "LIST")
  475.         {
  476.             cout << static_cast<string>(Set) << endl;
  477.         }
  478.  
  479.         if (flag)
  480.             flag = !StatusSet.isFull();
  481.     }
  482.  
  483. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement