Etaoin Shrdlu
InterviewTime limit1sMemory limit128 MB
Concatenate each sample's lines, count overlapping adjacent character pairs, then print the five most frequent digrams with their counts and rounded relative frequencies.
- Level
Medium4 of 10
- Topics
- String, Hash map, Sorting, Implementation
- Solved
- No attempts yet
Problem
The relative frequency of characters in natural-language text matters a great deal in cryptography, but the statistics differ from language to language. Here are the nine most frequent characters, ordered by relative frequency, for several common languages:
English: ETAOINSHR
German: ENIRSATUD
French: EAISTNRUL
Spanish: EAOSNRILD
Italian: EAIONLRTS
Finnish: AITNESLOK
Just as important as the frequencies of single characters are the frequencies of pairs of adjacent characters, called digrams. A digram is any two consecutive characters of the text, and the pairs overlap: a run of identical characters contains equal digrams. Given several text samples, find the digrams with the highest relative frequencies.
Input
The input contains several test cases. Each test case starts with a line holding a single integer , the number of text lines in the case. Then follow lines, each at most characters long. Concatenating these lines, with the line-break characters removed, yields the text sample to analyze. The sample consists of printable ASCII characters only.
The constraint is . A line with terminates the input and is not processed.
Output
For each test case output 5 lines, one for each of the 5 most frequent digrams. On each line print the digram itself (two characters), a single space, the absolute number of occurrences, another single space, and the relative frequency — that is, the number of occurrences divided by the total number of digrams in the sample. Round the relative frequency to exactly 6 decimal places.
When two digrams occur equally often, order them by their (ASCII) values, smaller first. Separate the output of consecutive test cases with a blank line.