← 返回 jpmorgan 的题目列表Generate Table of Contents from Markdown-like Headings
类型:online_judge
Problem: Generate a Table of Contents (TOC) from Document Lines
You are given an array of strings lines, representing a document line by line from top to bottom. Generate a Table of Contents according to the rules:
A line starting with # (one hash + a space) is a chapter.
The chapter title is the remainder after removing # .
A line starting with ## (two hashes + a space) is a section.
A section belongs to the most recent chapter above it.
The section title is the remainder after removing ## .
All other lines are ignored.
Numbering rules:
Chapters are numbered from 1 in appearance order. Output: "{chapterIndex}. {chapterTitle}".
Sections are numbered from 1 within each chapter. Output: "{chapterIndex}.{sectionIndex}. {sectionTitle}".
Return (or print) the TOC entries in scanning order.
Edge cases
If a ## section appears before any chapter, ignore it.
Input
First line: integer n (number of lines)
Next n lines: one string per line
Output
Multiple lines, each a TOC entry in order
Constraints
1 <= n <= 2 * 10^5
Each line length <= 2000
Example
Input:
6
# Intro
some text
## Goal
# Next
## Details
end
Output:
1. Intro
1.1. Goal
2. Next
2.1. Details
Example
Input
1
# A
Output
1. A