Cod sursa(job #972129)

Utilizator mihai995mihai995 mihai995 Data 11 iulie 2013 01:18:07
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>
#include <set>
using namespace std;

const int inf = 0x3f3f3f3f;

template<const int Size>
class HashTable{
    int hash[Size];

    inline bool keepGoing(int poz, int val){
        return hash[poz] && hash[poz] != val;
    }

    int supposedPosition(int val){
        int poz = 0;

        while ( keepGoing( (val + poz * poz) % Size), val )
            ++poz;

        return (val + poz * poz) % Size;
    }

public:
    HashTable(){
        memset(hash, 0, sizeof(hash));
    }

    bool find(int val){
        return hash[ supposedPosition(val) ] == val;
    }

    void insert(int val){
        hash[ supposedPosition(val) ] = val;
    }

    void erase(int val){
        int poz = supposedPosition(val);

        if (hash[poz] == val)
            hash[poz] = inf;
    }
};

HashTable<3036643> H;

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

int main(){
    int times, tip, x;

    in >> times;

    while (times--){
        in >> tip >> x;

        if (tip == 1)
            H.insert(x);

        if (tip == 2)
            H.erase(x);

        if (tip == 3)
            out << H.find(x)  << "\n";
    }

    return 0;
}