Pagini recente » Cod sursa (job #672605) | Cod sursa (job #388646) | Cod sursa (job #1984532) | Cod sursa (job #138284) | Cod sursa (job #2246385)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
class Hash {
private:
static const int HASHSIZE = 666013;
vector<int> hash[HASHSIZE];
inline int hash_func(int key) {
return key % HASHSIZE;
}
vector<int>::iterator find_it(int bucket, int searched_key) {
for (auto it = hash[bucket].begin(); it != hash[bucket].end(); ++it)
if (searched_key == *it)
return it;
return hash[bucket].end();
}
public:
Hash();
void ins(int key);
void del(int key);
int find(int key);
};
Hash::Hash() {
}
void Hash::ins(int key) {
int bucket = hash_func(key);
if (find_it(bucket, key) == hash[bucket].end())
hash[bucket].push_back(key);
}
void Hash::del(int key) {
int bucket = hash_func(key);
auto it = find_it(bucket, key);
if (it != hash[bucket].end())
hash[bucket].erase(it);
}
int Hash::find(int key) {
int bucket = hash_func(key);
if (find_it(bucket, key) != hash[bucket].end())
return 1;
return 0;
}
int main() {
int inps;
f >> inps;
int op, key;
Hash *hash = new Hash();
for (int i = 1; i <= inps; i++) {
f >> op >> key;
switch (op) {
case 1:
hash->ins(key);
break;
case 2:
hash->del(key);
break;
case 3:
g << hash->find(key) << "\n";
break;
}
}
}