Alex_tz307

Hash

Sep 12th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #define MOD
  2. vector < pair < int , int > > Hash[MOD];
  3.  
  4. void Insert (int x) {
  5.   int key = x % MOD;
  6.   for (size_t it = 0; it < Hash[key].size(); ++it)
  7.     if (Hash[key][it].first == x) {
  8.       Hash[key][it].second ++;
  9.       return;
  10.     }
  11.   Hash[key].push_back({x, 1});
  12. }
  13.  
  14. void Erase (int x) {
  15.   int key = x % MOD;
  16.   vector < pair < int , int > > :: iterator it;
  17.   for (it = Hash[key].begin(); it != Hash[key].end(); ++it)
  18.     if (it -> first == x) {
  19.       it -> second --;
  20.       if (it -> second == 0)
  21.         Hash[key].erase (it);
  22.       return;
  23.     }
  24. }
  25.  
  26. int Search (int x) {
  27.   int key = x % MOD;
  28.   for (auto it : Hash[key])
  29.     if (it.first == x)
  30.       return it.second;
  31.   return 0;
  32. }
Add Comment
Please, Sign In to add comment