Cod sursa(job #1070616)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 1 ianuarie 2014 17:55:29
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.85 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;

class INPUT {
  static const int BUFSIZE = 1<<16;
  static char buffer[];
  char *bufpos;
  char *bufend;
  void grabBuffer();
 public:
  INPUT() { grabBuffer(); }
  bool eof() { return bufend==buffer; }
  char nextChar() { return *bufpos; }
  inline char readChar();
  inline void skipWS();
  inline unsigned readUnsigned();
};

char INPUT::buffer[INPUT::BUFSIZE];

void INPUT::grabBuffer() {
  bufpos = buffer;
  bufend = buffer + fread( buffer, 1, BUFSIZ, stdin );
}

char INPUT::readChar() {
  char res = *bufpos++;
  if(bufpos==bufend) grabBuffer();
  return res;
}

inline bool myisspace(char c) { return c<=' '; }

void INPUT::skipWS() {
  while(!eof() && myisspace(nextChar())) readChar();
}

unsigned INPUT::readUnsigned() {
  skipWS();
  unsigned res = 0;
  while(!eof() && isdigit(nextChar())) {
    res = 10u * res + (readChar()-'0');
  }
  return res;
}

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

    INPUT input;

    N = input.readUnsigned();

    while ( N-- )
    {
        type = input.readUnsigned();
        key = input.readUnsigned();

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

    return 0;
}