Cod sursa(job #972124)

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

class Vector{
    vector<int> v;

public:
    void insert(int x){
        v.push_back(x);
    }

    int find(int x){
        for (size_t i = 0 ; i < v.size() ; i++)
            if (v[i] == x)
                return i;

        return -1;
    }

    void erase(int poz){
        if (poz == -1)
            return;
        v[poz] = v.back();
        v.pop_back();
    }

    int end(){
        return -1;
    }
};

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

public:
    void insert(int x){
        if (!find(x))
            hash[x % Size].insert(x);
    }

    void erase(int x){
        if (find(x))
            hash[x % Size].erase( hash[x % Size].find(x) );
    }

    bool find(int x){
        return hash[x % Size].find(x) != hash[x % Size].end();
    }
};

HashTable<set<int>, 1> 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;
}