Pagini recente » Cod sursa (job #91794) | Cod sursa (job #3205743) | Cod sursa (job #2256280) | Cod sursa (job #2684348) | Cod sursa (job #825261)
Cod sursa(job #825261)
/*
* =====================================================================================
*
* 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]; //limita prea mare la vector,trebuie sa "tai" un 9 pentru a nu da segmentation fault,aceeasi valoare si la
const int primeNumber; // 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);
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;
}