Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <fstream>
  2. #include <list>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct item
  8. {
  9. string key, value;
  10. item *prev, *next, *after, *before;
  11. };
  12.  
  13. const int hash_size = 1e6 + 3;
  14.  
  15. class HashTable {
  16. public:
  17. item *hashMap[hash_size];
  18. item *last = nullptr;
  19.  
  20. int getHash(string &x) {
  21. int ans = 0;
  22. for (int i = 0; i < x.length(); i++) {
  23. ans *= 31;
  24. ans %= hash_size;
  25. ans += x[i] - 'A' + 1;
  26. ans %= hash_size;
  27. }
  28. return ans;
  29. }
  30.  
  31. void Put(string &key, string &value) {
  32. int h = getHash(key);
  33. bool changed = false;
  34. auto it = hashMap[h];
  35. while (it) {
  36. if (it->key == key) {
  37. it->value = value;
  38. changed = true;
  39. break;
  40. }
  41. it = it->next;
  42. }
  43. if (!changed) {
  44. it = new item();
  45. it->key = key;
  46. it->value = value;
  47. it->prev = nullptr;
  48. it->next = hashMap[h];
  49.  
  50. if (hashMap[h])
  51. hashMap[h]->prev = it;
  52.  
  53. it->after = nullptr;
  54.  
  55. if (last)
  56. last->after = it;
  57.  
  58. it->before = last;
  59. last = it;
  60. hashMap[h] = it;
  61. }
  62. }
  63.  
  64. void Delete(string &key) {
  65. int h = getHash(key);
  66. auto it = hashMap[h];
  67. while (it) {
  68. if (it->key == key) {
  69. if (!(it->prev)) {
  70. if (!(it->next)) {
  71. hashMap[h] = nullptr;
  72. } else {
  73. hashMap[h] = it->next;
  74. hashMap[h]->prev = nullptr;
  75. }
  76. } else {
  77. if (!(it->next)) {
  78. it->prev->next = nullptr;
  79. } else {
  80. it->prev->next = it->next;
  81. it->next->prev = it->prev;
  82. }
  83. }
  84. if (!(it->after)) {
  85. if (!(it->before)) {
  86. last = nullptr;
  87. } else {
  88. last = it->before;
  89. it->before->after = nullptr;
  90. }
  91. } else {
  92. if (!(it->before)) {
  93. it->after->before = nullptr;
  94. } else {
  95. it->after->before = it->before;
  96. it->before->after = it->after;
  97. }
  98. }
  99. delete it;
  100. break;
  101. }
  102. it = it->next;
  103. }
  104. }
  105.  
  106. string Get(string &key) {
  107. string ans = "none";
  108. int h = getHash(key);
  109. auto it = hashMap[h];
  110. while (it) {
  111. if (it->key == key) {
  112. ans = it->value;
  113. break;
  114. }
  115. it = it->next;
  116. }
  117. return ans;
  118. }
  119.  
  120. string Prev(string &key) {
  121. string ans = "none";
  122. int h = getHash(key);
  123. auto it = hashMap[h];
  124. while (it) {
  125. if (it->key == key && it->before) {
  126. ans = it->before->value;
  127. break;
  128. }
  129. it = it->next;
  130. }
  131. return ans;
  132. }
  133.  
  134. string Next(string &key) {
  135. string ans = "none";
  136. int h = getHash(key);
  137. auto it = hashMap[h];
  138. while (it) {
  139. if (it->key == key && it->after) {
  140. ans = it->after->value;
  141. break;
  142. }
  143. it = it->next;
  144. }
  145. return ans;
  146. }
  147. };
  148.  
  149. int main()
  150. {
  151. ifstream fin ("linkedmap.in");
  152. ofstream fout ("linkedmap.out");
  153. string query;
  154. HashTable HTable;
  155.  
  156. while (fin >> query)
  157. {
  158. string x, y;
  159. fin >> x;
  160. switch (query[0])
  161. {
  162. case 'p':
  163. {
  164. if (query[1] == 'u')
  165. {
  166. fin >> y;
  167. HTable.Put(x, y);
  168. }
  169. else
  170. {
  171. fout << HTable.Prev(x) << '\n';
  172. }
  173. break;
  174. }
  175. case 'd':
  176. {
  177. HTable.Delete(x);
  178. break;
  179. }
  180. case 'g':
  181. {
  182. fout << HTable.Get(x) << '\n';
  183. break;
  184. }
  185. case 'n':
  186. {
  187. fout << HTable.Next(x) << '\n';
  188. break;
  189. }
  190. }
  191. }
  192. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement