Cod sursa(job #1322390)

Utilizator deea101Andreea deea101 Data 19 ianuarie 2015 23:58:18
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
using namespace std;
#define NMAX 4000000
class Hash
{
    private:
        struct node
        {
            int key,state;
            node()
            {
                state=0;
            }
        }v[NMAX];
        
        int size;
        int hash(int key)
        {
            return key%size;
        }
        
        int findBest(int h,int key)
        {
            while(v[h].state!=0 && !(v[h].key==key && v[h].state==1))
                h=(h+1)%size;
            return h;
        }

    public:
		Hash()
		{
			size=NMAX;
		}
        bool lookup(int key)
        {
            int h=hash(key);
            h=findBest(h,key);

            if(v[h].state==0)
                return 0;
            else return 1;
        }
        void insert(int key)
        {
            int h=hash(key);
            h=findBest(h,key);
            
            if(v[h].state==0)
            {
                v[h].key=key;
                v[h].state=1;
            }
        }
        void erase(int key)
        {
            int h=findBest(hash(key),key);
            if(v[h].state!=0)
                v[h].state=-1;
        }
}H;

#include <fstream>
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int main()
{
    int T;
    f>>T;
     
    int q,x;
    while(T--)
    {
        f>>q>>x;
        switch (q)
        {
        case 1: H.insert(x); break;
        case 2: H.erase(x); break;
        case 3: g<<H.lookup(x)<<'\n'; break;
        }
    }
}