Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. std::ifstream input("set.in", std::ios_base::in);
  9. std::ofstream output("set.out", std::ios_base::out | std::ios_base::trunc);
  10.  
  11. struct HashTable {
  12.     HashTable() {
  13.         hastTable.assign(SIZE + 3, vector<int>(0)); // 3 про запас, лень думать
  14.     }
  15.  
  16.     int getHash(int value) {
  17.         long a = value * 113 + 145861;
  18.         a %= 6130667;
  19.         a %= SIZE;
  20.         return abs(a);
  21.     }
  22.  
  23.     void insert(int value) {
  24.         if (!contains(value)) {
  25.             int hash = getHash(value);
  26.             hastTable[hash].push_back(value);
  27.         }
  28.     }
  29.  
  30.     bool contains(int value) {
  31.         int hash = getHash(value);
  32.         if (!hastTable[hash].empty()) {
  33.             for (int i : hastTable[hash]) {
  34.                 if (i == value) {
  35.                     return true;
  36.                 }
  37.             }
  38.         }
  39.         return false;
  40.     }
  41.  
  42.     void remove(int value) {
  43.         if (contains(value)) {
  44.             int hash = getHash(value);
  45.             for (int &i : hastTable[hash]) {
  46.                 if (i == value) {
  47.                     i = 1000001;
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     const int SIZE = 987911;
  54.     vector<vector<int>> hastTable;
  55. };
  56.  
  57. int main() {
  58.     string command;
  59.     int value;
  60.     HashTable hashtable;
  61.     while (input >> command) {
  62.         input >> value;
  63.         if (command == "insert") {
  64.             hashtable.insert(value);
  65.         } else if (command == "exists") {
  66.             output << (hashtable.contains(value) ? "true" : "false") << "\n";
  67.         } else if (command == "delete") {
  68.             hashtable.remove(value);
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement