Cod sursa(job #1185102)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 14 mai 2014 23:32:14
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <iostream>
#include <fstream>

using namespace std;



class LinearProbing
{
    private:

        static const unsigned NR = 21;
        static const unsigned MASK = ( 1 << 21 );

        unsigned HT[MASK];

        int valid( unsigned pos, unsigned value )
        {
            return HT[pos] && HT[pos] != value;
        }

        unsigned position( unsigned value )
        {
            unsigned pos = 0;

            while ( valid( (value + pos) & ( MASK - 1 ), value ) ) pos++;

            return (value + pos) & ( MASK - 1 );
        }

    public:

        void insert( unsigned value )
        {
            HT[ position( value ) ] = value;
        }

        bool find( unsigned value )
        {
            return HT[ position( value ) ] == value;
        }

        void erase( unsigned value )
        {
            unsigned pos = position( value );

            if ( HT[pos] == value ) HT[pos] = -1;
        }
};

int N;
unsigned key, type;

const int DIM_BUFF = ( 1 << 20 );

char buffer[DIM_BUFF];
int position1 = DIM_BUFF;

char GetChar()
{
    if ( position1 == DIM_BUFF )
    {
        fread( buffer, 1, DIM_BUFF, stdin );
        position1 = 0;
    }

    return buffer[ position1++ ];
}

unsigned rd()
{
    unsigned nr = 0;
    char c;

    do
    {
        c = GetChar();

    } while ( !isdigit( c ) );

    do
    {
        nr = nr * 10 + ( c - '0' );
        c = GetChar();

    } while ( isdigit( c ) );

    return nr;
}

LinearProbing HT;

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    N = rd();

    while ( N-- )
    {
        type = rd(); key = rd();

        if ( type == 1 ) HT.insert( key );
        if ( type == 2 ) HT.erase( key );
        if ( type == 3 ) printf("%d\n", HT.find( key ) );
    }

    return 0;
}