Cod sursa(job #2670253)

Utilizator Florinos123Gaina Florin Florinos123 Data 9 noiembrie 2020 15:18:41
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
#include <vector>

using namespace std;

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

int n;

int cerinta, val;

int aux[100005];

const int mod = 100003;

vector < int > v[100005];

int Hash (int x)
{
    int rez = x % mod;
    return rez;
}

void Add (int x)
{
    int cod = Hash(x);
    bool gasit = false;

    for (int i=0; i<v[cod].size() && !gasit; i++)
    {
        if (v[cod][i] == x)
            gasit = true;
    }

    if (!gasit) v[cod].push_back(x);
}

void Delete (int x)
{
    int cod = Hash(x);
    int k = 0;

    for (int i=0; i<v[cod].size(); i++)
    {
        if (v[cod][i] != x)
        {
            k ++, aux[k] = v[cod][i];
        }
    }

    v[cod].clear();

    for (int i=1; i<=k; i++)
        v[cod].push_back(aux[i]);
}

bool Search (int x)
{
    int cod = Hash(x);

    for (int i=0; i<v[cod].size(); i++)
    {
        if (v[cod][i] == x)
            return true;
    }
    return false;
}

int main()
{
    f >> n;
    for (int i=1; i<=n; i++)
    {
        f >> cerinta >> val;

        if (cerinta == 1) Add(val);

        if (cerinta == 2) Delete(val);

        if (cerinta == 3) g << Search(val) << "\n";
    }
    return 0;
}