Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define TASKNAME "multimap"
  4. using namespace std;
  5.  
  6. bool prime(int x) {
  7. for(int i = 2; i * i <= x; i ++)
  8. if(x % i == 0)
  9. return 0;
  10. return 1;
  11. }
  12.  
  13. int getSmCode(char x) {
  14. if(x >= 'a' && x <= 'z')
  15. return x - 'a' + 1;
  16. return x - 'A' + 1 + 26;
  17. }
  18.  
  19. struct HashSet{
  20. int mod = 541, cnt = 0;
  21. vector<string> arr[541];
  22.  
  23. HashSet() {
  24. for(int i = 0; i < 541; i ++)
  25. arr[i] = {};
  26. }
  27.  
  28. int count() {
  29. return this -> cnt;
  30. }
  31.  
  32. int getHash(string x) {
  33. int cp = 1;
  34. int res = 0;
  35. for(auto i : x) {
  36. res += (getSmCode(i) * cp) % mod;
  37. cp *= 59;
  38. cp %= mod;
  39. res %= mod;
  40. }
  41. return res % mod;
  42. }
  43.  
  44. bool exists(string x) {
  45. int hash = getHash(x);
  46. for(auto i : arr[hash])
  47. if(i == x)
  48. return 1;
  49. return 0;
  50. }
  51.  
  52. void insert(string x) {
  53. if(exists(x))
  54. return;
  55. cnt++;
  56. arr[getHash(x)].push_back(x);
  57. }
  58.  
  59. void print() {
  60. cout << cnt;
  61. for(int i = 0; i < mod; i ++)
  62. for(auto j : arr[i])
  63. cout << ' ' << j;
  64. cout << "\n";
  65. }
  66.  
  67. void remove(string x) {
  68. if(!exists(x)) return;
  69. cnt--;
  70. int h = getHash(x);
  71. int idx = 0;
  72. int n = arr[h].size();
  73. for(int i = 0; i < n; i ++)
  74. if(arr[h][i] == x)
  75. idx = i;
  76. swap(arr[h][idx], arr[h][arr[h].size() - 1]);
  77. arr[h].pop_back();
  78. }
  79. };
  80.  
  81. struct HashMap{
  82. int mod = 1e6 + 33;
  83. vector<pair<string, HashSet*> > arr[(int)(1e6 + 33)];
  84.  
  85. HashMap() {
  86. mod = 1e6+33;
  87. for(int i = 0; i < mod; i ++)
  88. arr[i] = {};
  89. }
  90.  
  91. int getHash(string x) {
  92. int cp = 1;
  93. int res = 0;
  94. for(auto i : x) {
  95. res += (i * cp) % mod;
  96. cp *= 137;
  97. cp %= mod;
  98. res %= mod;
  99. }
  100. return res % mod;
  101. }
  102.  
  103. void put(string x, string y) {
  104. if(!exists(x))
  105. {
  106. arr[getHash(x)].push_back({x, new HashSet()});
  107. arr[getHash(x)].back().second -> insert(y);
  108. return;
  109. }
  110. int hash = getHash(x), idx = 0;
  111. for(int i = 0; i < arr[hash].size(); i ++)
  112. if(arr[hash][i].first == x)
  113. arr[hash][i].second -> insert(y);
  114. }
  115.  
  116. bool exists(string x) {
  117. int hash = getHash(x);
  118. for(int i = 0; i < arr[hash].size(); i++)
  119. if(arr[hash][i].first == x)
  120. return 1;
  121. return 0;
  122. }
  123.  
  124. void get(string x) {
  125. if(!exists(x))
  126. {
  127. cout << "0\n";
  128. return;
  129. }
  130. int hash = getHash(x);
  131. for(int i = 0; i < arr[hash].size(); i ++)
  132. if(arr[hash][i].first == x)
  133. arr[hash][i].second -> print();
  134. }
  135.  
  136. void remove(string x, string y) {
  137. if(!exists(x)) return;
  138. int hash = getHash(x), idx = -1;
  139. for(int i = 0; i < arr[hash].size(); i ++)
  140. if(arr[hash][i].first == x)
  141. idx = i;
  142. if(idx == -1) return;
  143. arr[hash][idx].second -> remove(y);
  144. if(arr[hash][idx].second -> count() == 0)
  145. remove_all(x);
  146. }
  147.  
  148. void remove_all(string x) {
  149. if(!exists(x)) return;
  150. int idx = -1, hash = getHash(x);
  151. for(int i = 0; i < arr[hash].size(); i ++)
  152. if(arr[hash][i].first == x)
  153. idx = i;
  154. if(idx == -1) return;
  155. if(arr[hash].size() == 0) return;
  156. swap(arr[hash][idx], arr[hash][arr[hash].size() - 1]);
  157. arr[hash].pop_back();
  158. }
  159. };
  160.  
  161. HashMap* hm;
  162.  
  163. int32_t main() {
  164. srand(time(NULL));
  165. ios_base::sync_with_stdio(0);
  166. freopen(TASKNAME".in", "r", stdin);
  167. freopen(TASKNAME".out", "w", stdout);
  168. cin.tie(NULL);
  169. cout.tie(NULL);
  170. string s;
  171. string x, y;
  172. hm = new HashMap();
  173. while(cin >> s) {
  174. cin >> x;
  175. if(s == "put")
  176. {
  177. cin >> y;
  178. hm -> put(x, y);
  179. }
  180. else if(s == "get")
  181. hm -> get(x);
  182. else if(s == "delete")
  183. {
  184. cin >> y;
  185. hm -> remove(x, y);
  186. } else if(s == "deleteall")
  187. hm -> remove_all(x);
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement