Cod sursa(job #2160202)

Utilizator jeanFMI - Petcu Ion Cristian jean Data 11 martie 2018 12:03:37
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

class MyHash {
    private:
        int MOD;
        vector<vector<int> > H;
        int hash(int x);
        vector<int>::iterator search(int x);
    public:
        MyHash();
        void insert(int x);
        void erase(int x);
        bool exists(int x);
};

MyHash::MyHash() {
    MOD = 666013;
    H.clear();
    H.resize(MOD);
}

int MyHash::hash(int x) {
    return x % MOD;
}

vector<int>::iterator MyHash::search(int x) {
    vector<int> &list = H[hash(x)];
    return find(list.begin(), list.end(), x);
}

void MyHash::insert(int x) {
    if (!exists(x)) {
        H[hash(x)].push_back(x);
    }
}

void MyHash::erase(int x) {
    int h = hash(x);
    vector<int>::iterator it = search(x);
    if (it != H[h].end()) {
        H[h].erase(it);
    }
}

bool MyHash::exists(int x) {
    return search(x) != H[hash(x)].end();
}

int main() {
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int N, op, x;
    MyHash hash;

    for (cin >> N; N; N--) {
        cin >> op >> x;
        if (op == 1) hash.insert(x);
        else if (op == 2) hash.erase(x);
        else cout << hash.exists(x) << endl;
    } 

    return 0;
}