Cod sursa(job #1193280)

Utilizator heracleRadu Muntean heracle Data 31 mai 2014 13:35:42
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <cstdio>

template  <unsigned int KEY ,unsigned int nrelem , class T = unsigned int> struct hash
{
    unsigned int urm[nrelem+1];
    unsigned int lst[KEY];
    T val[nrelem+1];
    int nr;

    bool contains(T x)
    {
    /*
        int p=lst[x%KEY];
        while(p!=0)
        {
            if(val[p]==x)
                return 1;
            p=urm[p];
        }
        return 0;

        */

        int p=lst[x&(KEY-1)];
    while(p!=0 && val[p]!=x)
        p=urm[p];
    return p!=0;
    }

    void clean()
    {
        for(int i=0; i<KEY; i++)
            lst[i]=0;
        nr=0;
    }

    void add(T x)
    {
        int p=x%KEY;
        nr++;
        val[nr]=x;
        urm[nr]=lst[p];
        lst[p]=nr;
    }

    void sterge(T x)
    {
        int p=lst[x%KEY];

        if(x==val[p])
        {
            lst[x%KEY]=urm[p];
            return;
        }
        while(p!=0 && val[urm[p]]!=x)
        {
            p=urm[p];
        }
        if(val[p]==x)
        {
            urm[p]=urm[urm[p]];
        }


    }
    void sterge_tot(T x)
    {
        int p=lst[x%KEY];

        if(x==val[p])
        {
            lst[x%KEY]=urm[p];
            p=urm[p];
        }

        while(p!=0)
        {
            if(val[urm[p]]==x)
            {
                urm[p]=urm[urm[p]];
            }
            p=urm[p];
        }
    }

};

FILE* in;
FILE* out;

hash< 1<<20 , 1000003 > h;

int main()
{
    in=fopen("hashuri.in","r");
    out=fopen("hashuri.out","w");

    int t;

    fscanf(in,"%d",&t);

    int g,x;

    for(int i=1; i<=t; i++)
    {
        fscanf(in,"%d %d",&g,&x);

        if(g==1)
        {
            if(h.contains(x)==0)
                h.add(x);
        }
        if(g==2)
        {
            h.sterge(x);
        }
        if(g==3)
        {
            fprintf(out,"%d\n",h.contains(x));
        }

    }


    return 0;
}