Advertisement
Guest User

Untitled

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