Cod sursa(job #2773594)

Utilizator iustin.pericicaPericica Iustin iustin.pericica Data 7 septembrie 2021 20:19:36
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.66 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
{

    vector<unsigned long long> table[666013];
public:
    void insertItem(unsigned long long x);

    void deleteItem(unsigned long long key);

    bool findItem(int x,unsigned long long key);

    int hashFunction(unsigned long long x) {
        return (x % 666013);
    }

};

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

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

    for(int i = 0; i < table[index].size(); i++){
        if (table[index][i] == key)
          table[index].erase(table[index].begin() + i);
          break;
    }
}

bool Hash::findItem(int x, unsigned long long key){
    unsigned long long index = hashFunction(key);

    for(int i = 0; i < table[index].size(); i++){
        if (table[index][i] == key)
          return true;
    }

}

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