Cod sursa(job #1354547)

Utilizator anaid96Nasue Diana anaid96 Data 21 februarie 2015 21:15:35
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include<stdio.h>
#include<vector>

using namespace std;

FILE *in, *out;

//definitions
#define pb push_back

//constants
const int modulo = 666013;

//variables
int oper;
int type, value;
vector<int> Hash[modulo];


//functions
void insert(int value);
void erase(int value);
int search(int value);

int main(void)
{
	in = fopen("hashuri.in", "rt");
	out = fopen("hashuri.out", "wt");
	
	fscanf(in, "%d", &oper);
	
	while(oper--)
	{
		fscanf(in, "%d", &type);
		fscanf(in, "%d", &value);
		if(type == 1)
			insert(value);
		else
			if(type == 2)
				erase(value);
			else
				fprintf(out, "%d\n", search(value));
	}
	
	fclose(in);
	fclose(in);
	return 0;
}

void insert(int value)
{
	int pos = value % modulo;
	vector<int> :: iterator it, end=Hash[pos].end();
	for(it=Hash[pos].begin(); it!=end; ++it)
		if( *it == value)
			return;
	Hash[pos].pb(value);	
}

void erase(int value)
{
	int pos = value % modulo;
	vector<int> :: iterator it, end=Hash[pos].end();
	for( it=Hash[pos].begin(); it!=end; ++it)
		if( *it == value)
		{
			Hash[pos].erase(it);
			return;
		}
}

int search(int value)
{
	int pos = value % modulo;
	vector<int> :: iterator it, end=Hash[pos].end();
	for( it=Hash[pos].begin(); it!=end; ++it)
		if( *it == value)
			return 1;
	return 0;	
}