Advertisement
Guest User

Untitled

a guest
May 27th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4.  
  5. public class ZeichenkettenHashfunktion {
  6.  
  7.     public static int berechneHashwert(String eingabe,int a) {
  8.         char[]input = eingabe.toCharArray();
  9.         int result = 0;
  10.         for (int i=input.length-1;i>0;i--) {
  11.             result= result + (a*input[i]);
  12.         }
  13.         result = result + input[0];
  14.         return result%17021;
  15.     }
  16.    
  17.     public static int berechneKollisionen (int i) throws FileNotFoundException {
  18.         //Fehlerabsicherung noch machen
  19.         Scanner s = new Scanner(new File("words.txt"));
  20.         int countwords = 0;
  21.         int counthashresults = 0;
  22.         int[] hashresults = new int[17021];
  23.         hashresults[0]=1;
  24.         while (s.hasNext()) {
  25.             int j = berechneHashwert(s.next(),i);
  26.             hashresults[j]=j;
  27.             countwords=countwords+1;
  28.         }
  29.         for(i=0;i<hashresults.length;i++) {
  30.             if (hashresults[i]==i)
  31.                 counthashresults=counthashresults+1;
  32.         }
  33.         s.close();
  34.         System.out.println(countwords + "    " + counthashresults);
  35.         return countwords - counthashresults;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement