import java.io.*; import java.util.ArrayList; /* * Eingabe bitte per stdin, etwa * ./a.out < test.txt * in einem Linux-System. Die Inputdatei muss das * folgende Format haben: * ... */ public class HashDoS { public static int counter; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s; if ((s = in.readLine()) == null) { System.err.println("Keine Eingabe auf stdin"); return; } String[] raw = s.split("\\s+"); int n = Integer.parseInt(raw[0]); if ( raw.length != n+1 ) { System.err.println("Ungültige Eingabe"); } int[] list = new int[n]; for( int i = 0; i < n; i++ ) { list[i] = Integer.parseInt(raw[i+1]); } System.out.println("Suche nach Duplikaten"); boolean res = hasDoublet( n, list ); if ( res ) System.out.println("Liste enhält Duplikate"); else System.out.println("Liste enthält keine Duplikate"); System.out.println("Operationen: " + counter); return; } public static int hash(int x, int m, int c) { int r = x % 23; return ((x % m) * (x % m) + 3 * x + r * r * r * r + c) % m; } public static boolean hasDoublet(int n, int[] list) { int m = 2 * n; int c = (int) (Math.random() * 1000); ArrayList> buckets = new ArrayList>(); counter = 0; for (int i = 0; i < m; ++i) { buckets.add(new ArrayList()); } for (int i = 0; i < n; ++i) { if (list[i] < 0) { return false; } int key = hash(list[i], m, c); counter++; ArrayList candidates = buckets.get(key); for (Integer element : candidates) { counter++; if ( element == list[i]) return true; } candidates.add(list[i]); } return false; } }