Under construction

Still being built. Things may change

HTML

Interview

Time limit1sMemory limit128 MB

Summary
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,123 is a single word, whereas abc, 123 is the two words abc, and 123.
  • 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.

Examples3

  1. Example 1

    Input
    Hallo, dies ist eine 
    ziemlich lange Zeile, die in Html
    aber nicht umgebrochen wird.
    <br>
    Zwei <br> <br> produzieren zwei Newlines. 
    Es gibt auch noch das tag <hr> was einen Trenner darstellt.
    Zwei <hr> <hr> produzieren zwei Horizontal Rulers.
    Achtung       mehrere Leerzeichen irritieren
    
    Html genauso wenig wie
    
    
    mehrere Leerzeilen.
    
    Expected output
    Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochen
    wird.
    Zwei
    
    produzieren zwei Newlines. Es gibt auch noch das tag
    --------------------------------------------------------------------------------
    was einen Trenner darstellt. Zwei
    --------------------------------------------------------------------------------
    --------------------------------------------------------------------------------
    produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Html
    genauso wenig wie mehrere Leerzeilen.
    
  2. Example 2

    Input
    Hello world
    
    Expected output
    Hello world
    
  3. Example 3

    Input
    a <br> <br> b
    
    Expected output
    a
    
    b