Pagini recente » Cod sursa (job #81436) | Cod sursa (job #2601948) | Cod sursa (job #121691) | Cod sursa (job #105987) | Cod sursa (job #1048395)
#include <stdio.h>
#include <cstring>
using namespace std;
const int MOD=999983;
struct hash
{
int value;
hash *link;
hash(int value,hash *link)
{
this->value=value;
this->link=link;
}
}*H[999983];
int find(int x)
{
int pos=x%MOD;
for(hash *p=H[pos];p;p=p->link)
if(p->value==x)
return 1;
return 0;
}
void add(int x)
{
if(find(x)) return;
int pos=x%MOD;
hash *p=new hash(x,H[pos]);
H[pos]=p;
}
void del(int x)
{
if(!find(x)) return;
int pos=x%MOD;
hash *pre,*curr;
curr=pre=NULL;
for(curr=H[pos];curr;pre=curr,curr=curr->link)
if(curr->value==x) break;
hash *keep=curr;
curr=curr->link;
if(pre!=NULL) pre->link=curr;
else H[pos]=curr;
delete keep;
}
int main()
{
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
memset(H,0,sizeof(H));
int N;
scanf("%d",&N);
while(N--)
{
int op,x;
scanf("%d%d",&op,&x);
switch(op)
{
case 1:
add(x);
break;
case 2:
del(x);
break;
case 3:
printf("%d\n",find(x));
break;
}
}
return 0;
}