Cod sursa(job #2773590)

Utilizator iustin.pericicaPericica Iustin iustin.pericica Data 7 septembrie 2021 19:49:38
Problema Hashuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.69 kb
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

// https://www.geeksforgeeks.org/c-program-hashing-chaining/
class Hash
{
    int BUCKET;

    list<int> *table;
public:
    Hash(int V);
    void insertItem(int x);

    void deleteItem(int key);

    bool findItem(int key);

    int hashFunction(int x) {
        return (x % BUCKET);
    }

    void displayHash();
};

Hash::Hash(int b)
{
    this->BUCKET = b;
    table = new list<int>[BUCKET];
}

void Hash::insertItem(int key)
{
    int index = hashFunction(key);
    table[index].push_back(key);
}

void Hash::deleteItem(int key)
{
  int index = hashFunction(key);

  list <int> :: iterator i;
  for (i = table[index].begin();
           i != table[index].end(); i++) {
    if (*i == key)
      break;
  }

  if (i != table[index].end())
    table[index].erase(i);
}

bool Hash::findItem(int key){

    int index = hashFunction(key);
      list <int> :: iterator i;
      for (i = table[index].begin(); i != table[index].end(); i++) {
        if (*i == key)
          return true;
      }

    return false;

}

int main()
{
    int n;
    int operatie, x;
    fin>>n;
    Hash hashSet(666013);
    for(int i = 1;i<=n;++i){
        fin>>operatie>>x;
        if(operatie == 1 && !hashSet.findItem(x)){
            hashSet.insertItem(x);
        }
        else if(operatie == 2){
            hashSet.deleteItem(x);
        }
        else{
            if(!hashSet.findItem(x)){
                fout<<0<<"\n";
            }
            else fout<<1<<"\n";
        }
    }
    return 0;
}