Advertisement
Goga21

Untitled

Apr 2nd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. int mod(long a, long b)
  5. {
  6.     if(b < 0)
  7.         return -mod(-a, -b);
  8.     int ret = a % b;
  9.     if(ret < 0)
  10.         ret+=b;
  11.     return ret;
  12. }
  13.  
  14. struct Hashmap{
  15.     vector<pair<int, int>> element;
  16.     int a = 13;
  17.     long m = 1e9 + 19;
  18.     long capacity;
  19.     long count = 0;
  20.  
  21.     long hash(long key) const{
  22.         long c = a;
  23.         long sum = 0;
  24.         for (int i = 0; key != 0; ++i) {
  25.             sum += mod((mod(key, 10) * c), m);
  26.             c = mod((c * a), m);
  27.             key/=10;
  28.         }
  29.         return sum %capacity;
  30.     }
  31.     void init(int size){
  32.         this->element.resize(size);
  33.         capacity = size;
  34.     }
  35.     void put(long key, long value){
  36.         long hashed = hash(key);
  37.         if(this->element[hashed].first == key){
  38.             this->element[hashed].second = value;
  39.         }else if(this->element[hashed].first == 0){
  40.             this->element[hashed].first = key;
  41.             this->element[hashed].second = value;
  42.         }else if(this->element[hashed].first != 0){
  43.             long temp = hashed + 1;
  44.             while(this->element[temp].first != 0){
  45.                 if(temp < capacity){
  46.                     temp++;
  47.                 }else{
  48.                     temp = 0;
  49.                 }
  50.             }
  51.             this->element[temp].first = key;
  52.             this->element[temp].second = value;
  53.         }
  54.         this->count++;
  55.     }
  56.     long get(long key) const{
  57.         long hashed = hash(key);
  58.         if(this->element[hashed].first == key){
  59.             return this->element[hashed].second;
  60.         }else{
  61.             for(int i = hashed + 1; i < count; i++){
  62.                 if(i == count - 1){
  63.                     for(int j = 0; j < hashed; j++){
  64.                         if(this->element[j].first == key) return this->element[j].second;
  65.                     }
  66.                     break;
  67.                 }
  68.                 if(this->element[i].first == key) return this->element[i].second;
  69.             }
  70.             return -1;
  71.         }
  72.     }
  73.     long delete_pair(int key){
  74.         long hashed = hash(key);
  75.         if(this->element[hashed].first == 0){
  76.             return -1;
  77.         }
  78.         int val = this->element[hashed].second;
  79.         this->element[hashed].second = 0;
  80.         this->element[hashed].first = 0;
  81.         return val;
  82.     }
  83. };
  84.  
  85.  
  86. int main(){
  87.         ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  88.         Hashmap hashmap;
  89.         int n;
  90.         cin >> n;
  91.  
  92.         hashmap.init(n + 1);
  93.         vector<string> res;
  94.         while(n--){
  95.             string request;
  96.             cin >> request;
  97.             if(request == "get" || request == "delete"){
  98.                 int key;
  99.                 cin >> key;
  100.                 int ans;
  101.                 if(request == "get"){
  102.                     ans = hashmap.get(key);
  103.                 }else{
  104.                     ans = hashmap.delete_pair(key);
  105.                 }
  106.                 if(ans == -1){
  107.                     res.emplace_back("None");
  108.                 }else{
  109.                     res.push_back(to_string(ans));
  110.                 }
  111.             }else if(request == "put"){
  112.                 int key;
  113.                 int value;
  114.                 cin >> key >> value;
  115.                 hashmap.put(key, value);
  116.             }
  117.         }
  118.         for(const auto& el : res){
  119.             cout << el << '\n';
  120.         }
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement