Cod sursa(job #2888164)

Utilizator ana_madalina_18Radu Ana Madalina ana_madalina_18 Data 10 aprilie 2022 19:02:17
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
const int P=666013;
int base=97;
vector <int> H[P+5];
int Hash(int x)
{
    int rasp=0, p=1;
    while(x!=0)
    {
        int cif=x%10;
        x=x/10;
        rasp=rasp+cif*p;
        rasp=rasp%P;
        p=p*base;
        p=p%P;
    }
    return rasp;
}
int main()
{
    int n;
    fin>>n;
    for(int op=1;op<=n;op++)
    {
        int oper, x;
        fin>>oper>>x;
        int nr_hash=Hash(x);
        auto it = find(H[nr_hash].begin(), H[nr_hash].end(), x);
        if(oper==1 && it==H[nr_hash].end())
        {
            H[nr_hash].push_back(x);
        }
        if(oper==2 && it!=H[nr_hash].end())
        {
            H[nr_hash].erase(it);
        }
        if(oper==3)
        {
            if(it!=H[nr_hash].end())
            {
                fout<<1<<'\n';
            }
            else
            {
                fout<<0<<'\n';
            }
        }
    }
    return 0;
}