Solution of 10008 - What's Cryptanalysis?

Problem Description
source:http://uva.onlinejudge.org/external/100/10008.html

Cryptanalysis is the process of breaking someone else’s cryptographic writing. This sometimes involves some kind of statistical analysis of a passage of (encrypted) text. Your task is to write a program which performs a simple analysis of a given text. 

Input 

The first line of input contains a single positive decimal integer n. This is the number of lines which follow in the input. The next n lines will contain zero or more characters (possibly including whitespace). This is the text which must be analyzed.

Output 

Each line of output contains a single uppercase letter, followed by a single space, then followed by a positive decimal integer. The integer indicates how many times the corresponding letter appears in the input text. Upper and lower case letters in the input are to be considered the same. No other characters must be counted. The output must be sorted in descending count order; that is, the most frequent letter is on the first output line, and the last line of output indicates the least frequent letter. If two letters have the same frequency, then the letter which comes first in the alphabet must appear first in the output. If a letter does not appear in the text, then that letter must not appear in the output.

Sample Input 


This is a test. 
Count me 1 2 3 4 5. 
Wow!!!! Is this question easy? 

Sample Output 

S 7 
T 6 
I 5 
E 4 
O 3 
A 2 
H 2 
N 2 
U 2 
W 2 
C 1 
M 1 
Q 1 
Y 1 

Solution:
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main()
{
    int i,n,j,count[30],value[30];
    string input;
    char c;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%c",&n,&c)==2)
    {
        memset(count,0,sizeof(count));
        memset(value,0,sizeof(value));
        for(i=0;i<n;i++)
        {
            getline( cin, input );
            for(j=0;input[j]!='\0';j++)
            {
                if(input[j]>=97 && input[j]<=122)
                {
                    count[input[j]-97]+=1;
                    value[input[j]-97]+=1;
                }

                else if(input[j]>=65 && input[j]<=90)
                {
                    count[input[j]-65]+=1;
                    value[input[j]-65]+=1;
                }

            }
        }
        sort(value,value+26);

        for(i=25;i>=0;i--)
        {
            if(value[i]>0)
            {
                for(j=0;j<26;j++)
                {
                    if(value[i]==count[j])
                    {
                        printf("%c %d\n",(j+65),count[j]);
                        count[j]=0;
                    }
                }
            }


        }
    }
    return 0;
}
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience