HTML
InterviewTime limit1sMemory limit128 MB
Render an HTML fragment of plain text with <br> and <hr> tags, wrapping lines at 80 characters.
- Level
Easy3 of 10
- Topics
- String, Implementation, Simulation, Brute force
- Solved
- No attempts yet
Problem
You are given an HTML document that consists only of <br> and <hr> tags together with plain text. Write a program that produces the rendered result of this document according to the rules below.
No output line may contain more than 80 characters.
Input
The original HTML document is given as input. It is made up of words and HTML tags, and each token (a word or a tag) is separated by one or more spaces, tabs, or newline characters.
- A word is a run of consecutive letters, digits, or punctuation marks. For example,
abc,123is a single word, whereasabc, 123is the two wordsabc,and123. - A word is always at most 80 characters long and never contains the characters
<or>. - The only tags that appear in the input are
<br>and<hr>.
Output
Print the result according to the following rules.
- If appending the next word to the current line would keep the line length within 80 characters, print it on the current line, separated from the previous word by a single space. If it would exceed 80 characters, print that word on a new line instead.
- When a
<br>tag is read, finish the current line and start a new one. - When an
<hr>tag is read, first move to a new line if the current line is not empty, then print exactly 80-characters on their own line. After that, start a new line again. - The last line ends with a newline character.
- Any run of consecutive newlines, spaces, or tabs is treated as a single space.