Under construction

Still being built. Things may change

Etaoin Shrdlu

Interview

Time limit1sMemory limit128 MB

Summary
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 kk identical characters contains k−1k-1 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 nn, the number of text lines in the case. Then follow nn lines, each at most 8080 characters long. Concatenating these nn lines, with the line-break characters removed, yields the text sample to analyze. The sample consists of printable ASCII characters only.

The constraint is 1≤n≤641 \le n \le 64. A line with n=0n = 0 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.

Examples1

  1. Example 1

    Input
    2
    Take a look at this!!
    !!siht ta kool a ekaT
    5
    P=NP
     Authors: A. Cookie, N. D. Fortune, L. Shalom
     Abstract: We give a PTAS algorithm for MaxSAT and apply the PCP-Theorem [3]
     Let F be a set of clauses. The following PTAS algorithm gives an optimal
     assignment for F:
    0
    
    Expected output
     a 3 0.073171
    !! 3 0.073171
    a  3 0.073171
     t 2 0.048780
    oo 2 0.048780
    
     a 8 0.037209
    or 7 0.032558
    .  5 0.023256
    e  5 0.023256
    al 4 0.018605