Advertisement
Goga21

Untitled

Apr 3rd, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Hashmap{
  6.     vector<vector<pair<int, int>>> element;
  7.     long capacity{};
  8.  
  9.     long hash(long key) const{
  10.         return abs(key) % capacity;
  11.     }
  12.     void init(int size){
  13.         this->element.resize(size);
  14.         capacity = size;
  15.     }
  16.     void put(long key, long value){
  17.         long hashed = hash(key);
  18.         if(!this->element[hashed].empty() && this->element[hashed][0].first == key){
  19.             this->element[hashed][0].second = value;
  20.         }else{
  21.             this->element[hashed].emplace_back(key, value);
  22.         }
  23.     }
  24.     long get(long key) const{
  25.         long hashed = hash(key);
  26.         if(this->element[hashed].empty()){
  27.             return -1;
  28.         }else if(this->element[hashed][0].first == key){
  29.             return this->element[hashed][0].second;
  30.         }else{
  31.             for(auto el : element[hashed]){
  32.                 if(el.first == key) return el.second;
  33.             }
  34.             return -1;
  35.         }
  36.     }
  37.     long delete_pair(int key){
  38.         long hashed = hash(key);
  39.         if(this->element[hashed].empty()){
  40.             return -1;
  41.         }
  42.         for(int i = 0; i < element[hashed].size(); i++){
  43.             if(element[hashed][i].first == key){
  44.                 int val = element[hashed][i].second;
  45.                 this->element[hashed].erase(element[hashed].begin() + i);
  46.                 return val;
  47.             }
  48.         }
  49.     }
  50. };
  51.  
  52.  
  53. int main(){
  54.     ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  55.     Hashmap hashmap;
  56.     int n;
  57.     cin >> n;
  58.     hashmap.init(n + 1);
  59.     vector<string> res;
  60.     while(n--){
  61.         string request;
  62.         cin >> request;
  63.         if(request == "get" || request == "delete"){
  64.             int key;
  65.             cin >> key;
  66.             int ans;
  67.             if(request == "get"){
  68.                 ans = hashmap.get(key);
  69.             }else{
  70.                 ans = hashmap.delete_pair(key);
  71.             }
  72.             if(ans == -1){
  73.                 res.emplace_back("None");
  74.             }else{
  75.                 res.push_back(to_string(ans));
  76.             }
  77.         }else if(request == "put"){
  78.             int key;
  79.             int value;
  80.             cin >> key >> value;
  81.             hashmap.put(key, value);
  82.         }
  83.     }
  84.     for(const auto& el : res){
  85.         cout << el << '\n';
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement