Pagini recente » Cod sursa (job #1753623) | Cod sursa (job #1701479) | Clasament 21 | Borderou de evaluare (job #1058857) | Cod sursa (job #914858)
Cod sursa(job #914858)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#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, random3, random4, sizeHash;
bool *viz1, *viz2;
//bitset<1000000> viz1, viz2;
void swap(int &,int &);
void changeHashingFunction();
int getRandomNumber();
int funct1(int);
int funct2(int);
};
int main (){
srand((unsigned)time(NULL));
blablaHashing Hash(1000009, "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];
viz1 = new bool [n];
viz2 = new bool [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){
if(!find(x)) {
ok = insertNumber(x);
if (!ok) {
in.seekg(0, ios::beg);
out.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;
}
}
inline 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 ;
}
inline 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;
}
}
inline bool blablaHashing::insertNumber(int x){
for (int i = 1; i <= loopLimit; i += 2){
int tempAux = funct1(x);
if (!viz1[tempAux]){
table1[tempAux] = x;
viz1[tempAux] = 1;
return true;
}
swap(x, table1[tempAux]);
tempAux = funct2(x);
if (!viz2[tempAux]) {
table2[tempAux] = x;
viz2[tempAux] = 1;
return true;
}
swap(x, table2[tempAux]);
}
return false;
}
blablaHashing::~blablaHashing(){
delete []viz1;
delete []viz2;
delete []table1;
delete []table2;
in.close();
out.close();
}
void blablaHashing::changeHashingFunction(){
random1 = rand()%sizeHash;
random2 = rand()%sizeHash;
random3 = rand()%sizeHash;
random4 = rand()%sizeHash;
//viz1.reset();
//viz2.reset();
//fill(viz1, viz1 + sizeHash, false);
//fill(viz2, viz2 + sizeHash, false);
memset(viz1, 0, sizeof(viz1));
memset(viz2, 0, sizeof(viz2));
}
inline int blablaHashing::funct1(int x){
return (((long long)x * (long long)random1) + (long long)random2) % sizeHash;
}
inline int blablaHashing::funct2(int x){
return (((long long)x * (long long)random3) + (long long)random4) % sizeHash;
}
inline void blablaHashing::swap(int &a,int &b){
int tempAux = a;
a = b;
b = tempAux;
}
int blablaHashing::getRandomNumber(){
return 4;
}