Cod sursa(job #1520596)

Utilizator jimcarterJim Carter jimcarter Data 9 noiembrie 2015 04:17:42
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <cstdio>
#include <vector>
using namespace std;

FILE *f = fopen ( "hashuri.in" , "r" ) , *g = fopen ( "hashuri.out" , "w" );

const int MAX = 666015;
int i , N , type , request;
vector < int > hash [ MAX ];

int hashFunction ( int number )
{
   return ( number % 666013 ) ;
}

void add ( int number )
{
	int value = hashFunction ( number );
	hash [ value ] . push_back ( number );
}

void remove ( int number )
{
	int value = hashFunction ( number );
	for ( i = 0 ; i < hash [ value ] . size () ; i ++ )
		if ( hash [ value ] [ i ] == number )
			hash [ value ] [ i -- ] = hash [ value ] [ hash [ value ] . size () - 1 ] , hash [ value ] . pop_back () ;
}

int isIn ( int number )
{
	int value = hashFunction ( number );
	for ( i = 0 ; i < hash [ value ] . size () ; i ++ )
		if ( hash [ value ] [ i ] == number )
			return 1;
	return 0;
}

void read()
{
	fscanf ( f , "%d" , &N );
	for ( ; N ; N -- )
	{
		fscanf ( f , "%d %d" , &type , &request );
		if ( type == 1 ) //add request to the hash
			add ( request );
		else
			if ( type == 2 ) //remove request from the hash
				remove ( request );
			else //check to see if request is in hash or not
				fprintf ( g , "%d\n" , isIn ( request ) );
	}
}

int main()
{
	read();
	return 0;
}