Cod sursa(job #1494393)

Utilizator deea101Andreea deea101 Data 30 septembrie 2015 23:35:00
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
enum State {empty, full, del};

struct node {
	unsigned int key;
	State state;
	
	node() {state = empty;}
	node(int key): key(key) {state = full;}
};

class Hash {
	node *table;
	unsigned int size;
	
	unsigned int probe(unsigned int h, unsigned int i){
		return (h+i);
	}
	unsigned int hash(int key){
		return key%size;
	}
	unsigned int find(int key) {
		unsigned int i, p, h = hash(key);
		for(i = 0, p = probe(h, i); i < size; i++, p = probe(h, i)) 
			if(table[p].state == empty || table[p].key == key)
				return p;
	}
		
public:
	Hash (): size(1<<20) {
		table = new node [1<<20];
	}
	void insert(int key) {
		unsigned int i = find(key);
		table[i].key = key;
		table[i].state = full;
	}
	void remove(int key) {
		unsigned int i = find(key);
		if(table[i].state == full)
			table[i].state = del;
	}
	bool isIn(int key) {
		unsigned int i = find(key);
		if(table[i].state == full)
			return true;
		return false;
	}
};

#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
 
int main()
{
    int T;
    f>>T;
	Hash h;
      
    int q,x;
    while(T--)
    {
        f>>q>>x;
        switch (q)
        {
        case 1: h.insert(x); break;
        case 2: h.remove(x); break;
        case 3: g<<h.isIn(x)<<'\n'; break;
        }
    }
}