Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. std::string str = "none";
  6. const int N = 100000;
  7. const std::string aaa = "aaaaaaaaaaaaaaaaaaaaa";
  8. std::string prev1 = aaa;
  9.  
  10. struct hashset {
  11.     std::vector<std::string> a[N];
  12.     int get_hash(std::string x) {
  13.         int t = 0;
  14.         for (int i = 0; i < x.length(); i++)
  15.             t += ((i + 1)*int(x[i]));
  16.         return t;
  17.     }
  18.     void add_next(std::string next2, std::string x) {
  19.         int hash = get_hash(x);
  20.         std::string y, prev2;
  21.         for (long long i = 0; i < a[hash].size(); i += 4) {
  22.             if (x == a[hash][i]) {
  23.                 y = a[hash][i + 1];
  24.                 prev2 = a[hash][i + 2];
  25.                 std::swap(a[hash][i], a[hash][a[hash].size() - 4]);
  26.                 std::swap(a[hash][i + 1], a[hash][a[hash].size() - 3]);
  27.                 std::swap(a[hash][i + 2], a[hash][a[hash].size() - 2]);
  28.                 std::swap(a[hash][i + 3], a[hash][a[hash].size() - 1]);
  29.                 a[hash].pop_back();
  30.                 a[hash].pop_back();
  31.                 a[hash].pop_back();
  32.                 a[hash].pop_back();
  33.             }
  34.         }
  35.         a[hash].push_back(x);
  36.         a[hash].push_back(y);
  37.         a[hash].push_back(prev2);
  38.         a[hash].push_back(next2);
  39.     }
  40.     void add_prev(std::string prev2, std::string x) {
  41.         int hash = get_hash(x);
  42.         std::string y, next2;
  43.         for (long long i = 0; i < a[hash].size(); i += 4) {
  44.             if (x == a[hash][i]) {
  45.                 y = a[hash][i + 1];
  46.                 next2 = a[hash][i + 3];
  47.                 std::swap(a[hash][i], a[hash][a[hash].size() - 4]);
  48.                 std::swap(a[hash][i + 1], a[hash][a[hash].size() - 3]);
  49.                 std::swap(a[hash][i + 2], a[hash][a[hash].size() - 2]);
  50.                 std::swap(a[hash][i + 3], a[hash][a[hash].size() - 1]);
  51.                 a[hash].pop_back();
  52.                 a[hash].pop_back();
  53.                 a[hash].pop_back();
  54.                 a[hash].pop_back();
  55.             }
  56.         }
  57.         a[hash].push_back(x);
  58.         a[hash].push_back(y);
  59.         a[hash].push_back(prev2);
  60.         a[hash].push_back(next2);
  61.     }
  62.     void put(std::string x, std::string y) {
  63.         int hash = get_hash(x);
  64.         std::string tmp1 = prev1;
  65.         std::string tmp2 = aaa;
  66.         int tmp3 = 0;
  67.         for (long long i = 0; i < a[hash].size(); i+=4) {
  68.             if (x == a[hash][i]) {
  69.                 tmp1 = a[hash][i + 2];
  70.                 tmp2 = a[hash][i + 3];
  71.                 tmp3 = 1;
  72.                 std::swap(a[hash][i], a[hash][a[hash].size() - 4]);
  73.                 std::swap(a[hash][i + 1], a[hash][a[hash].size() - 3]);
  74.                 std::swap(a[hash][i + 2], a[hash][a[hash].size() - 2]);
  75.                 std::swap(a[hash][i + 3], a[hash][a[hash].size() - 1]);
  76.                 a[hash].pop_back();
  77.                 a[hash].pop_back();
  78.                 a[hash].pop_back();
  79.                 a[hash].pop_back();
  80.             }
  81.         }
  82.         a[hash].push_back(x);
  83.         a[hash].push_back(y);
  84.         a[hash].push_back(tmp1);
  85.         a[hash].push_back(tmp2);
  86.         if (tmp3 != 1) {
  87.             if (x != prev1 && prev1 != aaa) add_next(x, prev1);
  88.             prev1 = x;
  89.         }
  90.     }
  91.     void del(std::string x) {
  92.         int hash = get_hash(x);
  93.         for (long long i = 0; i < a[hash].size(); i+=4) {
  94.             if (x == a[hash][i]) {
  95.                 if (a[hash][i + 3] == a[hash][i + 2] && a[hash][i + 3] == aaa) prev1 = a[hash][i + 2];
  96.                 add_next(a[hash][i + 3], a[hash][i + 2]);
  97.                 if (a[hash][i + 3] != aaa) add_prev(a[hash][i + 2], a[hash][i + 3]);
  98.                 prev1 = a[hash][i + 2];
  99.                 std::swap(a[hash][i], a[hash][a[hash].size() - 4]);
  100.                 std::swap(a[hash][i + 1], a[hash][a[hash].size() - 3]);
  101.                 std::swap(a[hash][i + 2], a[hash][a[hash].size() - 2]);
  102.                 std::swap(a[hash][i + 3], a[hash][a[hash].size() - 1]);
  103.                 a[hash].pop_back();
  104.                 a[hash].pop_back();
  105.                 a[hash].pop_back();
  106.                 a[hash].pop_back();
  107.                 return;
  108.             }
  109.         }
  110.     }
  111.     std::string get(std::string x) {
  112.         int hash = get_hash(x);
  113.         for (long long i = 0; i < a[hash].size(); i+=4) {
  114.             if (x == a[hash][i] && a[hash][i + 1] != "") return a[hash][i + 1];
  115.         }
  116.         return str;
  117.     }
  118.     std::string next(std::string x) {
  119.         int hash = get_hash(x);
  120.         for (long long i = 0; i < a[hash].size(); i+=4) {
  121.             if (x == a[hash][i] && a[hash][i + 3] != aaa) return get(a[hash][i + 3]);
  122.         }
  123.         return str;
  124.     }
  125.     std::string prev(std::string x) {
  126.         int hash = get_hash(x);
  127.         for (long long i = 0; i < a[hash].size(); i+=4) {
  128.             if (x == a[hash][i] && a[hash][i + 2] != aaa) return get(a[hash][i + 2]);
  129.         }
  130.         return str;
  131.     }
  132. };
  133.  
  134.  
  135. int main() {
  136.     std::ios_base::sync_with_stdio(false);
  137.     freopen("linkedmap.in", "r", stdin);
  138.     freopen("linkedmap.out", "w", stdout);
  139.     hashset map = *new hashset();
  140.     std::string text;
  141.     std::string x, y;
  142.     while (std::cin >> text)
  143.     {
  144.         if (text == "put") {
  145.             std::cin >> x >> y;
  146.             map.put(x, y);
  147.         } else if (text == "get") {
  148.             std::cin >> x;
  149.             std::cout << map.get(x) << "\n";
  150.         } else if (text == "next") {
  151.             std::cin >> x;
  152.             std::cout << map.next(x) << "\n";
  153.         } else if (text == "prev") {
  154.             std::cin >> x;
  155.             std::cout << map.prev(x) << "\n";
  156.         } else {
  157.             std::cin >> x;
  158.             map.del(x);
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement