Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <string>
  7. #include <list>
  8. #include <unordered_map>
  9. using namespace std;
  10.  
  11. #define mp make_pair
  12.  
  13.  
  14. struct List {
  15. List() : next(nullptr), prev(nullptr), value("") {
  16. }
  17. List *next = nullptr;
  18. List *prev = nullptr;
  19. string value;
  20. };
  21. struct Node {
  22. string first;
  23. string second;
  24. List *a;
  25. };
  26. List *last = nullptr;
  27. struct HashTable {
  28. HashTable() : v(vector<vector<Node>>(size)) {
  29. p.push_back(37);
  30. for (int i = 0; i < 100; ++i) {
  31. p.push_back(p.back() * 37);
  32. }
  33. }
  34.  
  35. unsigned int hash(string x) {
  36. unsigned int pos = 0;
  37. for (size_t i = 0; i < x.size(); ++i) {
  38. pos += x[i] * p[i];
  39. }
  40.  
  41. return pos % size;
  42. }
  43. string get(string x) {
  44. if (!exists(x)) return "none";
  45. int pos = hash(x);
  46. int i = find_pos(v[pos], x);
  47. return v[pos][i].second;
  48. }
  49. bool exists(const string &x) {
  50. int pos = hash(x);
  51. for (size_t i = 0; i < v[pos].size(); ++i) {
  52. if (v[pos][i].first == x) return 1;
  53. }
  54. return 0;
  55. }
  56. int find_pos(const vector<Node> &mas, const string s) {
  57. for (size_t i = 0; i < mas.size(); ++i) {
  58. if (mas[i].first == s) {
  59. return (int)i;
  60. }
  61. }
  62. return -1;
  63. }
  64. void insert(string x, string s) {
  65. if (exists(x)) {
  66. int pos = hash(x);
  67. int i = find_pos(v[pos], x);
  68.  
  69. v[pos][i].second = s;
  70. return;
  71. }
  72. List *l = new List();
  73. l->prev = last;
  74. l->value = x;
  75. if (last != nullptr) last->next = l;
  76. last = l;
  77. Node q;
  78. q.first = x;
  79. q.second = s;
  80. q.a = l;
  81. cur_size++;
  82. v[hash(x)].push_back(q);
  83. }
  84. void erase(string &x) {
  85. if (!exists(x)) {
  86. return;
  87. }
  88. cur_size--;
  89. int pos = hash(x);
  90. int i = find_pos(v[pos], x);
  91. if (v[pos][i].a->prev != nullptr) {
  92. v[pos][i].a->prev->next = v[pos][i].a->next;
  93. }
  94. if (v[pos][i].a->next != nullptr) {
  95. v[pos][i].a->next->prev = v[pos][i].a->prev;
  96. } else {
  97. last = v[pos][i].a->prev;
  98. }
  99. delete v[pos][i].a;
  100. v[pos].erase(v[pos].begin() + i);
  101. }
  102. string next(const string x) {
  103. if (!exists(x)) {
  104. return "none";
  105. }
  106. int pos = hash(x);
  107. int i = find_pos(v[pos], x);
  108. if (v[pos][i].a->next == nullptr) {
  109. return "none";
  110. }
  111. return get(v[pos][i].a->next->value);
  112. }
  113. string prev(const string x) {
  114. if (!exists(x)) {
  115. return "none";
  116. }
  117. int pos = hash(x);
  118. int i = find_pos(v[pos], x);
  119. if (v[pos][i].a->prev == nullptr) {
  120. return "none";
  121. }
  122. return get(v[pos][i].a->prev->value);
  123. }
  124. int cur_size = 0;
  125. int size = 1000000;
  126. vector<vector<Node>> v;
  127. vector<int> p;
  128. };
  129.  
  130. int main() {
  131. #ifdef _KOCH
  132. freopen("read.txt", "r", stdin);
  133. #else
  134. freopen("linkedmap.in", "r", stdin);
  135. freopen("linkedmap.out", "w", stdout);
  136. #endif
  137. ios::sync_with_stdio(0);
  138. cin.tie(0);
  139. cout.tie(0);
  140. string s;
  141. HashTable h;
  142. while (cin >> s) {
  143. string x;
  144. cin >> x;
  145. if (s[0] == 'p' && s[1] == 'u') {
  146. string f;
  147. cin >> f;
  148. h.insert(x, f);
  149. continue;
  150. }
  151. if (s[0] == 'g') {
  152. cout << h.get(x) << "\n";
  153. continue;
  154. }
  155. if (s[0] == 'd') {
  156. h.erase(x);
  157. continue;
  158. }
  159. if (s[0] == 'n') {
  160. cout << h.next(x) << "\n";
  161. continue;
  162. }
  163. if (s[0] == 'p' && s[1] == 'r') {
  164. cout << h.prev(x) << "\n";
  165. }
  166. }
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement