Cod sursa(job #797391)

Utilizator mihai995mihai995 mihai995 Data 13 octombrie 2012 22:28:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <vector>
using namespace std;

const int K = 666013;

struct HashTable{
    vector<int> hash[K];

    inline int h(int x){
        return x % K;
    }

    bool find(int x){
        vector<int>& v = hash[h(x)];

        for (vector<int> :: iterator it = v.begin() ; it != v.end() ; it++)
            if (*it == x)
                return true;
        return false;
    }

    void insert(int x){
        if (!find(x))
            hash[x % K].push_back(x);
    }

    void erase(int x){
        vector<int>& v = hash[x % K];

        for (vector<int> :: iterator it = v.begin() ; it != v.end() ; it++)
            if (*it == x){
                *it = v.back();
                v.pop_back();
                return;
            }
    }
};

HashTable H;

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

int main(){
    int t, q, x;

    in >> t;

    while (t--){
        in >> q >> x;

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

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

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

    return 0;
}