Cod sursa(job #2783749)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 14 octombrie 2021 23:29:32
Problema Hashuri Scor 100
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");

const int mod=666013;
const int base=89;
int operatii;
vector <int> v[mod+5];

int hesh(int n)
{
    long long h=0, p=1;
    while(n)
    {
        h += p * (n % 10);
        h %= mod;
        n /= 10;
        p *= base;
    }
    return h;
}

void inserthash(int n)
{
    int h = hesh(n);
    vector <int> :: iterator it = find(v[h].begin(), v[h].end(), n);
    if(it != v[h].end() && !v[h].empty())
        return;
    v[h].push_back(n);
}

void deletehash(int n)
{
    int h = hesh(n);
    vector <int> :: iterator it = find(v[h].begin(), v[h].end(), n);
    if(it != v[h].end() && !v[h].empty())
        v[h].erase(it);
}

void findhash(int n)
{
    int h = hesh(n);
    vector <int> :: iterator it = find(v[h].begin(), v[h].end(), n);
    if(it != v[h].end() && !v[h].empty())
        fout<<1<<'\n';
    else
        fout<<0<<'\n';
}

void solve()
{
    int cerinta, x;
    fin>>cerinta>>x;
    if(cerinta==1)
        inserthash(x);
    if(cerinta==2)
        deletehash(x);
    if(cerinta==3)
        findhash(x);
}

int main()
{
    fin>>operatii;
    while(operatii--)
        solve();
    return 0;
}