B. INI-file

Codeforces
IDCF72B
Time5000ms
Memory256MB
Difficulty
implementation
English · Original
Chinese · Translation
Formal · Original
The INI file format is a de facto standard for configuration files. INI files are simple text files with a basic structure. They are commonly associated with Microsoft Windows, but are also used on other platforms. Each line in INI-file stands for key-value mapping or defines new section. A key-value line has a format "_key=value_",where _key_ — is the name of some property, and _value_ — it's value. It is possible that it will be spaces from the both sides of _key_ and/or _value_, the spaces should be ignored. A section line has a format "_\[section\]_". It means that all key-value lines after it define properties of the specified section. Of cause, the following section line changes the current section. A section line may have spaces around any of brackets. Also you should ignore comment lines — the first non-space character of comment line is "_;_". You task is to write the program which will format given INI-file in a special way: * first, print key-value lines which do not belong to any section; * print all the sections in the lexicographical (alphabetical) order of their names; * inside each of two previous items, order key-value lines lexicographically by "_key_"; * if there are more than one key-value lines with the same key inside a single section (or outside any sections), leave only one line (which appears later in the input data); * remove all redundant spaces and lines. ## Input The first line contains single integer _n_ (1 ≤ _n_ ≤ 510) — the number of lines in given INI-file. The rest of the input contains a valid INI-file in _n_ lines. Values of _section_, _key_ and _value_ contain only Latin letters, digits, "_._" and/or "_\-_". Each line has length not exceeding 255 characters and not less than 1 character. The total length of all the lines does’t exceed 10000. ## Output Print formatted INI-file. [samples]
INI 文件格式是配置文件的事实标准。INI 文件是具有基本结构的简单文本文件。它们通常与 Microsoft Windows 关联,但也用于其他平台。 INI 文件中的每一行要么表示键值映射,要么定义新节。键值行的格式为 "_key=value_",其中 _key_ 是某个属性的名称,_value_ 是其值。_key_ 和/或 _value_ 两侧可能存在空格,这些空格应被忽略。 节行的格式为 "_[section]_"。它表示其后的所有键值行都定义指定节的属性。当然,后续的节行会更改当前节。节行的任意括号周围可能包含空格。 此外,您应忽略注释行——注释行中第一个非空格字符为 "_;_"。 您的任务是编写一个程序,以特定方式格式化给定的 INI 文件: 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 510])——表示给定 INI 文件的行数。 其余输入包含 #cf_span[n] 行的有效 INI 文件。_section_、_key_ 和 _value_ 的值仅包含拉丁字母、数字、"_._" 和/或 "_-_"。 每行长度不超过 255 个字符,且不少于 1 个字符。所有行的总长度不超过 10000。 请输出格式化后的 INI 文件。 ## Input 第一行包含一个整数 #cf_span[n](#cf_span[1 ≤ n ≤ 510])——表示给定 INI 文件的行数。其余输入包含 #cf_span[n] 行的有效 INI 文件。_section_、_key_ 和 _value_ 的值仅包含拉丁字母、数字、"_._" 和/或 "_-_"。每行长度不超过 255 个字符,且不少于 1 个字符。所有行的总长度不超过 10000。 ## Output 请输出格式化后的 INI 文件。 [samples]
**Definitions** Let $ n \in \mathbb{Z} $ be the number of lines in the INI file, with $ 1 \leq n \leq 510 $. Let $ L = (l_1, l_2, \dots, l_n) $ be the sequence of input lines, where each $ l_i $ is a string of length $ \in [1, 255] $. Let $ \mathcal{S} $ be the set of section names. Let $ \mathcal{K} $ be the set of key names. Let $ \mathcal{V} $ be the set of value strings. Each section, key, and value consists only of characters from $ \Sigma = \{ \text{a-z}, \text{A-Z}, \text{0-9}, \text{.}, \text{-} \} $. Let $ \text{section} : \{1, \dots, n\} \to \mathcal{S} \cup \{\bot\} $ be a function mapping line index to its current section (or $ \bot $ if no section is active). Let $ \text{entries} : \mathcal{S} \times \mathcal{K} \to \mathcal{V} $ be a partial function mapping (section, key) to value. **Constraints** 1. Each line $ l_i $ is one of the following: - **Section line**: $ l_i = \texttt{[} s \texttt{]} $, where $ s \in \mathcal{S} $, possibly surrounded by arbitrary whitespace. - **Key-value line**: $ l_i = \texttt{key} \texttt{=} \texttt{value} $, where $ \texttt{key} \in \mathcal{K} $, $ \texttt{value} \in \mathcal{V} $, possibly surrounded by arbitrary whitespace. - **Comment line**: The first non-space character of $ l_i $ is `;`. - **Empty line**: Contains only whitespace. 2. Section lines set the active section; subsequent key-value lines belong to the most recently defined section. 3. Whitespace surrounding section names, keys, and values must be stripped. 4. Comment lines and empty lines are ignored in output. **Objective** Output a formatted INI file such that: - All section lines appear in the order of their first occurrence, with exactly one blank line preceding each section line (except the first). - All key-value pairs under a section are grouped together, sorted lexicographically by key. - Each key-value pair is formatted as `key=value` with no leading or trailing whitespace. - No comment lines or empty lines are included in the output. - No duplicate keys within the same section; if a key is redefined, the last value takes precedence. - Sections are output in the order they first appear.
Samples
Input #1
11
a= 1
b=a
a = 2
 ; comment
\[z\]
1=2
\[y\]
2=3
\[z\]
2=1
\[w\]
Output #1
a=2
b=a
\[w\]
\[y\]
2=3
\[z\]
1=2
2=1
API Response (JSON)
{
  "problem": {
    "name": "B. INI-file",
    "description": {
      "content": "The INI file format is a de facto standard for configuration files. INI files are simple text files with a basic structure. They are commonly associated with Microsoft Windows, but are also used on ot",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF72B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The INI file format is a de facto standard for configuration files. INI files are simple text files with a basic structure. They are commonly associated with Microsoft Windows, but are also used on ot...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "INI 文件格式是配置文件的事实标准。INI 文件是具有基本结构的简单文本文件。它们通常与 Microsoft Windows 关联,但也用于其他平台。\n\nINI 文件中的每一行要么表示键值映射,要么定义新节。键值行的格式为 \"_key=value_\",其中 _key_ 是某个属性的名称,_value_ 是其值。_key_ 和/或 _value_ 两侧可能存在空格,这些空格应被忽略。\n\n节行的格式...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the number of lines in the INI file, with $ 1 \\leq n \\leq 510 $.  \nLet $ L = (l_1, l_2, \\dots, l_n) $ be the sequence of input lines, where each $ l_i $ i...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments