Pagini recente » Cod sursa (job #569891) | Cod sursa (job #1486751) | Cod sursa (job #1854503) | Cod sursa (job #2088966) | Cod sursa (job #346476)
Cod sursa(job #346476)
/*
Problema Hashuri infoarena.
http://infoarena.ro/problema/hashuri
*/
#include <iostream>
#include <fstream>
using namespace std;
#define N 1000001
#define MOD 200003
struct nod {
int val;
nod *next;
};
nod *hash[MOD], *nil;
ifstream inFile;
ofstream outFile;
int n;
void init() {
nil=new nod();
nil->val=0;
nil->next=0;
for (int i=0; i<MOD; i++) {
hash[i]=nil;
}
}
void put_in_hash(int x, int y) {
nod *node;
node=new nod();
node->val=x;
node->next=hash[y];
hash[y]=node;
}
bool is_in_hash(int x, int mod) {
for (nod *i=hash[mod]; i!=nil; i=i->next)
if (i->val==x)
return 1;
return 0;
}
void del(int x, int mod) {
if (hash[mod]->val==x) {
nod *t=hash[mod];
hash[mod]=hash[mod]->next;
delete t;
}
for (nod *i=hash[mod]; i!=nil; i=i->next)
if (i->next->val==x) {
nod *t=i->next;
i->next=t->next;
delete t;
return;
}
}
/*void print_list(nod *P) {
for (nod *i=P; i!=nil; i=i->next)
cout<<i->val<<" ";
cout<<endl;
}
void print_hash() {
// tiparesc numai primele 5 intrari din hash
cout<<"Hash-ul este: "<<endl;
for (int i=0; i<=5; i++)
if (hash[i]!=nil) {
cout<<i<<": ";
print_list(hash[i]);
}
cout<<"...";
}*/
int main() {
int x, y, mod;
inFile.open("hashuri.in", ios::in);
if (inFile.is_open()==0) exit(1);
outFile.open("hashuri.out", ios::out);
if (outFile.is_open()==0) exit(1);
init();
inFile>>n;
for (int i=1; i<=n; i++) {
inFile>>y;
inFile>>x;
mod=x%MOD;
switch (y) {
case 1: {
if (is_in_hash(x, mod)==0)
put_in_hash(x, mod);
break;
}
case 2: {
del(x, mod);
break;
}
case 3: {
bool b=is_in_hash(x, mod);
outFile<<b<<endl;
break;
}
}
}
//print_hash();
return 0;
}