Statistical Trouble
Time limit1sMemory limit128 MB
Output each cross table of two survey questions as raw counts and rounded row and column percentages in a fixed 6-character grid.
- Level
Hard8 of 10
- Topics
- Implementation, Matrix, Simulation, Math
- Solved
- No attempts yet
Problem
Your team was hired by the international corporation ACM (Analytical Calculation Maxims). Every year ACM designs and runs various surveys. A survey is a simple form: a list of questions and, for each question, a list of possible answers. Surveys are distributed around the globe, where field agents question a target group of people. All answers are gathered at the ICPC (International Computation and Processing Center), where teams of analysts mine the raw data for relevant correlations. The raw data for one survey is a set of lines; each line corresponds to one questioned person and lists, for every question, the answer that person gave.
The first step of the analysis your team must automate is building cross tables that correlate the answers to interesting pairs of questions. In its simplest form, for a pair of questions a cross table has one row for every possible answer to the first question and one column for every possible answer to the second question. Each cell holds the number of raw-data lines that contain both of the corresponding answers at the same time.
Your task is more involved: besides the plain cross-table values you must also output the total for every row and every column (the sum of the values in that row or column), placed in an extra last column and an extra last row, together with a percentage distribution for every row and every column. The row-wise percentage in a cell is the percent ratio of that cell's value to the total of its row, unless the row total is zero (in which case the row-wise percentages of that row are undefined). Column-wise percentages are defined the same way, using column totals. Thus each cell can show up to three numbers: the value itself, its row-wise percent, and its column-wise percent. Percentages also apply to the totals: in the last (total) column the row-wise percent is always 100% (unless the row total is zero, when it is undefined), while the column-wise percent shows the ratio of that row's total to the grand total of all lines (the value in the bottom-right cell).
Percentages are rounded to integers on output using the following deterministic rule. Compute each exact percentage and take its floor (the largest integer that is not greater than it). Consider a group of percentages that must sum to 100%: for row-wise percentages this is one row excluding its total column, and for column-wise percentages this is one column excluding its total row. If the floors of a group sum to (they are short by ), add exactly 1 to the last cells of the group that have a non-zero fractional part, scanning left to right within a row and top to bottom within a column. Percentages that are already integers are printed unchanged. When the relevant total is zero, every percentage in that group is undefined.
Input
The input consists of three sections: the survey description, the survey results, and the cross-table descriptions.
The first line is the survey name, at most 100 characters long. The following lines describe the questions. The first line of a question is a 3-character question code (capital letters and digits only), a space, and the question name (at most 80 characters). Each following line that describes one possible answer starts with a space, then a single-character answer code (a capital letter, a digit, or one of the characters ., *, @), a space, and an answer description (at most 40 characters). The list of questions ends with a line containing only the character #. Answer codes are unique within a question, and question codes are unique within the input. Every question has between 2 and 10 possible answers, and there are between 2 and 100 questions.
The next lines are the survey results. Each line has one character per question, in the order the questions appear, giving that person's answer code for each question, with no separators between characters. This section ends with a line containing only #. There is at least one result line, and the total number of recorded answers (result lines times questions) is at most 10000.
The next lines describe the cross tables to build. Each description is one line: the code of the first question, a space, the (different) code of the second question, a space, and the cross-table name (at most 100 characters). This section ends with a line containing only #. There are at most 100 cross-table descriptions.
No line has trailing spaces. No name begins or ends with a space, but names may contain spaces.
Output
For every cross-table description, in the order they appear in the input, write its cross table to the output.
On the first line of a cross table write the survey name, a space, a - (dash), a space, and the cross-table name. Then write the description of the first question and the description of the second question exactly as they appear in the input, in the same format. Then write an empty line, followed by the table itself.
The table has exactly lines and exactly characters on every line, where is the number of possible answers to the first question and is the number of possible answers to the second question.
The table has one heading line and rows of 3 lines each. The first rows correspond to the answers of the first question, in input order; the last row holds the column totals. The table has columns, each 6 characters wide: the first column holds row headings, the next columns correspond to the answers of the second question in input order, and the last column holds the row totals. Everything in every cell (including headings) is right-aligned and left-padded with spaces to a width of exactly 6 characters.
The heading of the first column is empty. The heading of each of the next columns is the second question's code, a : (colon), and the corresponding answer code. The heading of the last column is the word TOTAL. The heading of each of the first rows is the first question's code, a : (colon), and the corresponding answer code; the heading of the last row is the word TOTAL. A row heading appears on the first of that row's three lines; the heading column is blank on the other two lines.
For every non-heading cell, the first line holds the cross-table value, the second line holds the row-wise percent, and the third line holds the column-wise percent. Each percent is written as the rounded integer followed by a mandatory % character, or as a single - (dash) if that percent is undefined.
Separate consecutive cross tables in the output with a single empty line.