Cod sursa(job #922298)

Utilizator aladinaladin aladinn aladin Data 22 martie 2013 02:57:59
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.09 kb
#include <cstdio>
#include <tr1/functional>
#include <algorithm>
#include <string>
#include <iostream>
#define MOD 1048576
using std::string;
using std::cin;

template <class tip>
class Hash
{
  public:
    Hash()    {
        hh = new tip[MOD+1]();
        ok = new char[MOD + 1]();
    }

    ~Hash() {delete[] hh; delete[] ok;}

    //inline Hash& operator += (int&);
    //inline Hash& operator -= (int&);
    inline Hash& operator+=(const tip &val)
    {
        insert(val);
        return *this;
    }

    inline Hash& operator-=(const tip &val)
    {
        del(val);
        return *this;
    }

    inline int newpoz(int poz)
    {
        return (poz*5+1)&MOD;
    }

    inline int insert(const tip &x)
    {
        unsigned int poz=hashff(x) & MOD ,steps=0;
        while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
            poz=newpoz(poz),++steps;
        if (steps>=MOD)
            return 0;
        hh[poz]=x;
        ok[poz]=1;
        return poz;
    }

    inline unsigned int del(const tip &x)
    {
        unsigned int poz=hashff(x) & MOD,steps=0;
        while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
            poz=newpoz(poz),++steps;
        if (steps>=MOD)
            return 0;
        if (hh[poz]!=x) return -1;
        ok[poz]=-1;
        return poz;
    }

    inline unsigned int getpoz(const tip &x)
    {
        unsigned int poz=hashff(x) & MOD, steps = 0;
        while ((ok[poz]!=0) && (ok[poz]==-1 || hh[poz]!=x) && (steps<MOD))
            poz=newpoz(poz),++steps;
        if (hh[poz]!=x) return 0;
        return poz;
    }



  private:
    tip *hh;
    char *ok;

   std::tr1::hash<tip> hashff;
};


int main()
{
	int t,x,tip;
	Hash<string> hass;
	string k;

	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	//scanf("%d",&t);
	cin>>t;
	while (t--)
	{
	    cin>>tip>>k;
		//scanf("%d %s",&tip,&k);
		if (tip==1)
            hass+=k;
		else
			if (tip==2) hass-=k; else
				printf("%d\n",hass.getpoz(k)>0);
	}
	return 0;
}