Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;
  6. ifstream fin("multimap.in");
  7. ofstream fout("multimap.out");
  8. vector <vector<string>> HashTable(100001, vector <string>(1));
  9.  
  10. int GetHash(string key)
  11. {
  12. int Hash = 0;
  13. int p = 39;
  14. for (size_t i = 0; i < key.size(); i++) {
  15. int s = 0;
  16. if (key[i] - 'a' >= 0 && key[i] - 'a' <= 9)
  17. s = key[i] - 'a';
  18. else
  19. s = key[i] - 'A';
  20. Hash = (Hash + ((s + 1) * p) % 1000000) % 1000000;
  21. p = (p * 39) % 1000000;
  22. }
  23. return Hash;
  24. }
  25.  
  26. void Put(string key, string value)
  27. {
  28. int Hash = GetHash(key);
  29. if (HashTable[Hash].size() == 1 && HashTable[Hash][0] == "")
  30. {
  31. HashTable[Hash][0] = value;
  32. return;
  33. }
  34. for (size_t i = 0; i < HashTable[Hash].size(); ++i)
  35. {
  36. if (HashTable[Hash][i] == value)
  37. return;
  38. }
  39. HashTable[Hash].push_back(value);
  40. }
  41. int Find(int Hash, string value)
  42. {
  43. for (size_t i = 0; i < HashTable[Hash].size(); ++i)
  44. {
  45. if (HashTable[Hash][i] == value)
  46. return i;
  47. }
  48. }
  49. void Delete(string key, string value)
  50. {
  51. int Hash = GetHash(key);
  52. int index = Find(Hash, value);
  53. HashTable[Hash].erase(HashTable[Hash].begin() + index);
  54. }
  55. void DeleteAll(string key)
  56. {
  57. int Hash = GetHash(key);
  58. HashTable[Hash].clear();
  59. HashTable[Hash].push_back("");
  60. }
  61. void Get(string key)
  62. {
  63. int Hash = GetHash(key);
  64. if (HashTable[Hash][0] == "")
  65. {
  66. cout << "0" << endl;
  67. return;
  68. }
  69. int size = HashTable[Hash].size();
  70. cout << size << " ";
  71. for (size_t i = 0; i < HashTable[Hash].size(); ++i)
  72. cout << HashTable[Hash][i] << " ";
  73. cout << endl;
  74. }
  75. int main()
  76. {
  77. string s;
  78. while (cin >> s)
  79. {
  80. if (s == "put")
  81. {
  82. string key, value;
  83. cin >> key >> value;
  84. Put(key, value);
  85. }
  86. if (s == "delete")
  87. {
  88. string key, value;
  89. cin >> key >> value;
  90. Delete(key, value);
  91. }
  92. if (s == "deleteall")
  93. {
  94. string key;
  95. cin >> key;
  96. DeleteAll(key);
  97. }
  98. if (s == "get")
  99. {
  100. string key;
  101. cin >> key;
  102. Get(key);
  103. }
  104. }
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement