Pagini recente » Cod sursa (job #967611) | Cod sursa (job #72986) | Cod sursa (job #1906063) | Statistici Gabriela Strezea (Strezea_Gabriela_323CB) | Cod sursa (job #913103)
Cod sursa(job #913103)
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <bitset>
#include <ctime>
using namespace std;
class blablaHashing{
public:
blablaHashing(int, string, string);
~blablaHashing();
bool insertNumber(int);
bool find(int);
void erase(int);
void start();
private:
fstream in, out;
int *table1, *table2;
int loopLimit, random1, random2, sizeHash;
bitset<1000000> viz1, viz2;
void swap(int &,int &);
void changeHashingFunction();
int getRandomNumber();
int funct1(int);
int funct2(int);
};
int main (){
blablaHashing Hash(1000003, "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);
srand((unsigned)time(NULL));
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();
}
continue;
}
if (op == 2){
if (find (x)) erase (x);
continue;
}
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;
viz1[func1] = 0;
}
else{
table2[func2] = 0;
viz2[func2] = 0;
}
}
bool blablaHashing::insertNumber(int x){
for (int i = 1; i <= loopLimit; i++){
int tempAux = funct1(x);
if (!viz1[tempAux]){
table1[tempAux] = x;
viz1[tempAux] = 1;
return true;
}else if (table1[tempAux] == x){
return true;
}else swap(x, table1[tempAux]);
tempAux = funct2(x);
if (!viz2[tempAux]){
table2[tempAux] = x;
viz2[tempAux] = 1;
return true;
}else if (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();
viz1.reset();
viz2.reset();
}
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;
}