Advertisement
habur331

Untitled

Feb 20th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 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 DoubleHashSet : public virtual ISet<T>
  214. {
  215. private:
  216. T* arr_;
  217. bool* arr_exist_;
  218. int size_{ 0 };
  219. const int CAPACITY = 1003;
  220. const int prime_num = 37;
  221.  
  222. void get_hashes(const T& item, long long& hash1, long long& hash2)
  223. {
  224. auto hash_ = hash<T>();
  225.  
  226. const string string_item = (string) item;
  227.  
  228. hash1 = abs((int)(hash_(item))) % CAPACITY;
  229. hash2 = abs(prime_num - hash1 % prime_num) % (CAPACITY - 2);
  230. }
  231.  
  232. bool findIndex(T item, int* index = nullptr)
  233. {
  234. long long hash1;
  235. long long hash2;;
  236. int hash_code;
  237.  
  238. get_hashes(item, hash1, hash2);
  239.  
  240. for (int j = 0; j < CAPACITY; j++)
  241. {
  242. hash_code = (hash1 + j * hash2) % CAPACITY;
  243. if (arr_exist_[hash_code] != false && arr_[hash_code] == item)
  244. {
  245. if (index != nullptr)
  246. *index = hash_code;
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. public:
  253. DoubleHashSet()
  254. {
  255. arr_ = new T[CAPACITY];
  256. arr_exist_ = new bool[CAPACITY];
  257.  
  258. for (int i = 0; i < CAPACITY; i++)
  259. arr_exist_[i] = false;
  260. }
  261.  
  262. DoubleHashSet(const DoubleHashSet& set)
  263. {
  264. this->size_ = set.size_;
  265. this->arr_ = new T[CAPACITY];
  266. this->arr_exist_ = new bool[CAPACITY];
  267. std::copy(set.arr_, set.arr_ + set.CAPACITY, this->arr_);
  268. std::copy(set.arr_exist_, set.arr_exist_ + set.CAPACITY, this->arr_exist_);
  269. }
  270.  
  271. ~DoubleHashSet()
  272. {
  273. delete[] arr_;
  274. delete[] arr_exist_;
  275. }
  276.  
  277. void add(T item) override
  278. {
  279. if (size() == CAPACITY)
  280. throw overflow_error("Set is full");
  281.  
  282. long long hash1;
  283. long long hash2;
  284. int hash_code;
  285.  
  286. get_hashes(item, hash1, hash2);
  287.  
  288. for (int j = 1; j < CAPACITY; j++)
  289. {
  290. hash_code = (hash1 + j * hash2) % CAPACITY;
  291. if (arr_exist_[hash_code] == false)
  292. {
  293. size_++;
  294. arr_[hash_code] = item;
  295. arr_exist_[hash_code] = true;
  296. return;
  297. }
  298. }
  299. }
  300. void remove(T item) override
  301. {
  302. long long hash1;
  303. long long hash2;
  304. int hash_code;
  305.  
  306. get_hashes(item, hash1, hash2);
  307.  
  308. for (int j = 1; j < CAPACITY; j++)
  309. {
  310. hash_code = (hash1 + j * hash2) % CAPACITY;
  311. if (arr_[hash_code] == item)
  312. {
  313. size_--;
  314. arr_exist_[hash_code] = false;
  315. return;
  316. }
  317. }
  318. }
  319. bool contains(T item) override
  320. {
  321. return findIndex(item);
  322. }
  323. int size() override
  324. {
  325. return size_;
  326. }
  327. bool isEmpty() override
  328. {
  329. return size_ == 0;
  330. }
  331.  
  332. operator string() const
  333. {
  334. ostringstream ans;
  335. for (int i = 0; i < CAPACITY; i++)
  336. if (arr_exist_[i] != false)
  337. ans << arr_[i] << " ";
  338. return ans.str();
  339. }
  340.  
  341. DoubleHashSet& operator= (const DoubleHashSet& set)
  342. {
  343. if (this == &set)
  344. return *this;
  345.  
  346. delete[] arr_;
  347. delete[]arr_exist_;
  348.  
  349. this->size_ = set.size_;
  350. this->arr_ = new T[CAPACITY];
  351. this->arr_exist_ = new bool[CAPACITY];
  352.  
  353. std::copy(set.arr_, set.arr_ + set.CAPACITY, this->arr_);
  354. std::copy(set.arr_exist_, set.arr_exist_ + set.CAPACITY, this->arr_exist_);
  355.  
  356. return *this;
  357. }
  358. };
  359.  
  360. void parse_line(string& line, string& command, string& parameter)
  361. {
  362. const size_t space_index = line.find(' ');
  363.  
  364. if (space_index != string::npos)
  365. {
  366. command = line.substr(0, space_index);
  367. parameter = line.substr(space_index + 1);
  368. }
  369. else
  370. {
  371. command = line.substr(0);
  372. parameter = "";
  373. }
  374. }
  375.  
  376. int main()
  377. {
  378. int n, k;
  379. cin >> n >> k;
  380.  
  381. DoubleHashSet<string> Set;
  382. BoundedStack<DoubleHashSet<string>> StatusSet(k, DoubleHashSet<string>());
  383. bool flag = true;
  384.  
  385. string line, command, parameter;
  386. getline(cin, line);
  387.  
  388. for (int i = 0; i < n; i++)
  389. {
  390. getline(cin, line);
  391.  
  392. parse_line(line, command, parameter);
  393.  
  394. if (command == "NEW")
  395. {
  396. string second_name = parameter;
  397.  
  398. if (second_name[second_name.size() - 1] == '/')
  399. second_name = second_name.substr(0, second_name.size() - 1);
  400. else second_name = second_name.append("/");
  401.  
  402. if (!(Set.contains(second_name) || Set.contains(parameter)))
  403. {
  404. Set.add(parameter);
  405. StatusSet.push(Set);
  406. }
  407. else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  408. }
  409.  
  410. if (command == "REMOVE")
  411. {
  412. if (Set.contains(parameter))
  413. {
  414. Set.remove(parameter);
  415. StatusSet.push(Set);
  416. }
  417. else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  418. }
  419.  
  420. if (command == "UNDO")
  421. {
  422. int undo_parameter = 1;
  423. if (!parameter.empty())
  424. undo_parameter = stoi(parameter, nullptr, 10);
  425.  
  426. if ((!flag && (undo_parameter == StatusSet.size())) || (undo_parameter >= k) || undo_parameter > StatusSet.size())
  427. {
  428. cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  429. }
  430. else
  431. {
  432. for (int i = 0; i < undo_parameter; i++)
  433. {
  434. StatusSet.pop();
  435. }
  436. Set = StatusSet.top();
  437. }
  438. }
  439.  
  440. if (command == "LIST")
  441. {
  442. cout << static_cast<string>(Set) << endl;
  443. }
  444.  
  445. if (flag)
  446. flag = !StatusSet.isFull();
  447. }
  448.  
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement