Categories
CPE 程式解答

10402: What's Cryptanalysis?

10402:What's Cryptanalysis?
Time Limit: 3 sec

Description
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
3
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

  1.     #include <iostream>
  2.     #include <vector>
  3.     #include <algorithm>
  4.     using namespace std;
  5.      
  6.     class strcount{
  7.     public:
  8.         int num;
  9.         char ch;
  10.     };
  11.      
  12.     bool myfunction(strcount i,strcount j){
  13.         if(i.num>j.num)
  14.             return true;
  15.         else if(i.num==j.num)
  16.             return ((int)i.ch<(int)j.ch);
  17.         else
  18.             return false;
  19.     }
  20.      
  21.     int main()
  22.     {
  23.         vector<strcount> A(26);
  24.         for(int i=0;i<26;i++){
  25.             A[i].num=0;
  26.             A[i].ch=(char)(i+65);
  27.         }
  28.         string temp;
  29.         int a;
  30.         cin >> a;
  31.         //cout << a << endl;
  32.         for(int i=0;i<=a;i++){
  33.             //getline有点小问题 只好用小于等于 当然这对后期操作是没有影响的
  34.             getline(cin,temp);
  35.             //cout << temp << endl;
  36.             for(int k=0;k<temp.size();k++){
  37.                 int j = (int)temp[k];
  38.                 if(j>=97 >> j<=122) A[j-97].num++;
  39.                 if(j>=65 >> j<=90) A[j-65].num++;
  40.             }
  41.         }
  42.         sort(A.begin(),A.end(),myfunction);
  43.         for(int i=0;i<26;i++)
  44.             if(A[i].num!=0) cout << A[i].ch << " " << A[i].num << endl;
  45.         return 0;
  46.     }

pls:
程式都在cpe系統里鑒定通過了 當然可能coding style未必好 單純供大家參考
如果看不明白 欢迎留言或E-mail我 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.