fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int [] power = new int [1005];
  11. static int base = 29;
  12.  
  13.  
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. // your code goes here
  17. power[0] = 1; //Set Awal 1
  18. //Set tiap power kali basenya
  19. for (int x= 1; x<power.length; x++) {
  20. power[x] = power[x -1]*base;
  21. }
  22. String word = "lala";
  23. int hasil= getHash(word);
  24.  
  25. System.out.println(hasil);
  26.  
  27.  
  28. }
  29.  
  30. private static int getHash(String word) {
  31. int len = word.length();
  32. int hash = 0;
  33. for(int i = 0; i < len; i++) {
  34. hash += (int)(word.charAt(i)) * power[len-1-i];
  35. }
  36. return hash;
  37. }
  38. }
Success #stdin #stdout 0.07s 2184192KB
stdin
Standard input is empty
stdout
2718818