Cod sursa(job #2617705)

Utilizator hirneagabrielHirnea Gabriel hirneagabriel Data 22 mai 2020 17:18:50
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
class Hash
{
    int nr;
    vector<int>* tabel;
public:
    Hash(int);
    void Insert(int);
    void Delete(int);
    int Find(int);
    int hashFuction(int x)
    {
        return x % nr;
    }
};
Hash::Hash(int x)
{
    nr = x;
    tabel = new vector<int>[nr];
}

void Hash::Insert(int x)
{
    int i = hashFuction(x);
    tabel[i].push_back(x);
}

void Hash::Delete(int x)
{
    int i = hashFuction(x);
    vector <int>::iterator j;
    for (j = tabel[i].begin(); j != tabel[i].end(); i++)
        if (*j == x)
            break;
    if (j != tabel[i].end())
        tabel[i].erase(j);
}

int Hash::Find(int x)
{
    int i = hashFuction(x);
    vector <int>::iterator j;
    for (j = tabel[i].begin(); j != tabel[i].end(); i++)
        if (*j == x)
            return 1;
    return 0;
}
int main() {
    int N, x, y;
    Hash ob(700000);
    fin >> N;
    while (N)
    {
        fin >> x;
        if (x == 1)
        {
            fin >> y;
            ob.Insert(y);
        }
        if (x == 2)
        {
            fin >> y;
            ob.Delete(y);
        }
        if (x == 3)
        {
            fin >> y;
            fout << ob.Find(y) << "\n";
        }
        N--;
    }
    return 0;
}