E -What's Cryptanalysis?

                                            What's Cryptanalysis?

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.

sampale input

3
This is a test.
Count me 1 2 3 4 5.
Wow!!!!  Is this question easy?
sampale 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
教训:没看准题目,于是想复杂了,做了好久才做出来,突然发现它的输出格式数目相同的时候竟然是按字母表顺序,字母表是词典的字母表不是他给的那数据表的顺序,真崩溃。
题目中用到了一个大小写字母转换成小写字母的函数,tolower();它的用法1,本身为大写字母用这个函数便转换成小写,若本身为小写则不变。它的头文件为#include<stdlib.h>
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
int cnt[26];
int main()
{
    int n,ans=0;
    cin>>n;
    string str;
    string str1;
    str1="0";
    int h;
    //memset(cnt,0,sizeof(cnt));
    getchar();
    for(int i=0;i<n;i++)
    {
        getline(cin,str);
        int len=str.length();
        for(int j=0;j<len;j++)//记录字母出现的次数
        {
            if((tolower(str[j])-'a')>=0&&(tolower(str[j])-'a')<=25)
            {
               cnt[tolower(str[j])-'a']++;


            }
        }


    }
    for(int i1=0;i1<26;i1++)//记录出现字母的个数
        if(cnt[i1]!=0)
        ans++;
    int p=0;
    while(p<ans)//按出现次数的高低由大到小输出
    {
        int max=0;
        int maxi=0;
       for(int i1=0;i1<26;i1++)
            if(max<cnt[i1])
            {
              max=cnt[i1];
               maxi=i1;
            }
     cout<<char(maxi+65)<<' '<<max<<endl;
     cnt[maxi]=0;//注意一定要清零,否则会重复搜索
     p++;
    }


    return 0;
}

你可能感兴趣的:(E -What's Cryptanalysis?)