Wednesday 1 June 2011

uva 10008-What's Cryptanalysis

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define M 100
#define N 26
struct cryp
{
    int freq;
    char let;
};
bool compare(cryp c1,cryp c2);
int main()
{
    struct cryp count[M];
    register long i,n,p;
    char letter[10000],ch,c;
    //freopen("in.txt","r",stdin);
    for(i=0;i<M;i++)
        count[i].freq=0,count[i].let=i+65; //A=65 a=97
    scanf("%d",&n);
    while(n--)
    {   
        gets(letter);
        for(i=0;letter[i];i++)
        {
            ch=letter[i]&223;//Capital letter
            p=(ch^64)-1;
            count[p].freq++;
        }
    }
    sort(count,count+N,compare);
    for(i=0;count[i].freq;i++)
        printf("%c %d\n",count[i].let,count[i].freq);
    return 0;
}
bool compare(cryp c1,cryp c2)
{
    if(c1.freq>c2.freq)
        return true;
    else if((c1.freq==c2.freq)&&c1.let<c2.let)
        return true;
    return false;
}

No comments:

Post a Comment