Time limit
1s
Memory limit
128 MB
You are given screen layouts written as nested HTML tables. Convert each layout into one HTML table that preserves the same relative row and column positions.
This task uses only a very small subset of HTML. A document starts with <body> and ends with </body>; between them, each line contains one table. A table has one or more rows between <table> and </table>. Each row has one or more cells between <tr> and </tr>. The contents of a cell are either one alphanumeric text token or one nested table.
In a general table, cells can be omitted when a cell above or to the left spans over their positions. The input layouts do not use row or column span attributes: every input cell starts with exactly <td>. Instead, layouts are built by putting tables inside cells.
Within any one table, there is never more than one nested table in the same row, and never more than one nested table in the same column. Nesting may still continue for several levels.
For every input table, output an equivalent single table with no nested tables. Add rowspan and colspan attributes where necessary so the flattened table represents the same row and column relationships as the original nested layout. Actual cell sizes and shapes are irrelevant.
The input starts with a line containing <body> and ends with a line containing </body>. Each line between them contains one nested table layout.
Each table line has at most 10,000 characters. It consists only of <table>, <tr>, <td>, </td>, </tr>, </table>, and alphanumeric text tokens, and it forms a valid table.
Input tables contain no row or column span attributes, but they may contain nested tables. The nesting depth is at most 10. The equivalent flattened table has at least 1 row and 1 column and at most 100 rows and 100 columns. Each text token has at most 100 alphanumeric characters.
Output a line containing <body>, then one flattened table line for each input table line, then a line containing </body>.
Each output table must be valid and may contain only text inside cells; it must not contain nested tables. All tags must be lowercase. When a cell needs a span, use rowspan="NUMBER" and colspan="NUMBER"; if both are needed, print them in that order. Omit any span attribute whose value would be 1.