Cod sursa(job #607614)

Utilizator andunhillMacarescu Sebastian andunhill Data 12 august 2011 20:03:49
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.08 kb
#include<fstream>
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

class list
{	int dim;
	struct nod
	{	int x;
		nod *next,*prev;
		nod(int val,nod *n,nod *p)
		{	x = val; next = n; prev = p; }
	};
	
	nod *first,*last;
	
public:
	
	list()
	{	dim = 0; first = last = NULL;
	}
	
	void make(int x)
	{	dim = 1;
		first = new nod(x,NULL,NULL);
		last = first;
	}
	
	void push(int x)
	{	if(dim == 0)
			make(x);
		else
		{	nod *p = new nod(x,NULL,last);
			last = p; dim++;
		}
	}
	
	void pop_front()
	{	if(dim == 1) 
		{	delete first; first = NULL;
		}
		else
		{	nod *p = first;
			first->next->prev = NULL;
			first = first->next; delete p;
		}
		dim--;
	}
	
	void pop_back()
	{	if(dim == 1) 
		{	delete first; first = NULL;
		}
		else
		{	nod *p = last;
			last->prev->next = NULL;
			last = last->prev;
			delete p;
		}
		dim--;
	}
	
	void pop(int x)
	{	nod *p;
		for(p = first; p; p++)
			if(p->x == x) break;
		if(p == NULL) return;
		if(p == first) pop_front();
		else if(p == last) pop_back();
		else
		{	p->prev->next = p->next;
			p->next->prev = p->prev;
			delete p; dim--;
		}
	}
	
	bool empty()
	{	return dim == 0;
	}
	
	bool find(int x)
	{	for(nod *p = first; p; p++)
			if(p->x == x) return 1;
		return 0;
	}
	
};

class Hash
{	
	int Hash_value;
	list *lists;
	
	int hash(int x)
	{	return x % Hash_value;
	}
	
public:
	
	Hash(int x)
	{	Hash_value = x;
		lists = new list[Hash_value];
	}
	
	void push(int x)
	{	int poz = hash(x);
		if(!lists[poz].empty() && lists[poz].find(x)) return;
		lists[poz].push(x);
	}
	
	void pop(int x)
	{	int poz = hash(x);
		if(!lists[poz].find(x)) return;
		lists[poz].pop(x);
	}
	
	bool find(int x)
	{	int poz = hash(x);
		return (!lists[poz].empty() && lists[poz].find(x));
	}
	
};

int N;
Hash H(1000003);

int main()
{	int i,x,op;
	f>>N;
	for(i = 1; i <= N; i++)
	{	f>>op>>x;
		if(op == 1) H.push(x);
		else if(op == 2) H.pop(x);
		else if(H.find(x)) g<<1<<'\n';
		else g<<0<<'\n';
	}
	f.close();
	g.close();
	return 0;
}