Cod sursa(job #2397444)

Utilizator kywyPApescu tiGEriu kywy Data 4 aprilie 2019 13:31:32
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include<cstdio>
#include<vector>
#define MOD 666013
#define NR 29
using namespace std;

FILE* in=fopen("hashuri.in", "r");
FILE* out=fopen("hashuri.out", "w");

vector<int> hashuri[MOD+9];
int cautare(int nr);
int hashh(int nr);

void del(int nr)
{
    int poz=cautare(nr);
    if(poz!=-1)
    {
        hashuri[hashh(nr)].erase(hashuri[hashh(nr)].begin()+poz);
    }
}

void add(int nr)
{
    if(cautare(nr)==-1) hashuri[hashh(nr)].push_back(nr);
}

int hashh(int nr)
{
    int haesh=1;
    while(nr)
    {
        haesh=haesh*NR+nr%10;
        haesh%=MOD;
        nr/=10;
    }
    return haesh;
}

int cautare(int nr)
{
    int haesh=hashh(nr);
    for(int i=0; i<hashuri[haesh].size(); ++i)
    {
        if(hashuri[haesh][i]==nr) {return i;}
    }
    return -1;
}

int main()
{
    int n;
    fscanf(in, "%d", &n);
    for(int i=1; i<=n; ++i)
    {
        int x, y;
        fscanf(in, "%d%d", &x, &y);
        if(x==1)
        {
            add(y);
            //printf("%d ", hashh(y));
            //;printf("%d >",cautare(y));
        }
        if(x==2)
        {
            del(y);
        }
        if(x==3)
        {
            if(cautare(y)!=-1)
            {
                fprintf(out, "1\n");
            }
            else fprintf(out, "0\n");
        }
    }
}