import java.io.*;
import java.util.*;/**
   Computation of the order k entropy of a text.
   Usage: <code>Entropy k file</code>. Prints
   the value of the <code>k</code>-th order entropy
   of the text contained in <code>file</code>. Uses
   the classes <code>Element</code> and <code>Buffer</code>.
*/
public class Entropy{
    static int END = -1;  //end of file (value returned by FileReader)
    static int N=500000;
    /**
	Returns <code> -(1/k)Sigma s[i]log(s[i])</code>.
    */
    public static float entropy(int k,float s[],int dim){
	float r=0; int i;
	for(i=0;i<dim;i++)
	    if (s[i]!=0) r+=s[i]*Math.log(s[i]);
	r=-r/((float) Math.log(2));
	return r/k;
    }
    public static void init(Reader in, int k, int dim, float s[])
	throws IOException{
	Hashtable table = new Hashtable(dim);
	Buffer t=new Buffer(in,k);
	while( t.head != END){
	    Element.add(t.word, table);
	    t.next(in, k);
	}
	Element.count(table,s);
    }
    public static void main(String[] args) throws IOException {
	int i,k=Integer.parseInt(args[0]);
	
	float[] statkuples;
	Reader in=new FileReader(args[1]);
	statkuples=new float[N];
	init(in,k,N,statkuples);
	System.out.println("H"+k+"="+entropy(k,statkuples,N));
	
    }
}


