Advertisement
NHumme

5D

Nov 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "multimap"
  14.  
  15. const int sz1 = 500003, sz2 = 289, tt = 211;
  16.  
  17. struct hshmap {
  18.     string s;
  19.     int sz;
  20.     pair < string, bool > hm[sz2];
  21. };
  22.  
  23. vector < hshmap > hash_map[sz1];
  24. string key, zn, a;
  25.  
  26. int hash_f1(string s) {
  27.     int p = 449;
  28.     int hs = 0;
  29.     for (int i = 0; i < s.size(); ++i) {
  30.         hs = hs * p + s[i];
  31.         if (hs >= sz1) {
  32.             hs %= sz1;
  33.         }
  34.     }
  35.     return hs;
  36. }
  37.  
  38. int hash_f2(string s) {
  39.     int p = 457;
  40.     int hs = 0;
  41.     for (int i = 0; i < s.size(); ++i) {
  42.         hs = hs * p + s[i];
  43.         if (hs >= sz2) {
  44.             hs %= sz2;
  45.         }
  46.     }
  47.     return hs;
  48. }
  49.  
  50. int hash_f(string s) {
  51.     int p = 487;
  52.     unsigned long long hs = 0;
  53.     for (int i = 0; i < s.size(); ++i) {
  54.         hs = hs * p + s[i];
  55.     }
  56.     return hs;
  57. }
  58.  
  59. void get() {
  60.     int hk = hash_f1(key);
  61.     unsigned long long hk2 = hash_f(key);
  62.     for (int i = 0; i < hash_map[hk].size(); ++i) {
  63.         if (hash_f(hash_map[hk][i].s) == hk2) {
  64.             cout << hash_map[hk][i].sz << " ";
  65.             for (int j = 0; j < sz2; ++j) {
  66.                 if (hash_map[hk][i].hm[j].second) {
  67.                     cout << hash_map[hk][i].hm[j].first << " ";
  68.                 }
  69.             }
  70.             cout << "\n";
  71.             return;
  72.         }
  73.     }
  74.     cout << "0 \n";
  75. }
  76.  
  77. void put() {
  78.     int hk = hash_f1(key), hs = hash_f2(zn), t = -1, j;
  79.     unsigned long long hk2 = hash_f(key), hs2 = hash_f(zn);
  80.     j = hs;
  81.     for (int i = 0; i < hash_map[hk].size(); ++i) {
  82.         if (hash_f(hash_map[hk][i].s) == hk2) {
  83.             do {
  84.                 if (hash_f(hash_map[hk][i].hm[j].first) == hs2) {
  85.                     if (!hash_map[hk][i].hm[j].second) {
  86.                         hash_map[hk][i].hm[j].second = true;
  87.                         hash_map[hk][i].sz++;
  88.                     }
  89.                     return;
  90.                 }
  91.                 if (!hash_map[hk][i].hm[j].second) {
  92.                     t = j;
  93.                 }
  94.                 j = (j + tt) % sz2;
  95.             } while (j != hs);
  96.             hash_map[hk][i].hm[t] = make_pair(zn, true);
  97.             hash_map[hk][i].sz++;
  98.             return;
  99.         }
  100.     }
  101.     hshmap a;
  102.     a.s = key;
  103.     a.sz = 1;
  104.     a.hm[hs] = make_pair(zn, true);
  105.     hash_map[hk].push_back(a);
  106. }
  107.  
  108. void del() {
  109.     int hk = hash_f1(key), hs = hash_f2(zn), j;
  110.     unsigned long long hk2 = hash_f(key), hs2 = hash_f(zn);
  111.     j = hs;
  112.     for (int i = 0; i < hash_map[hk].size(); ++i) {
  113.         if (hash_f(hash_map[hk][i].s) == hk2) {
  114.             do {
  115.                 if (hash_f(hash_map[hk][i].hm[j].first) == hs2 && hash_map[hk][i].hm[j].second) {
  116.                     hash_map[hk][i].hm[j].second = false;
  117.                     hash_map[hk][i].sz--;
  118.                 }
  119.                 j = (j + tt) % sz2;
  120.             } while (j != hs);
  121.             return;
  122.         }
  123.     }
  124. }
  125.  
  126. void delall() {
  127.     int hk = hash_f1(key);
  128.     unsigned long long hk2 = hash_f(key);
  129.     hshmap a;
  130.     a.s = ""; a.sz = 0;
  131.     for (int i = 0; i < hash_map[hk].size(); ++i) {
  132.         if (hash_f(hash_map[hk][i].s) == hk2) {
  133.             hash_map[hk][i] = a;
  134.             return;
  135.         }
  136.     }
  137. }
  138.  
  139. int main() {
  140.  
  141. #ifdef _DEBUG
  142.     freopen("debug.in", "r", stdin);
  143.     freopen("debug.out", "w", stdout);
  144. #else
  145.     freopen(TASK".in", "r", stdin);
  146.     freopen(TASK".out", "w", stdout);
  147. #endif // _DEBUG
  148.  
  149.     ios_base::sync_with_stdio(0);
  150.     cin.tie(0);
  151.     cout.tie(0);
  152.  
  153.     while (cin >> a) {
  154.         cin >> key;
  155.         if (a[0] == 'p') {
  156.             cin >> zn;
  157.             put();
  158.         }
  159.         if (a[0] == 'g') {
  160.             get();
  161.         }
  162.         if (a[0] == 'd') {
  163.             if (a.size() == 6) {
  164.                 cin >> zn;
  165.                 del();
  166.             }
  167.             else {
  168.                 delall();
  169.             }
  170.         }
  171.     }
  172.  
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement