Cod sursa(job #731209)

Utilizator teodora.petrisorPetrisor Teodora teodora.petrisor Data 7 aprilie 2012 18:34:22
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.87 kb
//======================================
// Name			: HashTables.cpp
// Author		: Petrisor Teodora
// Description	: Hash table problem Infoarena
//======================================

#include<fstream>
#include<vector>			// include vector template
#define prime 700001		// define an appropriate prime number

using namespace std;

vector<int>a[prime];		// declare a vector of vectors

vector<int>::iterator find(int x)	// define the find function
{
	vector<int>::iterator it;		// create a new vector iterator
	int pos=x%prime;				// define position at which we can find x
	
	for(it=a[pos].begin();it!=a[pos].end();++it)	// parse every element in sublist found at given position
		if(*it==x)		// if we find the element
			return it;	// we return the appropriate iterator
			
	return a[pos].end();	// if not, we return the iterator with the cursor at the end of the list
}
int main()
{
	int n, i, op ,x;
	ifstream f("hashuri.in");		// open files to read/write
	ofstream g("hashuri.out");
	f>>n;	// read the number of elements
	
	for(i=0;i<n;i++)		// parse the pairs of elements
	{
		f>>op>>x;			// read the pair operation - number
		if(op==1)			// if we have to write a number
		{
			if(find(x)==a[x%prime].end())		// we write it if it is not already in the list
				a[x%prime].push_back(x);		// we use the push_back function of the vector template
		}
		else if(op==3)		// if we have to check whether it is there
		{
			if(find(x)!=a[x%prime].end())	// we write 1 if we find it, 0 otherwise
				g<<"1\n";
			else 
				g<<"0\n";
		}
		else if(op==2)		// if we have to remove it
		{
			vector<int>::iterator it;	// we declare an iterator
			it=find(x);					// we call the function find
			if(it!=a[x%prime].end())	// if the element does exist in the list
				a[x%prime].erase(it);	// we erase it
		}
	}
	f.close();	// close the files
	g.close();
	return 0;
}