Cod sursa(job #1527083)

Utilizator paul-gPaul Grigoras paul-g Data 17 noiembrie 2015 20:00:36
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <set>
#include <unordered_set>
#include <fstream>
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>

using namespace std;

class HashSet {

  vector<list<int>> buckets;
  int capacity;

  public:
  HashSet(int _capacity) : capacity(_capacity) {
    buckets = vector<list<int>>(capacity);
  }

  void insert(int x) {
    list<int>& l = buckets[hash(x)];
    bool found = std::find(l.begin(), l.end(), x) != l.end();
    if (!found) {
      l.push_front(x);
    }
  }

  void erase(int x) {
    list<int>& l = buckets[hash(x)];
    l.remove(x);
  }

  bool find(int x) {
    list<int>& l = buckets[hash(x)];
    return std::find(l.begin(), l.end(), x) != l.end();
  }

  private:
  int hash(int x) {
    return x % capacity;
  }

};

int main() {

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

  int n;
  f >> n;
  for (int i = 0; i < n; i++) {
    int op, x;
    f >> op >> x;

    switch (op) {
      case 1:
        s.insert(x);
        break;
      case 2:
        s.erase(x);
        break;
      case 3:
        g << (s.find(x) ? 1 : 0) << "\n";
        break;
      default:
        std::cout << "Not supported!" << std::endl;
        break;
    }
  }

  return 0;
}