Cod sursa(job #825259)

Utilizator Marius_mFMI-M2 Marius Melemciuc Marius_m Data 28 noiembrie 2012 00:53:51
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.93 kb
/*
 * =====================================================================================
 *
 *       Filename:  hash.cpp
 *
 *    Description:  http://infoarena.ro/problema/hashuri
 *
 *        Version:  1.0
 *        Created:  11/27/2012 10:55:53 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

class Hash
{
	vector<int> hash[499980];
	const int primeNumber;
public:
	Hash();
	~Hash();
	int position(const int& x);
	int insertValue(const int& x);
	int removeValue(const int& x);
	int searchValue(const int& x);
};

Hash::Hash() : primeNumber(499979)
{
}

int Hash::position(const int& x)
{
	return x%primeNumber;
}

int Hash::insertValue(const int& x)
{
	int pos = position(x);
	hash[pos].push_back(x);
	return 1;
}

int Hash::searchValue(const int& x)
{
	int pos = position(x);
	for(unsigned int i = 0 ; i < hash[pos].size() ; i++)
		if( hash[pos][i] == x )
			return i;
	return -1;
}

int Hash::removeValue(const int& x)
{
	int pos = position(x);
	int index = searchValue(x);
	if(index != -1 )
	{
		hash[pos][index] = hash[pos][ hash[pos].size()-1 ];
		hash[pos].pop_back();

		return 1;
	}
	return 0;  // nu se gaseste ,pentru a putea fi sters
}

Hash::~Hash()
{
}

int main()
{	
	Hash H;
	int N,operation,number;
	FILE *in,*out;
	in = fopen("hashuri.in","r");
	out = fopen("hashuri.out","w");
	fscanf(in,"%d",&N);printf("%d",N);
	for(int i = 1 ; i <= N ; i++)
	{
		fscanf(in,"%d %d",&operation,&number);
		if(operation == 1)
			if( H.searchValue(number) == -1)
				H.insertValue(number);
		if(operation == 2)
			if( H.searchValue(number) != -1 )
				H.removeValue(number);
		if(operation == 3)
			if( H.searchValue(number) != -1 )
				fprintf(out,"1\n");
			else
				fprintf(out,"0\n");
	}
	fclose(in);
	fclose(out);
	return 0;
}