Create a table of contents for a simple markup language. It must follow two rules:
- If a line starts with a single
#followed by a space, then it is a chapter title. - If a line starts with a double
##followed by a space, then it is a section title.
The table of contents should be displayed in the following format:
1. Title of the first chapter
1.1. Title of the first section of the first chapter
1.2. Title of the second section of the first chapter
...
2. Title of the second chapter
2.1. Title of the first section of the second chapter
2.2. Title of the second section of the second chapter
...
Note that each number is followed by a period and the last period is followed by 1 space.
For example, the input text is n = 10 lines long, where text is:
10
# Cars
Cars came into global use during the 20th century
Most definitions of car say they run primarily on roads
## Sedan
Sedan's first recorded use as a name for a car body was in 1912
## Coupe
A coupe is a passenger car with a sloping rear roofline and generally two doors
## SUV
The predecessors to SUVs date back to military and low-volume models from the late 1930s
There is no commonly agreed definition of an SUV, and usage varies between countries.
Sample Output
1. Cars
1.1. Sedan
1.2. Coupe
1.3. SUV
Explanation: The first line of input indicates there are n = 10 lines of text. There is only 1 chapter in the input, and it contains 3 sections. All lines that do not begin with # or ## are ignored in the table of contents.
This problem asks you to generate a table of contents from a lightweight markup text. Lines starting with `# ` become chapter titles, lines starting with `## ` become section titles, and all other lines are ignored. The main task is to scan the input once, keep track of chapter and section counters, and format the output exactly as `1. Title` and `1.1. Title`. A simple linear pass with string-prefix checks is enough.