Cod sursa(job #1804631)

Utilizator TudorVersoiuVersoiu Tudor Sorin TudorVersoiu Data 12 noiembrie 2016 20:30:41
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <cstring>


#define HASH_SIZE 10000000

using namespace std;
ifstream f("hashuri.in" );
ofstream g("hashuri.out");

int hashFunction(int x) {
    return x/10000000;
}

struct HASH_OBJECT {
    int *table;

    HASH_OBJECT()
    {
        table = new int[HASH_SIZE];
        memset(table, 0, sizeof HASH_SIZE);
    }
    ~HASH_OBJECT()
    {
        delete[] table;
    }

    void addElem(int x) {
        int poz = hashFunction(x);

        while ( table[poz] ) ++poz;

        table[poz] = x;
    }
    void delElem(int x) {
        int poz = hashFunction(x);

        while ( table[poz] != x && table[poz] ) ++poz;

        if ( table[poz] == x ) table[poz] = 0;
    }

    bool queryElem(int x) {
        int poz = hashFunction(x);

        while ( table[poz] != x && table[poz] ) ++poz;

        if ( table[poz] != x )
        {
            g << 0 << '\n';
            return 0;
        }

        g << 1 << '\n';
        return 1;
    }
};


int NrOperatii;

int main()
{
    f >> NrOperatii; int op, x;


    HASH_OBJECT tabel1;


    for ( int i=1; i<=NrOperatii; i++ )
    {
        f >> op >> x;

        switch ( op )
        {
            case 1: tabel1.addElem(x);     break;
            case 2: tabel1.delElem(x);     break;
            case 3: tabel1.queryElem(x);   break;
        }
    }
}