Advertisement
Goga21

Untitled

Mar 31st, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | Source Code | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define FILL_FACTOR 75
  6.  
  7. struct Item{
  8.     string key;
  9.     int value;
  10. };
  11.  
  12. struct Hashmap{
  13.     Item* element{};
  14.     int capacity;
  15.     int count = 0;
  16.  
  17.     long hash(string key) const{
  18.         long sum = 0;
  19.         for (int i = 0; i < key.size(); ++i) {
  20.             sum += key[i] * (i + 1);
  21.         }
  22.         return sum;
  23.     }
  24.     void init(int size){
  25.         this->element = (Item*)malloc(size * sizeof(Item));
  26.     }
  27.     void put(const string& key, int value){
  28.         long hashed = hash(key);
  29.         if(this->element[hashed].key == key){
  30.             this->element[hashed].value = value;
  31.         }else if(!this->element[hashed].key.empty()){
  32.             long temp = hashed + 1;
  33.             while(!this->element[temp].key.empty()){
  34.                 temp++;
  35.             }
  36.             this->element[temp].key = key;
  37.             this->element[temp].value = value;
  38.         }else{
  39.             this->element[hashed].key = key;
  40.             this->element[hashed].value = value;
  41.         }
  42.         this->count++;
  43.     }
  44.     int get(string key) const{
  45.         long hashed = hash(key);
  46.         if(this->element[hashed].key == key){
  47.             return this->element[hashed].value;
  48.         }else{
  49.             for(int i = hashed + 1; i < count; i++){
  50.                 if(i == count - 1){
  51.                     for(int j = 0; j < hashed; j++){
  52.                         if(this->element[j].key == key) return this->element[j].value;
  53.                     }
  54.                     break;
  55.                 }
  56.                 if(this->element[i].key == key) return this->element[i].value;
  57.             }
  58.             return -1;
  59.         }
  60.     }
  61.     int delete_key(string key){
  62.         int hashed = hash(key);
  63.         if(this->element[hashed].key.empty()){
  64.             return -1;
  65.         }
  66.         int val = this->element[hashed].value;
  67.         this->element[hashed].key = "";
  68.         this->element[hashed].value = 0;
  69.         this->capacity--;
  70.         return val;
  71.     }
  72. };
  73.  
  74. int main(){
  75.     Hashmap hashmap;
  76.     int n;
  77.     cin >> n;
  78.     hashmap.init(n);
  79.     while(n--){
  80.         string request;
  81.         cin >> request;
  82.         if(request == "get" || request == "delete"){
  83.             string key;
  84.             cin >> key;
  85.             int ans;
  86.             if(request == "get"){
  87.                 ans = hashmap.get(key);
  88.             }else{
  89.                 ans = hashmap.delete_key(key);
  90.             }
  91.             if(ans == -1){
  92.                 cout << "None\n";
  93.             }else{
  94.                 cout << ans << '\n';
  95.             }
  96.         }else if(request == "put"){
  97.             string key;
  98.             int value;
  99.             cin >> key >> value;
  100.             hashmap.put(key, value);
  101.         }
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement