Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct list{
  6. string element;
  7. string key;
  8. list* next;
  9. };
  10.  
  11. struct map{
  12. list* a[500009];
  13. map(){
  14. for (int i = 0; i < 500009; i++)
  15. a[i] = nullptr;
  16. }
  17.  
  18. void Put(string strKey, string el, int hashKey){
  19. list *findEl = a[hashKey];
  20. list *prevEl = findEl;
  21. while (findEl != nullptr && findEl->key != strKey) {
  22. prevEl = findEl;
  23. findEl = findEl->next;
  24. }
  25. if (findEl == nullptr) {
  26. list *newElement = new list;
  27. newElement->next = nullptr;
  28. newElement->element = el;
  29. newElement->key = strKey;
  30. if (prevEl == nullptr) {
  31. a[hashKey] = newElement;
  32. }
  33. else {
  34. prevEl->next = newElement;
  35. }
  36. }
  37. else {
  38. findEl->element = el;
  39. }
  40. }
  41.  
  42. string Get(string strKey, int hashKey) {
  43. list *findEl = a[hashKey];
  44. while (findEl != nullptr && findEl->key != strKey) {
  45. findEl = findEl->next;
  46. }
  47. if (findEl == nullptr)
  48. return "none";
  49. else
  50. return findEl->element;
  51. }
  52.  
  53. void Delete(string strKey, int hashKey){
  54. list* findEl = a[hashKey];
  55. list* prevEl = a[hashKey];
  56. int flag = 1;
  57. while(findEl != nullptr && findEl -> key != strKey){
  58. prevEl = findEl;
  59. findEl = findEl -> next;
  60. }
  61. if (findEl != nullptr && findEl -> key == strKey){
  62. if (findEl == a[hashKey])
  63. a[hashKey] = findEl -> next;
  64. else
  65. prevEl -> next = findEl -> next;
  66. delete(findEl);
  67. }
  68. }
  69. };
  70.  
  71. int primeNum[26] = {25013, 25031, 25033, 25037, 25057, 25073,
  72. 25087, 25097, 25111, 25117, 25121, 25127,
  73. 25147, 25153, 25163, 25169, 25171, 25183,
  74. 25189, 25219, 25229, 25237, 25243, 25247, 25253, 25261};
  75.  
  76. int FindHashKey(string str){
  77. int ans = 0, degree = 1;
  78. for (int i = 0; i < str.size(); i++) {
  79. ans = (abs(ans + (int) str[i] * degree) % 500009);
  80. degree *= primeNum[(int)str[0] - (int)'a'];
  81. }
  82. return ans;
  83. }
  84.  
  85. int main(){
  86. ios_base::sync_with_stdio(false);
  87. ifstream in;
  88. ofstream out;
  89. in.open("map.in");
  90. out.open("map.out");
  91. in.tie(NULL);
  92. map Map;
  93. while(!in.eof()){
  94. string com, element, key;
  95. in >> com;
  96. if (com.size() == 0)
  97. break;
  98. if (com == "put"){
  99. in >> key >> element;
  100. Map.Put(key, element, FindHashKey(key));
  101. }
  102. if (com == "get"){
  103. in >> key;
  104. out << Map.Get(key, FindHashKey(key)) << "\n";
  105. }
  106. if (com == "delete"){
  107. in >> key;
  108. Map.Delete(key, FindHashKey(key));
  109. }
  110. }
  111. in.close();
  112. out.close();
  113. return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement