Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class ZeichenkettenHashfunktion {
- public static int berechneHashwert(String eingabe,int a) {
- char[]input = eingabe.toCharArray();
- int result = 0;
- for (int i=input.length-1;i>0;i--) {
- result= result + (a*input[i]);
- }
- result = result + input[0];
- return result%17021;
- }
- public static int berechneKollisionen (int i) throws FileNotFoundException {
- //Fehlerabsicherung noch machen
- Scanner s = new Scanner(new File("words.txt"));
- int countwords = 0;
- int counthashresults = 0;
- int[] hashresults = new int[17021];
- hashresults[0]=1;
- while (s.hasNext()) {
- int j = berechneHashwert(s.next(),i);
- hashresults[j]=j;
- countwords=countwords+1;
- }
- for(i=0;i<hashresults.length;i++) {
- if (hashresults[i]==i)
- counthashresults=counthashresults+1;
- }
- s.close();
- System.out.println(countwords + " " + counthashresults);
- return countwords - counthashresults;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement