Cod sursa(job #1765631)

Utilizator tiberiu.bucur17Tiberiu Constantin Emanoil Bucur tiberiu.bucur17 Data 26 septembrie 2016 21:22:10
Problema Hashuri Scor 100
Compilator c Status done
Runda cerculdeinfo-lectia1-hashuri.rabinkarp Marime 1.05 kb
#include <stdio.h>
#define MAX_N 1000001
#define MOD 666013
int val[MAX_N],next[MAX_N],hash[MOD];
inline int caut(int x)
{
    int p=hash[x%MOD];
    while(p && val[p]!=x)
        p=next[p];
    return (p>0);
}
inline void add(int x,int p)
{
    if(!caut(x))
    {
        val[p]=x;
        next[p]=hash[x%MOD];
        hash[x%MOD]=p;
    }
}
inline void sterg(int x)
{
    int p=hash[x%MOD];
    if(val[p]==x)
        hash[x%MOD]=next[hash[x%MOD]];
    else
    {
        while(next[p] && val[next[p]]!=x)
            p=next[p];
        if(next[p])
            next[p]=next[next[p]];
    }
}
int main()
{
    FILE *fin,*fout;
    fin=fopen("hashuri.in","r");
    fout=fopen("hashuri.out","w");
    int n,i,val,x;
    fscanf(fin,"%d",&n);
    for(i=1;i<=n;i++)
    {
        fscanf(fin,"%d%d",&val,&x);
        switch(val)
        {
            case 1: add(x,i);break;
            case 2: sterg(x);break;
            default: fprintf(fout,"%d\n",caut(x));
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}