Cod sursa(job #2191668)

Utilizator ibicecIT Zilla ibicec Data 3 aprilie 2018 13:09:37
Problema Hashuri Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 3.74 kb
package org.iuliu;

import java.io.*;
import java.util.Objects;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {
        FileReader input = new FileReader("hashuri.in");
        Scanner in = new Scanner(input);

        PrintWriter out = new PrintWriter("hashuri.out");
        Hashset<Integer> set = new Hashset<>(2_000_003);

        int n = in.nextInt();
        for (int i=0; i<n; i++) {
            int op = in.nextInt();
            Integer num = in.nextInt();
            switch (op) {
                case 1:
                    set.put(num);
                    break;
                case 2:
                    set.remove(num);
                    break;
                case 3:
                    out.println(set.contains(num) ? '1' : '0');
            }
        }

        out.close();
        in.close();


//        int n = 2_000_000;
//        for (; !is_prime(n); n++);
//        System.out.println(n);
    }

//    private static boolean is_prime(int n) {
//        if (n < 1) {
//            throw new IllegalArgumentException("n should be a positive number");
//        }
//        if (n <= 5) {
//            return true;
//        }
//        int sqrtN = (int) Math.sqrt(n);
//        for (int i = 2; i < sqrtN; i++) {
//            if (n % i == 0) {
////                System.err.printf("%d %d\n", n, i);
//                return false;
//            }
//        }
//        return true;
//    }


}

class Hashset<T> {

    private class SingleLinkedList {
        private class Entry {

            private T value;
            private Entry next;

            public Entry(T value, Entry next) {
                this.value = value;
                this.next = next;
            }

        }

        private Entry begin;

        public void add(T value) {
            if (!contains(value)) {
                this.begin = new Entry(value, this.begin);
            }
        }

        public boolean contains(T value) {
            Entry currentEntry = this.begin;
            while (currentEntry != null) {
                if (value.equals(currentEntry.value)) {
                    return true;
                }
            }
            return false;
        }

        public void remove(T value) {
            Entry previousEntry = null;
            Entry currentEntry = this.begin;
            while (currentEntry != null) {
                if (value.equals(currentEntry.value)) {
                    if (previousEntry != null) {
                        previousEntry.next = previousEntry.next.next;
                    } else {
                        this.begin = this.begin.next;
                    }
                }
                previousEntry = currentEntry;
                currentEntry = currentEntry.next;
            }
        }
    }

    private SingleLinkedList[] table;

    public Hashset(int size) {
        if (size < 1) {
            throw new IllegalArgumentException("size should be a positive number");
        }
//        for (; !is_prime(size); size++);
        table = (SingleLinkedList[]) new Object[size];
    }

    void put(T item) {
        Objects.requireNonNull(item);
        int hash = item.hashCode() % table.length;
        if (table[hash] == null) {
            table[hash] = new SingleLinkedList();
        }
        table[hash].add(item);
    }

    void remove(T item) {
        Objects.requireNonNull(item);
        int hash = item.hashCode() % table.length;
        if (table[hash] != null) {
            table[hash].remove(item);
        }
    }

    boolean contains(T item) {
        Objects.requireNonNull(item);
        int hash = item.hashCode() % table.length;
        if (table[hash] != null) {
            return table[hash].contains(item);
        }
        return false;
    }

}