Cod sursa(job #2788648)

Utilizator VladutzPredoiVlad Predoi VladutzPredoi Data 26 octombrie 2021 10:45:14
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 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 c;
vector <int> v[mod+5];

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

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

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

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

void solve()
{
    fin>>c;
    while(c--)
    {
        int cerinta, x;
        fin>>cerinta>>x;
        if(cerinta==1)
            adauga(x);
        if(cerinta==2)
            sterge(x);
        if(cerinta==3)
            afis(x);
    }
}

int main()
{
    solve();
    return 0;
}