Cod sursa(job #2221389)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 13 iulie 2018 22:30:02
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#define BIGPRIMENUMBER 138257

using namespace std;

class Hash
{
    vector<int> lists[BIGPRIMENUMBER];
public:
    bool search(int x, int &i)
    {
        int indice = x % BIGPRIMENUMBER;
        for(i = 0; i < lists[indice].size(); ++i)
        {
            if(lists[indice][i] == x)
                return true;
        }
        return false;
    }

    bool search(int x)
    {
        int indice = x % BIGPRIMENUMBER;
        for(int i = 0; i < lists[indice].size(); ++i)
        {
            if(lists[indice][i] == x)
                return true;
        }
        return false;
    }

    void insert(int x)
    {
        if(!search(x))
        {
            int indice = x % BIGPRIMENUMBER;
            lists[indice].push_back(x);
        }
    }

    void remove(int x)
    {
        int indice, listIndice = 0;
        if(search(x, listIndice))
        {
            int indice = x % BIGPRIMENUMBER;
            lists[indice].erase(lists[indice].begin() + listIndice);
        }
    }
};

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);
    int n;
    Hash h;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i)
    {
        int op, nr;
        scanf("%d", &op);
        scanf("%d", &nr);
        switch(op)
        {
            case 1:
            h.insert(nr);
            break;
            case 2:
            h.remove(nr);
            break;
            case 3:
            printf("%d\n", (int)h.search(nr));
            break;
            default:
            break;
        }
    }
    return 0;
}