Cod sursa(job #2741910)

Utilizator icnsrNicula Ionut icnsr Data 19 aprilie 2021 18:47:00
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.08 kb
#include <fstream>
#include <array>
#include <vector>
#include <algorithm>

class USet
{
public:
        void insert(const unsigned key)
        {
                const auto hash = get_hash(key);

                auto& tvec = buckets[hash];
                if(std::find(tvec.begin(), tvec.end(), key) == tvec.end())
                {
                        tvec.push_back(key);
                }
        }

        void erase(const unsigned key)
        {
                const auto hash = get_hash(key);

                auto& tvec = buckets[hash];
                const auto it = std::find(tvec.begin(), tvec.end(), key);
                if(it != tvec.end())
                {
                        tvec.erase(it);
                }
        }

        bool contains(const unsigned key)
        {
                const auto hash = get_hash(key);

                auto& tvec = buckets[hash];
                return std::find(tvec.begin(), tvec.end(), key) != tvec.end();
        }

        static constexpr unsigned SIZE = 49157;

private:
        unsigned get_hash(const unsigned key) const
        {
                return key % SIZE;
        }

private:
        std::array<std::vector<unsigned>, SIZE> buckets;
};

int main()
{
        std::ifstream fin("hashuri.in");
        std::ofstream fout("hashuri.out");

        unsigned N;
        fin >> N;

        USet uset;
        for(unsigned i = 0; i < N; ++i)
        {
                int op;
                unsigned key;
                fin >> op >> key;

                switch(op)
                {
                        case 1:
                        {
                                uset.insert(key);
                                break;
                        }
                        case 2:
                        {
                                uset.erase(key);
                                break;
                        }
                        case 3:
                        {
                                fout << uset.contains(key) << '\n';
                                break;
                        }
                }
        }
}