P2395 BBCode to Markdown

Background

BBCode and Markdown are two commonly used formatting languages. A formatting language means, for example, when posting a comment on some websites, you can use it to style your comment so that text can have colors, boldface, and various other styles.

Description

#### ~~PDF version of the problem: http://pan.baidu.com/s/1i3mxk4t Extraction code hayc~~ ### 2025-7-17: The link is down. The syntax of BBCode is as follows: ```plain [h1]Hello World![/h1] [h2][i]This is a BBCode[/i][/h2] [b][i]I love[/i] Olympic [i]Informatics[/i][/b] ``` For example, in the tag `[h1]`, the content wrapped between `[h1]` and `[/h1]` is a level-1 heading, and the text within should be rendered in the style of a level-1 heading. Please note that BBCode requires tags to be properly opened and closed and nested correctly, that is, the following examples are all invalid BBCode: ```plain [h1]Hello World! // tag not closed [h1]Hello World![/h2] // two tags do not match [h1][i]Hello World![/h1][/i] // incorrect nesting order ``` In addition, a very important point is that BBCode has a special tag [quote][/quote]. Any text inside it should not be parsed as BBCode code but should be converted to Markdown and then output verbatim. Now, introducing Markdown. Its syntax is as follows: ```cpp # Hello World! # ## *This is a Markdown* ## __*I love* Olympic *Informatics*__ ``` Your task is to convert a given piece of BBCode into Markdown and report errors for invalid input. All BBCode tags that will appear in the testdata and their corresponding Markdown counterparts are shown below. No other mappings will appear: ![](https://cdn.luogu.com.cn/upload/pic/1493.png) Additional notes for the `[quote]` tag: ![](https://cdn.luogu.com.cn/upload/pic/1494.png)

Input Format

The BBCode that needs to be converted.

Output Format

N/A

Explanation/Hint

To ensure smooth judging and avoid misjudgment, please pay attention to the following requirements: - Respect the original line breaks in the input and output. Do not add or remove line breaks on your own. - The exception to the above: when encountering a `[quote]` tag that is not on a new line, please start a new line in Markdown. Also, please remove the leading and trailing blank lines of the code block inside the `[quote]` tag. There will be no empty quotes. - Please pay attention to the spaces in the table above. - For invalid BBCode input, your program must report an error: - For mismatched tags, output `Match Error`. - For unclosed tags, output `Unclosed Mark`. Please refer to the samples. - When both situations occur at the same time (that is, both a matching error and unclosed tags), handle it as a matching error and output `Match Error`. - Due to the special nature of the quote tag, to avoid ambiguity, the testdata guarantees that the quote tag will not be erroneous. - For example: for a case like `[i][h1]Text[/i]`, handle it as `Match Error`. For a case like `[i][h1]Text[/h1]`, handle it as `Unclosed Mark`. - Due to Luogu limitations, when outputting `Unclosed Mark`, please split `close` in the middle into two separate strings, otherwise the record will be swallowed. - It is guaranteed that the following characters will not appear in text outside segments wrapped by the quote tag: `[` `]` `/` `*` `_` `#` `>`. However, the `/` character may appear in URLs. - The input will not contain incomplete tags, such as `[h1]Text[/h`. - For those who have used Markdown and BBCode elsewhere: the formats in this problem are not strictly the standard BBCode or Markdown formats. Please do not be misled by prior experience. Translated by ChatGPT 5