Cod sursa(job #1779322)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 15 octombrie 2016 03:50:41
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.07 kb
#include <bits/stdc++.h>

using namespace std;
class Hash{
private:
    vector<int> elements;
    vector<int> disp;
    vector<char> state;
    int dimension;

    int myHash(int key)
    {
        return (hash<int>()(key)) & (dimension - 1);
    }

    void naiveInsert(int position, int key, int value)
    {
        elements[position] = key;
        state[position] = 1;
        disp[position] = position - value;
    }

public:
    Hash(int hashSize)
    : elements(hashSize)
    , disp(hashSize)
    , state(hashSize)
    , dimension(hashSize)
    {
    }


    void insert(int key) {
        int value = myHash(key);
        int position = value - 1;
        while ( true ) {
            ++position;
            if (state[position] == 0) {
                naiveInsert(position, key, value);
                return; /// we managed to insert it;
            }

            if (state[position] == 1 && elements[position] == key) {
                return; /// the element exists
            }

            if (state[position] == 1 && disp[position] < position - key) {
                int auxKey = elements[position];
                int auxValue = position - disp[position];
                naiveInsert(position, key, value);
                key = auxKey;
                value = auxValue;
            }
        }
    }

    void remove(int key) {
        int value = myHash(key);
        int position = value - 1;
        while (true) {
            ++position;
            if (state[position] == 0) {
                return; /// the element does not exist
            }

            if (state[position] == 1 && elements[position] == key) {
                state[position] = 2;
                return;
            }
        }
    }

    bool search(int key) {
        int value = myHash(key);
        int position = value - 1;
        while (true) {
            ++position;
            if (state[position] == 0)
                return false;
            if (state[position] == 1 && elements[position] == key)
                return true;
        }
    }

};

#define DIM 666013
char buffer[DIM];
int poz = DIM - 1;

void scan(int &A)
{
    A = 0;
    while('0' > buffer[poz] || buffer[poz] > '9')
        if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
    while('0' <= buffer[poz] && buffer[poz] <= '9')
    {
        A = A * 10 + buffer[poz] - 48;
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    }
}

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

    Hash hashTable(1<<20);
    int op,x,N;
    scan(N);
    for (int i = 0; i < N; ++i) {
        scan(op);scan(x);
        if (op == 1) {
            if(hashTable.search(x))
                continue;
            hashTable.insert(x);
            continue;
        }
        if (op == 2) {
            if(!hashTable.search(x))
                continue;
            hashTable.remove(x);
            continue;
        }
        printf("%d\n",hashTable.search(x));
    }
    return 0;
}