Advertisement
habur331

Untitled

Feb 20th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.73 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 = 739;
  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. //
  333. //
  334. // DoubleHashSet& operator= (const DoubleHashSet& set)
  335. // {
  336. // if (this == &set)
  337. // return *this;
  338. //
  339. // delete[] arr_;
  340. // delete[]arr_exist_;
  341. //
  342. // this->size_ = set.size_;
  343. // this->arr_ = new T[CAPACITY];
  344. // this->arr_exist_ = new bool[CAPACITY];
  345. //
  346. // std::copy(set.arr_, set.arr_ + set.CAPACITY, this->arr_);
  347. // std::copy(set.arr_exist_, set.arr_exist_ + set.CAPACITY, this->arr_exist_);
  348. //
  349. // return *this;
  350. // }
  351. //};
  352.  
  353. template <typename T>
  354. class DoubleHashSet : ISet<T>
  355. {
  356. private:
  357. // pointer to array of T pointers
  358. T** array;
  359. int sizeS;
  360. int CAPACITY = 797;
  361. int PRIME_NUMBER = 37;
  362.  
  363. public:
  364. DoubleHashSet()
  365. {
  366. array = new T * [CAPACITY];
  367. for (int i = 0; i < CAPACITY; i++)
  368. array[i] = nullptr;
  369. this->sizeS = 0;
  370. }
  371.  
  372. DoubleHashSet(const DoubleHashSet& copy)
  373. {
  374. this->sizeS = copy.sizeS;
  375. this->array = new T * [CAPACITY];
  376. std::copy(copy.array, copy.array + copy.CAPACITY, this->array);
  377. }
  378.  
  379. DoubleHashSet& operator= (const DoubleHashSet& set)
  380. {
  381. if (this == &set)
  382. return *this;
  383.  
  384. delete[] this->array;
  385.  
  386. this->sizeS = set.sizeS;
  387. this->array = new T * [CAPACITY];
  388. std::copy(set.array, set.array + set.CAPACITY, this->array);
  389.  
  390. return *this;
  391. }
  392.  
  393. ~DoubleHashSet()
  394. {
  395. delete[] array;
  396. }
  397.  
  398. void add(T item)
  399. {
  400. if (!contains(item))
  401. {
  402. int i = 0, h1, h2;
  403. h1 = hashCode1(item);
  404. h2 = hashCode2(item, h1);
  405. while (array[(h1 + i * h2) % CAPACITY] != nullptr && i < CAPACITY)
  406. i++;
  407.  
  408. int index = (h1 + i * h2) % CAPACITY;
  409. array[index] = new T;
  410. array[index][0] = item;
  411. sizeS++;
  412. }
  413. }
  414.  
  415. /**
  416. * \remove element in array[index] if it exists
  417. }
  418. * \param element
  419. */
  420.  
  421. virtual void remove(T item)
  422. {
  423. int index = 0;
  424. if (contains(item, index))
  425. {
  426. array[index] = nullptr;
  427. sizeS--;
  428. }
  429. }
  430.  
  431. virtual bool contains(T item)
  432. {
  433. int i = 0, h1, h2;
  434. h1 = hashCode1(item);
  435. h2 = hashCode2(item, h1);
  436. while (i < CAPACITY)
  437. {
  438. int index = (h1 + i * h2) % CAPACITY;
  439.  
  440. if (array[index] != nullptr)
  441. if (array[index][0] == item)
  442. return true;
  443. i++;
  444. }
  445. return false;
  446. }
  447.  
  448. int size()
  449. {
  450. return this->sizeS;
  451. }
  452.  
  453. bool isEmpty()
  454. {
  455. return (this->sizeS == 0);
  456. }
  457. /**
  458. * \print all element that DoubleHashSet contains
  459. * \return line with all elements
  460. */
  461. string ToString()
  462. {
  463. ostringstream answer;
  464. for (int i = 0; i < CAPACITY - 1; i++)
  465. if (array[i] != nullptr)
  466. answer << array[i][0] << " ";
  467.  
  468. if (array[CAPACITY - 1] != nullptr)
  469. answer << array[CAPACITY - 1][0];
  470. return answer.str();
  471.  
  472. }
  473.  
  474. operator string() const
  475. {
  476. ostringstream ans;
  477. for (int i = 0; i < CAPACITY; i++)
  478. if (array[i] != nullptr)
  479. ans << array[i][0] << " ";
  480. return ans.str();
  481. }
  482.  
  483. private:
  484.  
  485. int hashCode1(T key) {
  486.  
  487. auto hashfunc = hash<T>();
  488.  
  489. int hashcode = hashfunc(key);
  490.  
  491. return abs(hashcode) % CAPACITY;
  492. }
  493.  
  494. int hashCode2(T key, int hash1) {
  495.  
  496. return (PRIME_NUMBER - (abs(hash1) % PRIME_NUMBER));
  497. }
  498.  
  499. virtual bool contains(T item, int& out_index)
  500. {
  501. int i = 0, h1, h2;
  502. h1 = hashCode1(item);
  503. h2 = hashCode2(item, h1);
  504. while (i < CAPACITY)
  505. {
  506. int index = (h1 + i * h2) % CAPACITY;
  507.  
  508. if (array[index] != nullptr)
  509. if (array[index][0] == item)
  510. {
  511. out_index = index;
  512. return true;
  513. }
  514. i++;
  515. }
  516.  
  517. return false;
  518. }
  519. };
  520.  
  521. void parse_line(string& line, string& command, string& parameter)
  522. {
  523. const size_t space_index = line.find(' ');
  524.  
  525. if (space_index != string::npos)
  526. {
  527. command = line.substr(0, space_index);
  528. parameter = line.substr(space_index + 1);
  529. }
  530. else
  531. {
  532. command = line.substr(0);
  533. parameter = "";
  534. }
  535. }
  536.  
  537. int main()
  538. {
  539. int n, k;
  540. cin >> n >> k;
  541.  
  542. DoubleHashSet<string> Set;
  543. BoundedStack<DoubleHashSet<string>> StatusSet(k, DoubleHashSet<string>());
  544. bool flag = true;
  545.  
  546. string line, command, parameter;
  547. getline(cin, line);
  548.  
  549. for (int i = 0; i < n; i++)
  550. {
  551. getline(cin, line);
  552.  
  553. parse_line(line, command, parameter);
  554.  
  555. if (command == "NEW")
  556. {
  557. string second_name = parameter;
  558.  
  559. if (second_name[second_name.size() - 1] == '/')
  560. second_name = second_name.substr(0, second_name.size() - 1);
  561. else second_name = second_name.append("/");
  562.  
  563. if (!(Set.contains(second_name) || Set.contains(parameter)))
  564. {
  565. Set.add(parameter);
  566. StatusSet.push(Set);
  567. }
  568. else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  569. }
  570.  
  571. if (command == "REMOVE")
  572. {
  573. if (Set.contains(parameter))
  574. {
  575. Set.remove(parameter);
  576. StatusSet.push(Set);
  577. }
  578. else cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  579. }
  580.  
  581. if (command == "UNDO")
  582. {
  583. int undo_parameter = 1;
  584. if (!parameter.empty())
  585. undo_parameter = stoi(parameter, nullptr, 10);
  586.  
  587. if ((!flag && (undo_parameter == StatusSet.size())) || (undo_parameter >= k) || undo_parameter > StatusSet.size())
  588. {
  589. cout << "ERROR: cannot execute " << command.append(" ").append(parameter).append("\n");
  590. }
  591. else
  592. {
  593. for (int i = 0; i < undo_parameter; i++)
  594. {
  595. StatusSet.pop();
  596. }
  597. Set = StatusSet.top();
  598. }
  599. }
  600.  
  601. if (command == "LIST")
  602. {
  603. cout << static_cast<string>(Set) << endl;
  604. }
  605.  
  606. if (flag)
  607. flag = !StatusSet.isFull();
  608. }
  609.  
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement