Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.91 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define SIZE_H1 (int)1e+4 + 7
  3. #define SIZE_H2 (int)1e+2 + 9
  4. #include <iostream>
  5. #include <vector>
  6. #include <string>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. struct Pair {
  11.     string key;
  12.     vector < vector <string> > val;
  13. };
  14. vector <vector <Pair> > hash_t(SIZE_H1);
  15.  
  16. int hashFunc(string key, int size) {
  17.     int p = 137;
  18.     int p_pow = 1;
  19.     int hash = 0;
  20.     for (size_t i = 0; i < key.size(); i++) {
  21.         hash += (int)key[i] * p_pow;
  22.         p_pow *= p; p_pow %= size;
  23.         hash %= size;
  24.     }
  25.     return abs(hash);
  26. }
  27.  
  28. void putPair(string key, string val);
  29. void deletePair(string key, string val);
  30. void deleteAll(string key);
  31. void getKey(string key);
  32.  
  33. int main() {
  34.     ios_base::sync_with_stdio(false);
  35.  
  36.     freopen("multimap.in", "r", stdin);
  37.     freopen("multimap.out", "w", stdout);
  38.  
  39.     string operation, key, value;
  40.  
  41.     while (cin >> operation) {
  42.         cin >> key;
  43.         if (operation == "put") {
  44.             cin >> value;
  45.             putPair(key, value);
  46.         }
  47.         else if (operation == "delete") {
  48.             cin >> value;
  49.             deletePair(key, value);
  50.         }
  51.         else if (operation == "deleteall")
  52.             deleteAll(key);
  53.         else if (operation == "get")
  54.             getKey(key);
  55.     }
  56.     return 0;
  57. }
  58.  
  59. void putPair(string key, string val) {
  60.     int hash1 = hashFunc(key, SIZE_H1);
  61.     int hash2 = hashFunc(val, SIZE_H2);
  62.     for (size_t i = 0; i < hash_t[hash1].size(); i++) {
  63.         if (hash_t[hash1][i].key == key) {
  64.             auto exs = find(hash_t[hash1][i].val[hash2].begin(), hash_t[hash1][i].val[hash2].end(), val);
  65.             if (exs == hash_t[hash1][i].val[hash2].end())
  66.                 hash_t[hash1][i].val[hash2].push_back(val);
  67.             return;
  68.         }
  69.     }
  70.     Pair *p = new Pair;;
  71.     p->key = key;
  72.     p->val.resize(SIZE_H2);
  73.     p->val[hash2].push_back(val);
  74.     hash_t[hash1].push_back(*p);
  75.     delete p;
  76. }
  77.  
  78. void deletePair(string key, string val) {
  79.     int hash1 = hashFunc(key, SIZE_H1);
  80.     int hash2 = hashFunc(val, SIZE_H2);
  81.     for (size_t i = 0; i < hash_t[hash1].size(); i++)
  82.         if (hash_t[hash1][i].key == key)
  83.             for (size_t j = 0; j < hash_t[hash1][i].val[hash2].size(); j++)
  84.                 if (hash_t[hash1][i].val[hash2][j] == val) {
  85.                     swap(hash_t[hash1][i].val[hash2][j], hash_t[hash1][i].val[hash2].back());
  86.                     hash_t[hash1][i].val[hash2].pop_back();
  87.                 }
  88. }
  89.  
  90. void deleteAll(string key) {
  91.     int hash1 = hashFunc(key, SIZE_H1);
  92.     for (size_t i = 0; i < hash_t[hash1].size(); i++)
  93.         if (hash_t[hash1][i].key == key) {
  94.             hash_t[hash1][i] = hash_t[hash1].back();
  95.             hash_t[hash1].pop_back();
  96.         }
  97.  
  98. }
  99.  
  100. void getKey(string key) {
  101.     int hash1 = hashFunc(key, SIZE_H1);
  102.     vector <string> buffer;
  103.     for (size_t i = 0; i < hash_t[hash1].size(); i++)
  104.         if (hash_t[hash1][i].key == key) {
  105.             for (size_t j = 0; j < SIZE_H2; j++)
  106.                 for (size_t z = 0; z < hash_t[hash1][i].val[j].size(); z++)
  107.                     buffer.push_back(hash_t[hash1][i].val[j][z]);
  108.             cout << buffer.size() << " ";
  109.             for (size_t k = 0; k < buffer.size(); k++)
  110.                 cout << buffer[k] << " ";
  111.             cout << endl;
  112.             return;
  113.         }
  114.     cout << 0 << endl;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement