#include <stdio.h>

#define FAIL -1
#define OK 0
#define N 'z'-'a'+1

int main(int argc, char **argv){
    FILE *fin;
    FILE *fout;
    int freqs[N]={0};
    int fpair[N][N]={0};
    char c, o=0;
    int r, j, i, count=0;
    float fried=0;
    if (argc<3) return FAIL;
    
    fin=fopen(argv[1], "r");
    if (!fin) return FAIL;
    fout=fopen(argv[2], "w");
    if (!fout) return FAIL;

    /* fscanf(stdin, "%c" &c); */
    fscanf(fin, "%c", &c);
    while(!feof(fin)){
        if (c>='A' && c<='Z') c=c-'A'+'a';
        if (c>='a' && c<='z'){ 
            freqs[c-'a']+=1;
            if (o!=0) fpair[o-'a'][c-'a']+=1;
            o=c;
            count++;
        }
        fscanf(fin, "%c", &c);
    }
    close(fin);

    for(i=0; i<N; i++){
        float v = freqs[i]/(float)count;
        fried += v*v;
        fprintf(fout,"%c %f\n", 'a'+i, v);
    }
    printf("Friedman Index: %f\n", fried);
            
    for(i=0; i<N; i++)
       for(j=0; j<N; j++)
            fprintf(fout,"%c%c %f\n", 'a'+i, 'a'+j, fpair[i][j]/(float)(count-1));
    close(fout);
    return OK;
}

