Cod sursa(job #1093293)

Utilizator mihai995mihai995 mihai995 Data 27 ianuarie 2014 21:36:58
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
using namespace std;

#define NIL 0
#define DELETED -1

template<const int Size, const int mod>
class HashTable{
    int table[Size], pos, inc;

    void lookup(int x){
        pos = x % Size;
        inc = 1 + x % mod;

        while (table[pos] != x && table[pos] != NIL){
            pos += inc;
            if (Size <= pos) pos -= Size;
        }
    }

public:
    void insert(int x){
        lookup(x);
        table[pos] = x;
    }

    void erase(int x){
        lookup(x);
        if (table[pos] == x) table[pos] = DELETED;
    }

    bool find(int x){
        lookup(x);
        return table[pos] == x;
    }
};

HashTable<1046527, 998077> H;

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

int main(){
    int nrE, type, x;

    in >> nrE;
    while (nrE--){
        in >> type >> x;
        if (type == 1) H.insert(x);
        if (type == 2) H.erase(x);
        if (type == 3) out << H.find(x) << "\n";
    }

    return 0;
}