Cod sursa(job #2819417)

Utilizator matei.tudoseMatei Tudose matei.tudose Data 18 decembrie 2021 12:23:57
Problema Hashuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 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);
        if (op == 1)
            Hash[hashul].push_back(numar);
        else if (op == 2)
        {
            auto it = find(Hash[hashul].begin(), Hash[hashul].end(), numar);
            if (it != Hash[hashul].end())
                Hash[hashul].erase(it);
        }
        else
        {
            bool found = false;
            for (int j = 0; j < Hash[hashul].size(); j++)
            {
                if (Hash[hashul][j] == numar)
                {
                    fout << "1\n";
                    found = true;
                }
            }
            if (!found)
                fout << "0\n";
        }
    }
    return 0;
}