Cod sursa(job #1477533)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 august 2015 15:07:14
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.9 kb
#include <bits/stdc++.h>

using namespace std;

template <const size_t MAX_SIZE>
class LinearProbing
{
private:

    static const int NIL = -1;
    static const int DELETED = -2;

    int *table;

public:

    LinearProbing() : table(new int [MAX_SIZE]) {

        for (size_t i = 0; i < MAX_SIZE; ++i )
            table[i] = NIL;
    }

    ~LinearProbing()
    {
        delete[] table;
    }

    size_t h(const int key, const size_t i) const
    {
        return (hash<int>()(key) + i) % MAX_SIZE;
    }

    void insert(const int key)
    {
        size_t i = 0;
        size_t j;

        do
        {
            j = h(key, i);

            if (table[j] == NIL || table[j] == DELETED)
            {
                table[j] = key;
                break;
            }

            i++;

        } while (i < MAX_SIZE);
    }

    bool find(const int key) const
    {
        size_t i = 0;
        size_t j;

        do
        {
            j = h(key, i);

            if (table[j] == key)
                return true;

            i++;

        } while (table[j] != NIL && i < MAX_SIZE);

        return false;
    }

    void erase(const int key)
    {
        size_t i = 0;
        size_t j;

        do
        {
            j = h(key, i);

            if (table[j] == key)
            {
                table[j] = DELETED;
                break;
            }

            i++;

        } while (table[j] != NIL && i < MAX_SIZE);
    }
};

const int M = 1 << 21;

LinearProbing<M> L;

int main()
{
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");

    ios_base::sync_with_stdio( false );

    int N;
    in >> N;

    while ( N-- )
    {
        int tip, x;

        in >> tip >> x;

        switch(tip)
        {
            case 1: L.insert(x); break;
            case 2: L.erase(x); break;
            case 3: out << L.find(x) << "\n"; break;

            default: cerr << "Error\n";
        }
    }

    return 0;
}