Cod sursa(job #346471)

Utilizator ancaaaancaaa ancaaa Data 7 septembrie 2009 22:52:51
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
/*
	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[N], *nil;
ifstream inFile;
ofstream outFile;
int n;



void init() {
	nil=new nod();
	nil->val=0;
	nil->next=0;

	for (int i=0; i<N; 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;
			i=i->next;
		}
}

/*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;
}