Cod sursa(job #2369168)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 5 martie 2019 21:32:55
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>
using namespace std;

#define FILE_NAME "hashuri"
ifstream in (FILE_NAME".in");
ofstream out(FILE_NAME".out");

const int HASH = 666013;
vector < int > HashTable[HASH];
int N;

bool checkHash(int x)
{
    int hashed = x % HASH;
    for(auto it : HashTable[hashed])
        if (it == x)
            return true;

    return false;
}

void addHash(int x)
{
    int hashed = x % HASH;
    if (!checkHash(x))
        HashTable[hashed].push_back(x);
}

void removeHash(int x)
{
    int hashed = x % HASH;
    if (!HashTable[hashed].empty())
    {
        for (unsigned i = 0; i < HashTable[hashed].size(); ++i)
            if (HashTable[hashed][i] == x)
            {
                swap(HashTable[hashed][i], HashTable[hashed][HashTable[hashed].size()-1]);
                HashTable[hashed].pop_back();
                break;
            }
    }
}

int main()
{
    in >> N;
    while(N--)
    {
        int op, x;
        in >> op >> x;
        if (op == 1)
            addHash(x);
        else if (op == 2)
            removeHash(x);
        else if (op == 3)
            out << checkHash(x) << '\n';
        else return -1;
    }

    return 0;
}