Cod sursa(job #1330830)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 31 ianuarie 2015 00:10:34
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include <fstream>
#include <vector>

using namespace std;

class Hash {

    private:
        struct Node {
            int key, state;
            Node() {
                state = 0;
            }
        };

        const int size = 2000100;
        Node A[2000100];

        int findSlot(int key) {

            int i = key % size;

            while(A[i].state != 0 && A[i].key != key) {
                i++;
                if(i == size)
                    i = 0;
            }

            return i;
        }

    public:
        void insert(int key) {

            int i = key % size;
            while(A[i].state == 1 && A[i].key != key) {
                i++;
                if(i == size)
                    i = 0;
            }

            if(A[i].state != 1) {
                A[i].key = key;
                A[i].state = 1;
            }
        }

        bool search(int key) {
            return (A[findSlot(key)].key == key);
        }

        void remove(int key) {

            int position = findSlot(key);

            if(A[position].state != 0) {
                A[position].key = -1;
                A[position].state = -1;
            }
        }
} h;

int main() {

    int type, x, queries;
    ifstream in("hashuri.in");
    ofstream out("hashuri.out");

    in >> queries;
    while(queries--) {

        in >> type >> x;

        switch(type) {
            case 1: h.insert(x); break;
            case 2: h.remove(x); break;
            case 3: out << h.search(x) << '\n';
            }

        }

    in.close();
    out.close();

    return 0;

}