Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. //ifstream fin("multimap.txt");
  7. //ofstream fout("multimap2.txt");
  8.  
  9. struct item {
  10. string key;
  11. vector <string> val;
  12. };
  13.  
  14. vector<vector<item>> HashTable(10000001, vector<item>());
  15.  
  16. int HashKey(string key) {
  17. int sum = 0;
  18. for (int i = 0; i < key.size(); i++)
  19. sum = sum * 13 + (int) key[i];
  20.  
  21. return abs(sum % 100003);
  22. }
  23.  
  24. bool ExistsString(string key) {
  25. int hash = HashKey(key);
  26.  
  27. for (int i = 0; i < HashTable[hash].size(); i++)
  28. if (HashTable[hash][i].key == key)
  29. return true;
  30.  
  31. return false;
  32. }
  33.  
  34. string GetString(string key) {
  35. int hash = HashKey(key);
  36.  
  37. for (int i = 0; i < HashTable[hash].size(); i++)
  38. if (ExistsString(key))
  39. if (HashTable[hash][i].key == key) {
  40. cout << HashTable[hash][i].val.size() << " ";
  41. for (int j = 0; j < HashTable[hash][i].val.size(); j++)
  42. cout << HashTable[hash][i].val[j] << " ";
  43. }
  44. else
  45. cout << 0;
  46. }
  47.  
  48. void PutString(string key, string value) {
  49. int hash = HashKey(key);
  50. int flag = 0;
  51.  
  52. for (int i = 0; i < HashTable[hash].size(); i++)
  53. if (HashTable[hash][i].key == key) {
  54.  
  55. for (int j = 0; j < HashTable[hash][i].val.size(); j++)
  56. if (HashTable[hash][i].val[j] == value) {
  57. flag = 1;
  58. return;
  59. }
  60.  
  61. if (flag == 0) {
  62. flag = 2;
  63. HashTable[hash][i].val.push_back(value);
  64. }
  65. }
  66.  
  67. if (flag == 0) {
  68. HashTable[hash].push_back({key, { value }});
  69. }
  70. }
  71.  
  72. void DeleteString(string key, string value) {
  73. int hash = HashKey(key);
  74.  
  75. for (int i = 0; i < HashTable[hash].size(); i++)
  76. if (HashTable[hash][i].key == key)
  77. for (int j = 0; j < HashTable[hash][i].val.size(); j++)
  78. if (HashTable[hash][i].val[j] == value)
  79. HashTable[hash][i].val.erase(HashTable[hash][i].val.begin() + i);
  80. }
  81.  
  82. void DeleteAllString(string key) {
  83. int hash = HashKey(key);
  84.  
  85. for (int i = 0; i < HashTable[hash].size(); i++)
  86. if (HashTable[hash][i].key == key) {
  87. for (int j = 0; j < HashTable[hash][i].val.size(); j++)
  88. HashTable[hash][i].val.erase(HashTable[hash][i].val.begin() + i);
  89. HashTable[hash][i].key.erase(HashTable[hash][i].key.begin() + i);
  90. }
  91. }
  92.  
  93. int main() {
  94. string operation;
  95. string str, value;
  96.  
  97. while (cin >> operation) {
  98. if (operation == "put") {
  99. cin >> str >> value;
  100. PutString(str, value);
  101. }
  102.  
  103. if (operation == "delete") {
  104. cin >> str >> value;
  105. DeleteString(str, value);
  106. }
  107.  
  108. if (operation == "get") {
  109. cin >> str;
  110. GetString(str);
  111. cout << "\n";
  112. }
  113.  
  114. if (operation == "deleteall") {
  115. cin >> str;
  116. DeleteAllString(str);
  117. }
  118. }
  119.  
  120. //fin.close();
  121. //fout.close();
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement