Cod sursa(job #2819418)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 18 decembrie 2021 12:28:09
Problema Hashuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

// 651097 si 699401
const int mod = 651097;
int base = 97, N;
vector<int> Hash[mod + 5];

int get_hash(int modulo, int baza, int numar)
{
    int hash = 0, contor = 1;
    while (numar)
    {
        int cif = numar % 10;
        hash += (cif * contor) % modulo;
        contor *= baza;
        contor = contor % modulo;
        numar /= 10;
    }
    return hash;
}

int main()
{
    fin >> N;
    for (int i = 0; i < N; i++)
    {
        int op, numar, hashul;
        fin >> op >> numar;
        hashul = get_hash(mod, base, numar);
        auto it = find(Hash[hashul].begin(), Hash[hashul].end(), numar);
        if (op == 1 && it == Hash[hashul].end())
            Hash[hashul].push_back(numar);
        else if (op == 2 && it != Hash[hashul].end())
            Hash[hashul].erase(it);
        else if (op == 3)
        {
            if (it != Hash[hashul].end())
                fout << "1\n";
            else
                fout << "0\n";
        }
    }
    return 0;
}