Cod sursa(job #1081981)

Utilizator Theorytheo .c Theory Data 14 ianuarie 2014 00:14:14
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <fstream>

using namespace std;

ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");

class QuadraticHashing{

    private:

        static const unsigned MASK = (1 << 21) - 1;
        static const unsigned C1 = 1;
        static const unsigned C2 = 0;
        static const unsigned MAX_COLLISIONS = 5;

        unsigned H[MASK + 1];

        unsigned position(unsigned value, unsigned pos) {

            return (value + C1 * pos * pos + C2 * pos) & MASK;
        }

    public:

        void insert(unsigned value) {

            unsigned pos = 0;
            unsigned processed = 0;

            do{
                processed = position(value, pos);
                if( H[processed] == 0 )
                    H[processed] = value;
                else ++pos;


            }while(H[processed] != value);
        }


        bool find(unsigned value) {

            unsigned pos = 0;

            do{
                unsigned processed = position(value, pos);
                if( H[processed] == value )
                    return true;
                else ++pos;


            }while(pos < MAX_COLLISIONS);

            return false;
        }


        void erase(unsigned value) {

            unsigned pos = 0;

            do{
                unsigned processed = position(value, pos);
                if( H[processed] == value )
                    H[processed] = 0;
                else ++pos;


            }while(pos < MAX_COLLISIONS);
        }
};

QuadraticHashing QH;


int main() {

    int N; int key; int type; fin >> N;

    while( N-- ) {

        fin >> type >> key;

        switch(type) {

            case 1: QH.insert(key); break;
            case 2: QH.erase(key); break;
            case 3: fout << QH.find(key) << '\n'; break;
        }
    }

    return 0;
}