Advertisement
Guest User

Untitled

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