Advertisement
NHumme

LinkedMap

Nov 21st, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "linkedmap"
  14.  
  15. vector < pair < string, string > > hash_map[1010101];
  16. vector < pair < int, int > > pr[1010101], nx[1010101];
  17. pair < int, int > prvs = make_pair(-1, -1);
  18.  
  19. int hash_f(string s) {
  20.     int p = 359;
  21.     int hs = 0;
  22.     for (int i = 0; i < s.size(); i++) {
  23.         hs = (hs * p + s[i]) % 1000000;
  24.     }
  25.     return hs;
  26. }
  27.  
  28. string get(string key) {
  29.     int hs = hash_f(key);
  30.     for (int i = 0; i < hash_map[hs].size(); i++) {
  31.         if (hash_map[hs][i].first == key) {
  32.             if (hash_map[hs][i].second != "")
  33.                 return hash_map[hs][i].second;
  34.         }
  35.     }
  36.     return "";
  37. }
  38.  
  39. void put(string key, string s) {
  40.     int hs = hash_f(key);
  41.     for (int i = 0; i < hash_map[hs].size(); i++) {
  42.         if (hash_map[hs][i].first == key) {
  43.             hash_map[hs][i].second = s;
  44.             if (prvs != make_pair(-1, -1) && nx[prvs.first][prvs.second] == make_pair(-1, -1)) {
  45.                 nx[prvs.first][prvs.second] = make_pair(hs, i);
  46.             }
  47.             return;
  48.         }
  49.     }
  50.     hash_map[hs].push_back(make_pair(key, s));
  51.     pr[hs].push_back(prvs);
  52.     nx[hs].push_back(make_pair(-1, -1));
  53.     if (prvs != make_pair(-1, -1)) {
  54.         nx[prvs.first][prvs.second] = make_pair(hs, pr[hs].size() - 1);
  55.     }
  56.     prvs = make_pair(hs, pr[hs].size() - 1);
  57. }
  58.  
  59. void del(string key) {
  60.     int hs = hash_f(key);
  61.     for (int i = 0; i < hash_map[hs].size(); i++) {
  62.         if (hash_map[hs][i].first == key) {
  63.             if (hash_map[hs][i].second != "") {
  64.                 hash_map[hs][i].second = "";
  65.                 pr[nx[hs][i].first][nx[hs][i].second] = pr[hs][i];
  66.                 nx[pr[hs][i].first][pr[hs][i].second] = nx[hs][i];
  67.             }
  68.             return;
  69.         }
  70.     }
  71. }
  72.  
  73. pair < int, int > find(string key) {
  74.     int hs = hash_f(key);
  75.     for (int i = 0; i < hash_map[hs].size(); i++) {
  76.         if (hash_map[hs][i].first == key && hash_map[hs][i].second.size()) {
  77.             return make_pair(hs, i);
  78.         }
  79.     }
  80.     return make_pair(-1, -1);
  81. }
  82.  
  83. int main() {
  84.  
  85. #ifdef _DEBUG
  86.     freopen("debug.in", "r", stdin);
  87.     freopen("debug.out", "w", stdout);
  88. #else
  89.     freopen(TASK".in", "r", stdin);
  90.     freopen(TASK".out", "w", stdout);
  91. #endif // _DEBUG
  92.  
  93.     ios_base::sync_with_stdio(0);
  94.     cin.tie(0);
  95.     cout.tie(0);
  96.  
  97.     string a, key, s;
  98.     while (cin >> a) {
  99.         cin >> key;
  100.         if (a == "put") {
  101.             cin >> s;
  102.             put(key, s);
  103.             continue;
  104.         }
  105.         if (a == "get") {
  106.             s = get(key);
  107.             cout << (s.size() ? s : "none") << "\n";
  108.             continue;
  109.         }
  110.         if (a == "delete") {
  111.             del(key);
  112.             continue;
  113.         }
  114.         pair < int, int > f = find(key);
  115.         if (f == make_pair(-1, -1)) {
  116.             cout << "none\n";
  117.             continue;
  118.         }
  119.         if (a == "next") {
  120.             if (nx[f.first][f.second] == make_pair(-1, -1)) {
  121.                 cout << "none\n";
  122.             }
  123.             else {
  124.                 cout << hash_map[nx[f.first][f.second].first][nx[f.first][f.second].second].second << "\n";
  125.             }
  126.             continue;
  127.         }
  128.         if (pr[f.first][f.second] == make_pair(-1, -1)) {
  129.             cout << "none\n";
  130.         }
  131.         else {
  132.             cout << hash_map[pr[f.first][f.second].first][pr[f.first][f.second].second].second << "\n";
  133.         }
  134.     }
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement