Cod sursa(job #1308949)

Utilizator deea101Andreea deea101 Data 4 ianuarie 2015 22:03:12
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <vector>
#define NMAX 666013
using namespace std;

class Hash
{
    private:
        vector <int> v[NMAX];
        int size;
		
		int hash(int key)
		{
			return key%size;
		}
    public:
        Hash()
        {
            size=NMAX;
        }
        void insert(int key)
        {
            int h=hash(key);
            for(int i=0;i<v[h].size();i++)
                if(v[h][i]==key) return;
            
            v[h].push_back(key);
        }
        bool lookup(int key)
        {
            int h=hash(key);
            for(int i=0;i<v[h].size();i++)
                if(v[h][i]==key) return 1;
            return 0;
        }
        void del(int key)
        {
            int h=hash(key);
            for(int i=0;i<v[h].size();i++)
                if(v[h][i]==key)
                {
                    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 N,q,x;
	f>>N;
	while(N--)
	{
		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;
		}
	}
}