Cod sursa(job #1997879)

Utilizator Horia14Horia Banciu Horia14 Data 5 iulie 2017 18:04:01
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include<cstdio>
#define MOD 666013
using namespace std;

struct nod
{
    int val;
    nod* next;
};

nod* Hash[MOD];

inline int h(int x)
{
    return x % MOD;
}

bool Search(int x)
{
    nod* p = Hash[h(x)];
    while(p != NULL && p->val != x)
        p = p->next;
    if(p != NULL)
        return true;
    return false;
}

void Insert(int x)
{
    int key = h(x);
    nod* elem = new nod;
    elem->val = x;
    elem->next = Hash[key];
    Hash[key] = elem;
}

void Delete(int x)
{
    int key = h(x);
    nod* p = Hash[key];
    if(p->val == x)
        Hash[key] = p->next;
    else
    {
        while(p->next->val != x)
            p = p->next;
        p->next = p->next->next;
    }
}

int main()
{
    int n, i, op, x;
    FILE *fin, *fout;
    fin = fopen("hashuri.in","r");
    fout = fopen("hashuri.out","w");
    fscanf(fin,"%d",&n);
    for(i=0; i<n; i++)
    {
        fscanf(fin,"%d%d",&op,&x);
        if(op == 1)
        {
            if(!Search(x))
                Insert(x);
        }
        else if(op == 2)
        {
            if(Search(x))
                Delete(x);
        }
        else
        {
            if(Search(x))
                fprintf(fout,"1\n");
            else fprintf(fout,"0\n");
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}