Try   HackMD

UVA 10008 - What's Cryptanalysis

題目:

密碼翻譯(cryptanalysis)是指把某個人寫的密文(cryptographic writing)加以分解。這個程序通常會對密文訊息做統計分析。你的任務就是寫一個程式來對密文作簡單的分析。

Input
輸入的第1列有一個正整數n,代表以下有多少列需要作分析的密文。 接下來的n列,每列含有0或多個字元(可能包含空白字元)

Output
每列包含一個大寫字元(A~Z)和一個正整數。這個正整數代表該字元在輸入中出現的次數。輸入中大小寫(例如:A及a)視為相同的字元。輸出時請按照字元出現的次數由大到小排列,如果有2個以上的字元出現次數相同的話,則按照字元的大小(例如:A在H之前)由小到大排列。 請注意:如果某一字元未出現在輸入中,那他也不應出現在輸出中。

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

Sample Output #1
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

C++ code:

#include <bits/stdc++.h> using namespace std; int main () { //ios::sync_with_stdio(0); cin.tie(0); int n, len = 0; int count[100] = {0}; cin >> n; cin.ignore(); while (n--) { string s; getline(cin, s); for (int i = 0; i < s.length(); ++i) { if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') { len++; count[toupper(s[i])]++; } } } for (int j = len; j >= 1; --j) { for (char i = 'A'; i <= 'Z'; ++i) { if (count[i] == j) { cout << i << " " << count[i] << endl; } } } }