Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int MAX = 1e6 + 1;
  6.  
  7. struct HashSet
  8. {
  9. vector <int> v[MAX];
  10. HashSet()
  11. {
  12. for (int i = 0; i < v->size(); ++i)
  13. v[i] = {};
  14. }
  15. int HashFunc(int x)
  16. {
  17. return (abs(x) % MAX);
  18. }
  19. bool exists(int x)
  20. {
  21. int hash = this->HashFunc(x);
  22. for (auto i : (this->v[hash]))
  23. if (i == x)
  24. return 1;
  25. return 0;
  26. }
  27. void insert(int x)
  28. {
  29. if (this->exists(x))
  30. return;
  31. v[HashFunc(x)].push_back(x);
  32. }
  33. void erase(int x)
  34. {
  35. if (!this->exists(x))
  36. return;
  37. int k = 0;
  38. for (int i = 0; i < v[HashFunc(x)].size(); ++i)
  39. if (v[HashFunc(x)][i] == x)
  40. k = i;
  41. swap(v[HashFunc(x)][k], v[HashFunc(x)][v[HashFunc(x)].size() - 1]);
  42. v[HashFunc(x)].pop_back();
  43. }
  44.  
  45. };
  46.  
  47.  
  48. int main()
  49. {
  50. freopen("set.in", "r", stdin);
  51. freopen("set.out", "w", stdout);
  52. string str;
  53. int x;
  54. auto h = new HashSet();
  55. while (cin >> str)
  56. {
  57. cin >> x;
  58. if (str == "insert")
  59. h->insert(x);
  60. else if (str == "delete")
  61. h->erase(x);
  62. else if (str == "exists")
  63. {
  64. if (h->exists(x))
  65. cout << "true" << '\n';
  66. else
  67. cout << "false" << '\n';
  68. }
  69. }
  70.  
  71.  
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement