Cod sursa(job #3231370)

Utilizator Razvan23Razvan Mosanu Razvan23 Data 26 mai 2024 10:51:56
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include<bits/stdc++.h>
using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
///hashuri

class Hash
{
public:
    int n;
    list<int> *H;
    Hash(int n)
    {
        this->n = n;
        H = new list<int>[n+5];
    }
    ~Hash()
    {
        delete []H;
    }
    int Modulo(int x)
    {
        return x % n;
    }
    void Insert(int x)
    {
        int index = Modulo(x);
        H[index].push_back(x);
    }
    void Delete(int x)
    {
        int index = Modulo(x);
        list <int> :: iterator it;
        for (it = H[index].begin(); it != H[index].end() && *it != x; it++)
            ;
        if(it != H[index].end()) H[index].erase(it);
    }
    void Afisare()
    {
        int i;
        for (i = 0; i < n; i++)
        {
            for (auto w : H[i])
                fout << w << " ";
            fout << "\n";
        }
    }
    bool Find(int x)
    {
        int index = Modulo(x);
        list<int>::iterator it;
        for (it = H[index].begin(); it != H[index].end(); it++)
            if (*it == x) return true;
        return false;
    }
};

int main()
{
    Hash A(1234577);
    int op, n, i, x;
    fin >> n;
    for(i=1; i<=n; i++)
    {
        fin >> op >> x;
        if(op == 1) A.Insert(x);
        else if(op == 2) A.Delete(x);
        else fout << A.Find(x)  << "\n";
    }
    return 0;
}