Cod sursa(job #2191692)

Utilizator ibicecIT Zilla ibicec Data 3 aprilie 2018 14:17:25
Problema Hashuri Scor 60
Compilator java Status done
Runda Arhiva educationala Marime 3.81 kb
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<>(5_000_011);

        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 = 5_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> {

    static class SingleLinkedList<L> {
        private class Entry<E> {
            private E key;
            private Entry next;

            public Entry(E key, Entry next) {
                this.key = key;
                this.next = next;
            }

        }

        private Entry begin;

        public SingleLinkedList() {
        }

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

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

        public void remove(L value) {
            Entry previousEntry = null;
            Entry currentEntry = this.begin;
            while (currentEntry != null) {
                if (value.equals(currentEntry.key)) {
                    if (previousEntry != null) {
                        previousEntry.next = currentEntry.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 = new SingleLinkedList[size];
    }

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

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

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

}