Advertisement
Guest User

Untitled

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