Cod sursa(job #1082016)

Utilizator Theorytheo .c Theory Data 14 ianuarie 2014 01:04:42
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <fstream>
#include <vector>

using namespace std;

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

const double DIVIDE = 0.45453;

class QuadraticHashing{

    private:

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

        vector <unsigned int> H[NMAX];

        unsigned position(unsigned value) {

            return (unsigned)(((double)value * DIVIDE - (unsigned)((double)value * DIVIDE)) * NMAX);
        }

    public:

        void insert(unsigned value) {

            unsigned pos = position(value);
            H[pos].push_back(value);
        }


        bool find(unsigned value) {

            unsigned pos = position(value);

            for(unsigned i = 0 ; i < H[pos].size(); ++i)
                if(value == H[pos][i])
                    return true;

            return false;
        }


        void erase(unsigned value) {

            unsigned pos = position(value);

           for(unsigned i = 0 ;i < H[pos].size(); ++i)
                if(value == H[pos][i]) {

                    swap( H[pos][i], H[pos][ H[pos].size() - 1] );
                    H[pos].pop_back();
                }
        }
};

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;
}