B. Sequence Formatting

Codeforces
IDCF81B
Time2000ms
Memory256MB
Difficulty
implementationstrings
English · Original
Chinese · Translation
Formal · Original
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "_1,2 ,3,..., 10_" will be corrected to "_1, 2, 3, ..., 10_". In this task you are given a string _s_, which is composed by a concatination of terms, each of which may be: * a positive integer of an arbitrary length (leading zeroes are not allowed), * a "comma" symbol ("_,_"), * a "space" symbol (" "), * "three dots" ("_..._", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string _s_ to ensure the following: * each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), * each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), * if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, * there should not be other spaces. Automate Polycarp's work and write a program that will process the given string _s_. ## Input The input data contains a single string _s_. Its length is from 1 to 255 characters. The string _s_ does not begin and end with a space. Its content matches the description given above. ## Output Print the string _s_ after it is processed. Your program's output should be _exactly_ the same as the expected answer. It is permissible to end output line with a line-break character, and without it. [samples]
Polycarp 非常细心。他甚至会像他的同学那样仔细地输入数字序列。如果他看到一个序列中逗号后没有空格、连续有两个空格,或出现其他不整齐的情况,他会立即纠正。例如,序列 "_1,2 ,3,...,   10_" 会被纠正为 "_1, 2, 3, ..., 10_"。 在本题中,给定一个字符串 #cf_span[s],它由若干项连接而成,每项可能是: Polycarp 希望通过添加和删除空格,使字符串 #cf_span[s] 满足以下要求: 请自动化 Polycarp 的工作,编写一个程序处理给定的字符串 #cf_span[s]。 输入数据包含一个字符串 #cf_span[s]。其长度为 1 到 255 个字符。字符串 #cf_span[s] 不以空格开头或结尾,其内容符合上述描述。 请输出处理后的字符串 #cf_span[s]。你的程序输出必须与预期答案完全一致。允许输出行以换行符结尾,也可以不以换行符结尾。 ## Input 输入数据包含一个字符串 #cf_span[s]。其长度为 1 到 255 个字符。字符串 #cf_span[s] 不以空格开头或结尾,其内容符合上述描述。 ## Output 请输出处理后的字符串 #cf_span[s]。你的程序输出必须与预期答案完全一致。允许输出行以换行符结尾,也可以不以换行符结尾。 [samples]
**Definitions** Let $ s \in \{ \text{ASCII characters} \}^* $ be the input string of length $ 1 \leq |s| \leq 255 $, with $ s \notin \text{leading or trailing space} $, and containing only digits, commas, dots, spaces, and possibly the substring `...`. **Constraints** 1. $ s $ contains terms separated by commas (`,`) and/or ellipses (`...`). 2. Terms are numeric sequences (digits only) or `...`. 3. No leading or trailing spaces in $ s $. 4. Spaces may be missing after commas, or multiple consecutive spaces may appear. **Objective** Transform $ s $ into $ s' $ such that: - Every comma (`,`) is followed by exactly one space. - Every ellipsis (`...`) is preceded and followed by exactly one space. - All other spaces are removed, except the single space required after commas and around ellipses. - No leading or trailing spaces in $ s' $. - No two consecutive spaces in $ s' $. Formally, $ s' $ is the unique string satisfying: $$ s' = \text{normalize}(s) $$ where `normalize` applies the following rules in order: 1. Replace all occurrences of `...` with a placeholder (e.g., `__ELL__`) to avoid ambiguity. 2. Remove all spaces. 3. Insert a single space after every comma. 4. Insert a single space before and after every `...` (replacing the placeholder). 5. Trim leading/trailing spaces (though guaranteed not to exist). Output $ s' $.
Samples
Input #1
1,2 ,3,...,     10
Output #1
1, 2, 3, ..., 10
Input #2
1,,,4...5......6
Output #2
1, , , 4 ...5 ... ...6
Input #3
...,1,2,3,...
Output #3
..., 1, 2, 3, ...
API Response (JSON)
{
  "problem": {
    "name": "B. Sequence Formatting",
    "description": {
      "content": "Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF81B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Polycarp 非常细心。他甚至会像他的同学那样仔细地输入数字序列。如果他看到一个序列中逗号后没有空格、连续有两个空格,或出现其他不整齐的情况,他会立即纠正。例如,序列 \"_1,2 ,3,...,   10_\" 会被纠正为 \"_1, 2, 3, ..., 10_\"。\n\n在本题中,给定一个字符串 #cf_span[s],它由若干项连接而成,每项可能是:\n\nPolycarp 希望通过添加和删除空格,...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ s \\in \\{ \\text{ASCII characters} \\}^* $ be the input string of length $ 1 \\leq |s| \\leq 255 $, with $ s \\notin \\text{leading or trailing space} $, and containing only digits, c...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments