Cod sursa(job #1708583)

Utilizator Cristi.96Ion Alexandru Cristian Cristi.96 Data 27 mai 2016 14:33:04
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.43 kb
using namespace std;
#include<iostream>
#include<fstream>
#include <vector>
#include <string>
#include <typeinfo>
#define maxim 13
// #include"poo118nod.h"
// #include"poo118numar.h"


template <class T, class C>

class Hash {
public:
    Hash();
    ~Hash();
    void inserare(T key, C val);
    void stergere(T key);
    int cautare(T key);
    C operator[](T key);
private:
    int ReT(T t);
    std::vector<std::vector<std::pair<T, C> > > vect;
};

template <class T, class C> Hash<T, C>::Hash() {
    for (int i = 0; i < maxim; i++) {
        std::vector<std::pair<T, C> > lista;
        vect.push_back(lista);
    }
}

template <class T, class C> Hash<T, C>::~Hash() {
    for (int i = 0; i < maxim; i++) vect[i].clear();
    vect.clear();
}

template <class T, class C> int Hash<T, C>::ReT (T t ) {
    T* t1 = &t;
    char* ct=(char*)t1;
    unsigned int r = 0;
    int j=sizeof(T);
    for (char* pc = ct; pc != ct + j; pc++) {
        r = (r * 10 + int(*pc)) % maxim;
    }

    return r;
}

template <class T, class C> void Hash<T, C>::inserare (T key, C val ) {
    int cheie = ReT(key);
    int i = 0,j=vect[cheie].size();
    for(;i<j;i++)
    if (vect[cheie][i].first == key) return;
    vect[cheie].push_back(std::make_pair(key, val));
}

template <class T, class C> void Hash<T, C>::stergere (T key ) {
    int cheie = ReT(key);
    int i = 0,j=vect[cheie].size();
    for (;i < j;i++)
        if (vect[cheie][i].first == key) {
            vect[cheie].erase(vect[cheie].begin() + i);
            return;
    }
}

template <class T, class C> int Hash<T, C>::cautare (T key ) {
    int cheie = ReT(key);
    int i = 0,j=vect[cheie].size();
    for(;i<j;i++)
    if (vect[cheie][i].first == key) return 1;
    return 0;
}

/*
template <class T, class C> C Hash<T, C>::operator[] (T key ) {
    int cheie = ReT(key);
    int i = 0,j=vect[cheie].size();

    for(;i<j;i++)
    if (vect[cheie][i].first == key)
        return vect[cheie][i].second;
    return -1;
}

*/
int main () {
    ifstream fin("hashuri.in");
    ofstream fout("hashuri.out");

    Hash<int, int> hash;
    int i=0,op,j=0;int nr;
    int N; fin >> N;
    for (; i < N; i++) {
    fin>>op>>nr;
        if (op==1) hash.inserare(nr, 0)
        else if (op == 2) hash.stergere(nr);
        else if (op == 3) fout << hash.cautare(nr) << "\n";
    }
 //   for(i=0;i<200;i++)  if(hash[i]!=-1) cout<<hash[i];
}