Cod sursa(job #774753)

Utilizator ionut_blesneagIonut Blesneag ionut_blesneag Data 6 august 2012 14:39:22
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include<fstream>
const int m=780683;
using namespace std;

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

int n,i,c;
long e;

struct hash
{long x;
 hash* next;};
hash* h[780683]; 

void add(long elem)
{hash* q = new hash;
 int modl;
 q->x=elem;
 modl=e%m;
 q->next=h[modl];
 h[modl]=q;
}

int search(long elem)
{int modl;
 modl=e%m;
 hash* q=h[modl];
 while(q)
  {if(q->x==elem)
      return 1;
   q=q->next;}
 return 0;  
}

void del(int elem)
{int modl;
 modl=e%m;
 hash* q=h[modl];
 if(q)
 {if(q->x==elem)
   h[modl]=q->next;
 else    
 {hash* p;
  p=q;
  q=q->next;
  while(q)
  {if(q->x==elem)
    p->next=q->next;
  q=q->next;}
 }
} 
}

int main()
{
f>>n;    
for(i=1; i<=n; i++)
  {f>>c>>e;
   if(c==1)
     add(e);
   if(c==2)
     del(e);
   if(c==3)
     g<<search(e)<<endl;
  }   
     
f.close();
g.close();
return 0;
}