Cod sursa(job #1755389)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 10 septembrie 2016 01:39:42
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <stdio.h>
#include <bits/stdc++.h>

using namespace std;

template<class T>
class HashSet {
    public:
        HashSet() {
            kMod = kHashPrimes[rand() % 3];
            hash_.resize(kMod);
        }

        void Insert(T value) {
            int bucket = value % kMod;
            for (T current_value : hash_[bucket]) {
                if (current_value == value) {
                    return;
                }
            }
            hash_[bucket].push_back(value);
        }

        void Remove(T value) {
            int bucket = value % kMod;
            for (T& current_value : hash_[bucket]) {
                if (current_value == value) {
                    swap(current_value, hash_[bucket].back());
                    hash_[bucket].pop_back();
                    return;
                }
            }
        }

        bool Find(T value) {
            int bucket = value % kMod;
            for (T current_value : hash_[bucket]) {
                if (current_value == value) {
                    return true;
                }
            }
            return false;
        }

    private:
        const int kHashPrimes[3] = {98317, 196613, 393241};
        int kMod;
        vector<vector<T>> hash_;

};

int main() {
    srand(time(0));
    cin.sync_with_stdio(false);

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

    HashSet<int> hash_set;

    int t;
    scanf("%d", &t);

    for (; t; t--) {
        int operation;
        int value;

        scanf("%d%d", &operation, &value);
        switch (operation) {
        case 1:
            hash_set.Insert(value);
            break;
        case 2:
            hash_set.Remove(value);
            break;
        default:
            printf("%d\n", hash_set.Find(value));
        }
    }

    return 0;
}