10008-what ' s cryptanalysis?
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=949
Cryptanalysis is the process of breaking someone else ' s cryptographic writing. This sometimes involves some kind to statistical analysis of a passage of (encrypted) text. Your task is, to write a, which performs a simple analysis of a given text.
Input
The a single positive decimal integer n for the contains of input. This is the number of lines which follow in the input. The next n lines would 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, then followed by a positive decimal in Teger. The integer indicates how to many the corresponding letter appears in the input text. Upper and lower case letters into the input are to be considered the same. No other characters must to be counted. The output must is sorted in descending count order; That's, the most frequent letter are on the "on" the "on" the "the", and "the", "the", "indicates" least frequent le Tter. IF two letters have the same frequency, then the letter which comes the the the alphabet of the the in the must of the the in the the output. If A letter does isn't appear in the text, then this 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
Complete code:
/*0.016s*/
#include <cstdio>
#include <algorithm>
using namespace std;
struct Letter
{
char ch;
int num;
BOOL operator < (const letter a) const
{return
num!= a.num? num > A.num:ch < a.ch;
}
} HASHTABLE[27];
Char str[100];
int main (void)
{
int n;
scanf ("%d\n", &n);///read swap line ~ for
(int i = 0; i < i++)
hashtable[i + 1].ch = ' A ' + i;
for (int i = 0; i < n; i++)
{
gets (str);
for (int j = 0; str[j]; j +)
if (Str[j] >= ' A ' && str[j] <= ' Z ' | | str[j] >= ' A ' && Str[j] & Lt;= ' z ')
hashtable[str[j] & 31].num++;
}
Sort (HashTable + 1, HashTable +);
for (int i = 1; I <= i++)
if (hashtable[i].num)
printf ("%c%d\n", hashtable[i].ch, hashtable[i].num);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/