Cod sursa(job #418518)

Utilizator mihaionlyMihai Jiplea mihaionly Data 15 martie 2010 22:56:47
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
using namespace std;
#define MOD 1999993
FILE *f=fopen("hashuri.in","r");
FILE *g=fopen("hashuri.out","w");
int n,x,ok;
struct hash
 {
 int x;
 hash *urm;
 };
hash *H[MOD+5];
void add(hash *&h,int x)
 {
 hash *ax=new hash;
 ax->x=x;
 ax->urm=h;
 h=ax;
 }
void del(hash *&h,int x)
 {
 if(!h) return;
 if(h->x==x) 
  {
  hash *p=new hash,*q=new hash;
  q=h;
  p=h->urm;
  h=p;
  delete q;
  return;
  }
 else
  for(hash *p=h;p->urm!=0;p=p->urm)
   {
   if(p->urm->x==x)
    {
	hash *q=new hash;
	q=p->urm;
	p->urm=p->urm->urm;
	delete q;
	return;
    }
   }
 }
bool find(int x)
 {
 for(hash *p=H[x%MOD];p!=NULL;p=p->urm)
  {
  if(p->x==x)
   return 1;
  }
 return 0;
 }
int main()
 {
 fscanf(f,"%d",&n);
 for(int i=1;i<=n;i++)
  {
  fscanf(f,"%d %d",&ok,&x);
  if(ok==1)
   add(H[x%MOD],x);
  else if(ok==2)
   del(H[x%MOD],x);
  else
   fprintf(g,"%d\n",find(x));
  }
 return 0;
 }