#include <stdio.h>

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

void get_data(char *f, float a[N]){
    FILE *fin;
    int i; 
    float v;
    char c;
    fin=fopen(f, "r");
    for(i=0; i<N; i++){
        fscanf(fin, "%c %f\n", &c, &v);
        a[i]=v;
    }
    close(fin);
    return;
}

char max(float a[N]){
    int i;
    float fmax=-1;
    char max='\0';
    for(i=0; i<N; i++) 
        if (a[i]>fmax) {
            fmax=a[i];
            max=i;
        }
    a[max]=-1;
    return max+'a';
}

void sort(float a[N], char r[N]){
    int i;
    for(i=0; i<N; i++)
        r[i]=max(a);
    return;
}

char get_letter(char lg[N], char cr[N], char l){
    int i;
    for(i=0; i<N; i++)
        if (cr[i]==l){
            return lg[i];
        }
    return '*';
}

int main(int argc, char **argv){
    float freqs_cr[N]={0};
    float freqs_lg[N]={0};
    char cr[N], lg[N], c;
    if (argc<3) return FAIL;
   
    get_data(argv[1], freqs_cr);
    sort(freqs_cr, cr);
    printf("Alphabet of ciphertext sorted by frequency: %s\n",cr);
    get_data(argv[2], freqs_lg);
    sort(freqs_lg, lg);
    printf("Alphabet of target language sorted by frequency: %s\n",lg);
    for(c='a'; c<='z'; c++)
        printf("%c", get_letter(lg, cr, c));
    printf("\n");
    return OK;
}

