Pagini recente » Cod sursa (job #1166905) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #598783) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #913080)
Cod sursa(job #913080)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <math.h>
using namespace std;
class blablaHashing{
public:
blablaHashing(int, string, string);
~blablaHashing();
bool insertNumber(int);
bool find(int);
void erase(int);
void start();
private:
int *table1, *table2, sizeHash;
int loopLimit;
int random1, random2;
fstream in, out;
void swap(int &,int &);
void changeHashingFunction();
int getRandomNumber();
int funct1(int);
int funct2(int);
};
int main (){
blablaHashing Hash(1000000, "hashuri.in", "hashuri.out");
Hash.start();
return 0;
}
blablaHashing::blablaHashing(int n, string fin, string fout){
sizeHash = n;
in.open(fin.c_str(), ios::in);
out.open(fout.c_str(), ios::out);
table1 = new int [n];
table2 = new int [n];
loopLimit = log2(sizeHash);
}
void blablaHashing::start(){
changeHashingFunction();
int M, op, x;
bool ok;
in >> M;
for (; M; M--){
in >> op >> x;
if (op == 1){
ok = insertNumber(x);
if (!ok){
in.seekg(0, ios::beg);
in.seekg(0, ios::beg);
start();
}
}
if (op == 2){
if (find (x)) erase (x);
}
if (op == 3){
if (find(x)) out << 1 << endl;
else out << 0 << endl;
}
}
}
bool blablaHashing::find (int x){
int func1 = funct1(x);
int func2 = funct2(x);
if (table1[func1] == x || table2[func2] == x) return true;
else return false ;
}
void blablaHashing::erase (int x){
int func1 = funct1(x);
int func2 = funct2(x);
if (table1[func1] == x) table1[func1] = 0;
else table2[func2] = 0;
}
bool blablaHashing::insertNumber(int x){
for (int i = 1; i <= loopLimit; i++){
int tempAux = funct1(x);
if (table1[tempAux] == x){
return true;
}else if (!table1[tempAux]){
table1[tempAux] = x;
return true;
}else swap(x, table1[tempAux]);
tempAux = funct2(x);
if (table2[tempAux] == x){
return true;
}else if (!table2[tempAux]){
table2[tempAux] = x;
return true;
}else swap(x, table2[tempAux]);
}
return false;
}
blablaHashing::~blablaHashing(){
in.close();
out.close();
}
void blablaHashing::changeHashingFunction(){
random1 = rand();
random2 = rand();
}
int blablaHashing::funct1(int x){
return (((long long)x * (long long)random1 + (long long)1000001) % (long long)random2 + (long long)1234567 ) % (long long)sizeHash;
}
int blablaHashing::funct2(int x){
return (((long long)x * (long long)random2 + (long long)1000001) % (long long)random1 + (long long)1234567 ) % (long long)sizeHash;
}
void blablaHashing::swap(int &a,int &b){
int tempAux = a;
a = b;
b = tempAux;
}
int blablaHashing::getRandomNumber(){
return 4;
}