Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <set>
  6. #include <math.h>
  7. #include <list>
  8.  
  9. using namespace std;
  10. typedef long long int lli;
  11.  
  12. class Set {
  13. private:
  14. const lli PRIME_SIZE = 1000001;
  15. vector<list<lli>>* hashTable;
  16.  
  17. int hash(lli num) const {
  18. return abs(num % PRIME_SIZE);
  19. }
  20. public:
  21. Set() {
  22. this->hashTable = new vector<list<lli>>(PRIME_SIZE);
  23. }
  24.  
  25. void insert(lli num) {
  26. lli index = hash(num);
  27. for (auto i : hashTable->at(index))
  28. if (i == num)
  29. return;
  30. hashTable->at(index).push_back(num);
  31. }
  32.  
  33. void erase(lli num) {
  34. lli index = hash(num);
  35. for (auto iter = hashTable->at(index).begin(); iter != hashTable->at(index).end(); iter++) {
  36. if (*iter == num) {
  37. hashTable->at(index).erase(iter);
  38. return;
  39. }
  40. }
  41. }
  42.  
  43. bool find(lli num) const {
  44. lli index = hash(num);
  45. for (auto iter = hashTable->at(index).begin(); iter != hashTable->at(index).end(); iter++)
  46. if (*iter == num)
  47. return true;
  48. return false;
  49. }
  50. };
  51.  
  52.  
  53. int main() {
  54. ifstream ist("set.in");
  55. ofstream ost("set.out");
  56.  
  57. Set newSet;
  58. string com;
  59. lli num;
  60. while (ist >> com >> num) {
  61. if (com == "insert")
  62. newSet.insert(num);
  63. else if (com == "delete")
  64. newSet.erase(num);
  65. else
  66. ost << (newSet.find(num) ? "true" : "false") << endl;
  67. }
  68.  
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement