Cod sursa(job #299655)

Utilizator laserbeamBalan Catalin laserbeam Data 6 aprilie 2009 22:02:26
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
/* BALAN CATALIN - HASHURI - INFOARENA - 06.04.2009 */
#include<cstdio>
#include<cstring>
#define hshconst 100003
using namespace std;
long i,N,type,nr,nr2;
char buf[32],*p;
long get()
{
	long t;
	for (t = 0; *p>='0' && *p<='9'; ++p)
		t = t*10 + *p-'0';
	for (;*p==' ';++p);
	return t;
}
typedef struct node{int val; node *next;} *list;
list hsh[hshconst+2];

void addnode(int x)
{
	int ind = 1;
	int nr = x%hshconst;
	list q=hsh[nr];
	while (q)
	{
		if (q->val == x) {ind=0;break;}
		else q=q->next;
	}
	if (ind)
	{
		q = new node;
		q->val = x;
		q->next = hsh[nr];
		hsh[nr]=q;
	}
}
void deletenode(int x)
{
	int nr = x%hshconst;
	list d,q=hsh[nr];
	if (q && q->val == x){hsh[nr]=q->next;delete q;}
	else while (q)
	{
		if (q->next && q->next->val==x)
		{
			d=q->next;
			q=d->next;
			delete d;
			break;
		}
		q=q->next;
	}
}

int query(int x)
{
	int nr = x%hshconst;
	list q=hsh[nr];
	while (q)
	{
		if (q->val == x) return 1;
		q=q->next;
	}
	return 0;
}

int main()
{
	memset(hsh,0,sizeof(hsh));
	FILE *f = fopen("hashuri.in","r");
	FILE *g = fopen("hashuri.out","w");
	fscanf(f,"%d\n",&N);
	for (i = 1; i <= N; ++i)
	{
		fgets(buf,sizeof(buf),f);p=buf;
		type=get();
		nr=get();
		switch (type)
		{
			case 1: {addnode(nr);break;}
			case 2: {deletenode(nr);break;}
			case 3: {fprintf(g,"%d\n",query(nr));}
		}
	}
	fclose(f);
	fclose(g);

return 0;
}