Advertisement
NHumme

5D

Nov 15th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 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. vector < pair < string, vector < string > > > hash_map[1010101];
  16.  
  17. int hash_f(string s) {
  18.     int p = 359;
  19.     int hs = 0;
  20.     for (int i = 0; i < s.size(); i++) {
  21.         hs = (hs * p + s[i]) % 1000000;
  22.     }
  23.     return hs;
  24. }
  25.  
  26. void get(string key) {
  27.     int hs = hash_f(key), sz = 0;
  28.     for (int i = 0; i < hash_map[hs].size(); i++) {
  29.         if (hash_map[hs][i].first == key) {
  30.             for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
  31.                 if (hash_map[hs][i].second[j] != "") {
  32.                     sz++;
  33.                 }
  34.             }
  35.         }
  36.     }
  37.     for (int i = 0; i < hash_map[hs].size(); i++) {
  38.         if (hash_map[hs][i].first == key) {
  39.             cout << sz << " ";
  40.             for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
  41.                 if (hash_map[hs][i].second[j] != "") {
  42.                     cout << hash_map[hs][i].second[j] << " ";
  43.                 }
  44.             }
  45.             cout << "\n";
  46.             return;
  47.         }
  48.     }
  49. }
  50.  
  51. void put(string key, string s) {
  52.     int hs = hash_f(key), t = -1;
  53.     for (int i = 0; i < hash_map[hs].size(); i++) {
  54.         if (hash_map[hs][i].first == key) {
  55.             for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
  56.                 if (hash_map[hs][i].second[j] == s) {
  57.                     return;
  58.                 }
  59.                 if (hash_map[hs][i].second[j] == "" && t == -1) {
  60.                     t = j;
  61.                 }
  62.             }
  63.             if (t != -1) {
  64.                 hash_map[hs][i].second[t] = s;
  65.             }
  66.             else {
  67.                 hash_map[hs][i].second.push_back(s);
  68.             }
  69.             return;
  70.         }
  71.     }
  72.     vector < string > a;
  73.     a.push_back(s);
  74.     hash_map[hs].push_back(make_pair(key, a));
  75. }
  76.  
  77. void del(string key, string s) {
  78.     int hs = hash_f(key), t = -1;
  79.     for (int i = 0; i < hash_map[hs].size(); i++) {
  80.         if (hash_map[hs][i].first == key) {
  81.             for (int j = 0; j < hash_map[hs][i].second.size(); j++) {
  82.                 if (hash_map[hs][i].second[j] == s) {
  83.                     hash_map[hs][i].second[j] = "";
  84.                     return;
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90. void delall(string key) {
  91.     int hs = hash_f(key);
  92.     for (int i = 0; i < hash_map[hs].size(); i++) {
  93.         if (hash_map[hs][i].first == key) {
  94.             hash_map[hs][i].second = { };
  95.         }
  96.     }
  97. }
  98.  
  99. /*string next_string(string s) {
  100.     for (int i = s.size() - 1; i >= 0; i--) {
  101.         if (s[i] == 'z') {
  102.             s[i] = 'a';
  103.             continue;
  104.         }
  105.         s[i] = char(s[i] + 1);
  106.         return s;
  107.     }
  108.     for (int i = 0; i < s.size(); i++) {
  109.         s[i] = 'a';
  110.     }
  111.     return s;
  112. }*/
  113.  
  114. int main() {
  115.  
  116. #ifdef _DEBUG
  117.     freopen("debug.in", "r", stdin);
  118.     freopen("debug.out", "w", stdout);
  119. #else
  120.     freopen(TASK".in", "r", stdin);
  121.     freopen(TASK".out", "w", stdout);
  122. #endif // _DEBUG
  123.  
  124.     ios_base::sync_with_stdio(0);
  125.     cin.tie(0);
  126.     cout.tie(0);
  127.  
  128.     string a, key, s;
  129.     while (cin >> a) {
  130.         cin >> key;
  131.         if (a[0] == 'p') {
  132.             cin >> s;
  133.             put(key, s);
  134.         }
  135.         if (a[0] == 'g') {
  136.             get(key);
  137.         }
  138.         if (a[0] == 'd') {
  139.             if (a.size() == 6) {
  140.                 cin >> s;
  141.                 del(key, s);
  142.             }
  143.             else {
  144.                 delall(key);
  145.             }
  146.         }
  147.     }
  148.  
  149.     /*string s = "", c;
  150.     for (int i = 1; i <= 5; i++) {
  151.         s += 'a';
  152.         c = s;
  153.         do {
  154.             if (hash_f(c) == 97) {
  155.                 cout << c << "\n";
  156.             }
  157.             c = next_string(c);
  158.         } while (s != c);
  159.     }*/
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement