Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. const int N = 1e5;
  5. const int Y = 1e5;
  6. const int lul = 31;
  7. int arr[N];
  8. struct seet{
  9. string key;
  10. vector <vector<string>> mas;
  11. seet(){
  12. mas.resize(228);
  13. }
  14. };
  15.  
  16. struct myltimap{
  17. vector <vector<seet>> set;
  18. myltimap(){
  19. set.resize(14880);
  20. }
  21. };
  22.  
  23. myltimap map;
  24. vector <string> memes;
  25. void module() {
  26. arr[0] = 1;
  27. for (int i = 1 ; i < N; ++ i) {
  28. arr[i] = arr[i - 1] * lul;
  29. arr[i] = abs(arr[i]);
  30. arr[i] %= Y;
  31. }
  32. }
  33.  
  34. int get_hash(string key, int size) {
  35. int hash_x = 0;
  36. for(char i : key) {
  37. hash_x = 41 * hash_x + i;
  38. }
  39. return abs(hash_x % size);
  40. }
  41.  
  42. void put(string key,string x){
  43. int hash_key = get_hash(key, 14880);
  44. for (auto & i : map.set[hash_key]){
  45. if (i.key == key){
  46. int elHash = get_hash(x, 228);
  47. for (const auto & luls : i.mas[elHash]){
  48. if (luls == x)
  49. return;
  50. }
  51. i.mas[elHash].push_back(x);
  52. return;
  53. }
  54. }
  55. auto* temp_set = new seet;
  56. temp_set->key = key;
  57. temp_set->mas[get_hash(x, 228)].push_back(x);
  58. map.set[hash_key].push_back(*temp_set);
  59. delete temp_set;
  60. }
  61.  
  62. void del(string key, string x){
  63. int hash_key = get_hash(key, 14880);
  64. for (auto & i : map.set[hash_key]){
  65. if (i.key == key){
  66. int hash_x = get_hash(x, 228);
  67. for (int luls = 0; luls < i.mas[hash_x].size(); luls++){
  68. if (i.mas[hash_x][luls] == x) {
  69. swap(i.mas[hash_x][luls], i.mas[hash_x].back());
  70. i.mas[hash_x].pop_back();
  71. return;
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78. void del_all(string key){
  79. int hash_key = get_hash(key, 14880);
  80. for (auto & i : map.set[hash_key]){
  81. if (i.key == key){
  82. for(auto & luls : i.mas){
  83. while(!luls.empty())
  84. luls.pop_back();
  85. }
  86. return;
  87. }
  88. }
  89. }
  90.  
  91. void get (string key){
  92. int hash_key = get_hash(key, 14880);
  93. for (auto & i : map.set[hash_key]){
  94. if (i.key == key){
  95. for(auto & luls : i.mas){
  96. for(const auto & k : luls) {
  97. memes.push_back(k);
  98. }
  99. }
  100. return;
  101. }
  102. }
  103. }
  104.  
  105. int main(){
  106. freopen("multimap.in", "r", stdin);
  107. freopen("multimap.out", "w", stdout);
  108. string com;
  109. while(cin >> com) {
  110. if (com == "put") {
  111. string key, x;
  112. cin >> key >> x;
  113. put(key, x);
  114. }
  115. if (com == "delete") {
  116. string key, x;
  117. cin >> key >> x;
  118. del(key, x);
  119. }
  120. if (com == "deleteall") {
  121. string key;
  122. cin >> key;
  123. del_all(key);
  124. }
  125. if (com == "get") {
  126. string key;
  127. cin >> key;
  128. get(key);
  129. cout << memes.size() << " ";
  130. for (const auto & i : memes)
  131. cout << i << " ";
  132. cout << endl;
  133. memes.resize(0);
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement