Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define TASKNAME "linkedmap"
  4. using namespace std;
  5.  
  6. const int MOD = 1000033;
  7.  
  8. struct StringIntHashMap{
  9. vector<pair<string, int> > arr[MOD];
  10.  
  11. StringIntHashMap() {
  12. for(int i = 0; i < MOD; i ++)
  13. arr[i] = {};
  14. }
  15.  
  16. int getHash(string x) {
  17. int cp = 1;
  18. int res = 0;
  19. for(auto i : x) {
  20. res += ((i - 'a' + 1) * cp) % MOD;
  21. cp *= 53;
  22. cp %= MOD;
  23. res %= MOD;
  24. }
  25. res = abs(res);
  26. return res % MOD;
  27. }
  28.  
  29. void put(string x, int y) {
  30. int hash = getHash(x);
  31. for(int i = 0; i < arr[hash].size(); i ++)
  32. if(arr[hash][i].first == x)
  33. {
  34. arr[hash][i].second = y;
  35. return;
  36. }
  37. arr[hash].push_back({x, y});
  38. }
  39.  
  40. int get(string x) {
  41. int hash = getHash(x);
  42. for(auto i : arr[hash])
  43. if(i.first == x)
  44. return i.second;
  45. return -1;
  46. }
  47. };
  48.  
  49. vector<string> keys;
  50. StringIntHashMap* key_pos;
  51.  
  52. struct HashMap{
  53. vector<pair<string, string> > arr[MOD];
  54.  
  55. HashMap() {
  56. for(int i = 0; i < MOD; i ++)
  57. arr[i] = {};
  58. }
  59.  
  60. int getHash(string x) {
  61. int cp = 1;
  62. int res = 0;
  63. for(auto i : x) {
  64. res += ((i - 'a' + 1) * cp) % MOD;
  65. cp *= 53;
  66. cp %= MOD;
  67. res %= MOD;
  68. }
  69. res = abs(res);
  70. return res % MOD;
  71. }
  72.  
  73. void put(string x, string y) {
  74. int hash = getHash(x);
  75. if(key_pos->get(x) == -1)
  76. {
  77. key_pos->put(x, keys.size());
  78. keys.push_back(x);
  79. }
  80. for(int i = 0; i < arr[hash].size(); i ++)
  81. if(arr[hash][i].first == x)
  82. {
  83. arr[hash][i].second = y;
  84. return;
  85. }
  86. arr[hash].push_back({x, y});
  87. }
  88.  
  89. bool exists(string x) {
  90. int hash = getHash(x);
  91. for(auto i : arr[hash])
  92. if(i.first == x)
  93. return 1;
  94. return 0;
  95. }
  96.  
  97. string get(string x) {
  98. if(!exists(x))
  99. return "none";
  100. int hash = getHash(x);
  101. for(auto i : arr[hash])
  102. if(i.first == x)
  103. return i.second;
  104. return "none";
  105. }
  106.  
  107. void remove(string x) {
  108. if(!exists(x))
  109. return;
  110. int idx = 0;
  111. int hash = getHash(x);
  112. if(arr[hash].size() < 0) return;
  113. for(int i = 0; i < arr[hash].size(); i ++)
  114. if(arr[hash][i].first == x)
  115. idx = i;
  116. swap(arr[hash][idx], arr[hash][arr[hash].size() - 1]);
  117. arr[hash].pop_back();
  118. keys[key_pos -> get(x)] = "";
  119. key_pos -> put(x, -1);
  120. }
  121. };
  122.  
  123. HashMap* hm;
  124.  
  125. int32_t main() {
  126. ios_base::sync_with_stdio(0);
  127. ifstream fin(TASKNAME".in");
  128. ofstream fout(TASKNAME".out");
  129. string s;
  130. string x, y;
  131. hm = new HashMap();
  132. key_pos = new StringIntHashMap();
  133. while(fin >> s) {
  134. fin >> x;
  135. if(s == "put")
  136. {
  137. fin >> y;
  138. hm -> put(x, y);
  139. }
  140. else if(s == "get")
  141. fout << (hm -> get(x)) << endl;
  142. else if(s == "delete")
  143. hm -> remove(x);
  144. else if(s == "prev")
  145. {
  146. if(!hm -> exists(x))
  147. {
  148. fout << "none" << endl;
  149. continue;
  150. }
  151. int pos = key_pos -> get(x);
  152. if(!pos)
  153. {
  154. fout << "none" << endl;
  155. continue;
  156. }
  157. int dec = 1;
  158. while(pos - dec >= 0 && keys[pos - dec] == "")
  159. dec++;
  160. if(pos - dec < 0) {
  161. fout << "none" << endl;
  162. continue;
  163. }
  164. fout << hm -> get(keys[pos-dec]) << endl;
  165. } else if(s == "next")
  166. {
  167. if(!hm -> exists(x))
  168. {
  169. fout << "none" << endl;
  170. continue;
  171. }
  172. int pos = key_pos -> get(x);
  173. if(pos == keys.size() - 1)
  174. {
  175. fout << "none" << endl;
  176. continue;
  177. }
  178. int inc = 1;
  179. while(pos + inc < keys.size() && keys[pos + inc] == "")
  180. inc++;
  181. if(pos + inc >= keys.size()) {
  182. fout << "none" << endl;
  183. continue;
  184. }
  185. fout << hm -> get(keys[pos+inc]) << endl;
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement