Cod sursa(job #2380487)

Utilizator dan.ghitaDan Ghita dan.ghita Data 15 martie 2019 01:47:14
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.18 kb
#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;
 
ifstream f("hashuri.in");
ofstream g("hashuri.out");

int n, q, x;
const int MOD = 666013;

class Node
{
public:
    int Val;
    Node* Next;

    Node(int x) : Val(x), Next(nullptr)
    {
    }
};

class Hash
{
private:
    vector<Node*> hash;

    int hashFunc(int x)
    {
        return x % MOD;
    }
public:
    Hash()
    {
        hash.resize(MOD + 5, nullptr);
    }

    void insert(int x)
    {
        int index = hashFunc(x);

        Node* currentNode = hash[index];
        if (!currentNode)
            hash[index] = new Node(x);
        else
        {
            while (currentNode->Next)
                if (currentNode->Next->Val == x)
                    return;
                else
                    currentNode = currentNode->Next;

            currentNode->Next = new Node(x);
        }
    }

    void remove(int x)
    {
        int index = hashFunc(x);

        Node* currentNode = hash[index];
        if (!currentNode)
            return;
        else 
            if (currentNode->Val == x)
            {
                hash[index] = currentNode->Next;
                delete currentNode;
            }
            else
                for (Node* next = currentNode->Next; next; currentNode = next, next = next->Next)
                    if (next->Val == x)
                    {
                        currentNode->Next = next->Next;
                        delete next;
                        return;
                    }
    }

    bool find(int x)
    {
        int index = hashFunc(x);
        for (Node* currentNode = hash[index]; currentNode; currentNode = currentNode->Next)
            if (currentNode->Val == x)
                return true;

        return false;
    }
};


int main()
{
    f >> n;

    Hash hash;
    while (n--)
    {
        f >> q >> x;
        switch (q)
        {
        case 1:
            hash.insert(x);
            break;
        case 2:
            hash.remove(x);
            break;
        case 3:
            g << hash.find(x) << '\n';
        }
    }
    return 0;
}