Cod sursa(job #2625550)

Utilizator ArthurelVilceanu Razvan-Arthur Arthurel Data 6 iunie 2020 00:35:23
Problema Hashuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.87 kb
#include <iostream>
#include <list>
#include <cstring>
#include <fstream>

using namespace std;

ifstream f ("hashuri.in");
ofstream g ("hashuri.out");

class Hash
{
private:
    static const long long hashGr = 10000000000;
    list<pair<int,int>> table[hashGr];

public:

    int hashFunc(int key);
    void inserareVal(int key,int value);
    void stergereVal(int key);
    int cautaVal (int key);



};



int Hash::hashFunc(int key)
{

    return key%hashGr;
}

void Hash::inserareVal(int key,int value)
{
    int hashVal= hashFunc(key);
    auto& cell = table[hashVal];
    auto bItr = begin(cell);
    bool exKey = false;
    for(;bItr != end(cell);bItr++)
    {
        if(bItr->first == key)
        {
            exKey=true;
            bItr->second = value;
            break;
        }
    }

    if (!exKey)
    {
        cell.emplace_back(key, value);
    }


}

void Hash::stergereVal(int key)
{
     int hashVal= hashFunc(key);
    auto& cell = table[hashVal];
    auto bItr = begin(cell);

    for(;bItr != end(cell);bItr++)
    {
        if(bItr->first == key)
        {

            bItr=cell.erase(bItr);
            break;
        }
    }
}
int Hash::cautaVal(int key)
{
     int hashVal= hashFunc(key);
    auto& cell = table[hashVal];
    auto bItr = begin(cell);

    for(;bItr != end(cell);bItr++)
    {
        if(bItr->first == key)
        {
            return 1;



            break;
        }
    }

    return 0;
}
int main()
{int n,val,op;
   Hash HT;


   f>>n;
   for (int i=1;i<=n;i++)
   {
       f>>op>>val;
       switch(op)
       {
           case 1: {HT.inserareVal(HT.hashFunc(val),val);break;}
           case 2: {HT.stergereVal(HT.hashFunc(val));break;}
           case 3: {g<<HT.cautaVal(HT.hashFunc(val))<<"\n";break;}

       }
   }


    return 0;
}