Cod sursa(job #3246345)

Utilizator JohnnyFortaPascu Ioan JohnnyForta Data 2 octombrie 2024 19:30:30
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <vector>
#include <list>
#include <fstream>
#include <iostream>

class HashMap{
  private:
    std::vector<std::list<unsigned int>> map;

    int hash(unsigned int x){
      return x % 10'000'000;
    }

  public:
    HashMap() : map(10'000'000){}

    bool insert(unsigned int x){
      int hash_code = hash(x);
      map[hash_code].push_back(x);
      if(map[hash_code].size() == 1)
        return false;
      return true;      
    }

    bool find(unsigned int x){
      int hash_code = hash(x);
      for(auto it = map[hash_code].begin(); it != map[hash_code].end(); ++it){
          if(*it == x)
            return true;
      }
      return false;
    }

    void remove(unsigned int x){
      int hash_code = hash(x);
      for(auto it = map[hash_code].begin(); it != map[hash_code].end(); ++it){
          if(*it == x){
            map[hash_code].erase(it);
            return;
          }
      }
    }
};

int main(){
  std::ifstream in("hashuri.in");
  std::ofstream out("hashuri.out");
  int teste, num, op;
  HashMap my_map;
  
  in>>teste;
  for(int i = 0; i < teste; i++){
    in>>op>>num;
    if(op == 1){
      my_map.insert(num);
    }else if(op == 2){
      my_map.remove(num);
    }else{
      if(my_map.find(num))
        out<<"1\n";
      else
        out<<"0\n";
    }
  }
}