Cod sursa(job #1324908)

Utilizator deea101Andreea deea101 Data 22 ianuarie 2015 22:03:42
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
using namespace std;
#define NULL 0
#define NMAX 666013

#include <vector>

class Hash
{
    private:
        vector <int> v[NMAX];
		int size;

	int hash(int x)
	{
		return (x%size);
	}
    public:
        Hash()
        {
            size=NMAX;
        }
        bool lookup(int x)
        {
            int h=hash(x),i;
            
			for(i=0;i<v[h].size();i++)
				if(v[h][i]==x) return 1;
			
			return 0;
		}
        
        void insert(int x)
        {
			if(lookup(x)) return; 
			
            int h=hash(x);
            v[h].push_back(x);
        }
        void del(int x)
        {
            int h=hash(x),i;
            for(i=0;i<v[h].size();i++)
				if(v[h][i]==x)
				{
					v[h][i]=v[h][v[h].size()-1];
					v[h].pop_back();
					return;
				}
        }
}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.del(x); break;
        case 3: g<<H.lookup(x)<<'\n'; break;
        }
    }
}