fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. //double C(String x,Map<Character,Integer>f){double H=0,g;for(char c:x.toCharArray())f.put(c,f.containsKey(c)?f.get(c)+1:1);for(char c:f.keySet()){g=f.get(c);H+=g*Math.log(g/x.length())/Math.log(2);}return-H;}
  6.  
  7. class Ideone
  8. {
  9. public static double C(String x,Map<Character,Integer>f)
  10. {
  11. double H=0,g;
  12.  
  13. for(char c:x.toCharArray())
  14. {
  15. f.put(c, f.containsKey(c) ? f.get(c)+1 : 1);
  16. }
  17.  
  18. for(char c : f.keySet())
  19. {
  20. g = f.get(c);
  21. H += (g/Math.log(2)) * Math.log(g/x.length());
  22. }
  23.  
  24. return-H;
  25. }
  26.  
  27. public static void main (String[] args) throws java.lang.Exception
  28. {
  29. System.out.println("'This is a test.'=> "+C("This is a test.",new TreeMap<Character,Integer>()));
  30. System.out.println("'00001111' => "+C("00001111",new TreeMap<Character,Integer>()));
  31. System.out.println("'cwmfjordbankglyphsvextquiz' => "+C("cwmfjordbankglyphsvextquiz",new TreeMap<Character,Integer>()));
  32. System.out.println("' ' => "+C(" ",new TreeMap<Character,Integer>()));
  33. }
  34. }
  35.  
  36.  
  37.  
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
'This is a test.'=> 45.09358392980084
'00001111' => 8.0
'cwmfjordbankglyphsvextquiz' => 122.21143267166846
'               ' => -0.0