QOJ.ac

QOJ

時間限制: 8 s 記憶體限制: 2048 MB 總分: 100

#2428. 逗号喷洒器

统计

正如实践所言,英语中关于逗号放置的规则复杂、令人沮丧且往往模棱两可。许多人,甚至包括英国人,在实践中都会忽略它们,并应用自定义规则,或者根本不使用任何规则。

Tanya Hart 拍摄。Morgen Dämmerung 设计的纱线图案。

Comma Sprinkler 博士通过制定一套规则解决了这个问题,这些规则可以在句子中毫无歧义且简洁地添加逗号。在这个问题中,你将通过编写一个算法来自动应用她的规则,从而帮助 Sprinkler 博士。

Sprinkler 博士为在现有文本中添加逗号的规则如下:

  1. 如果文本中某个单词的前面有一个逗号,则找到文本中该单词的所有出现位置,并在这些出现位置的前面都加上一个逗号,除非该位置是句子的第一个单词,或者该位置前面已经有了逗号。
  2. 如果文本中某个单词的后面有一个逗号,则找到文本中该单词的所有出现位置,并在这些出现位置的后面都加上一个逗号,除非该位置是句子的最后一个单词,或者该位置后面已经有了逗号。
  3. 重复应用规则 1 和规则 2,直到无法再使用这两条规则添加任何新的逗号为止。

例如,考虑以下文本: please sit spot. sit spot, sit. spot here now here.

由于第二个句子中 spot 后面有一个逗号,因此第三个句子中的 spot 后面也应该加上一个逗号(但第一个句子中的 spot 不加,因为它是该句的最后一个单词)。此外,由于第二个句子中单词 sit 前面有一个逗号,因此第一个句子中该单词前面也应该加上一个逗号(但第二个句子开头的 sit 前面不加逗号,因为它是该句的第一个单词)。最后,请注意,一旦第三个句子中 spot 后面加上了逗号,那么单词 here 的第一次出现前面就存在了一个逗号。因此,单词 here 的另一次出现前面也加上了逗号。此时没有更多的逗号可以添加,所以最终结果为: please, sit spot. sit spot, sit. spot, here now, here.

输入格式

输入包含一行文本,至少包含 2 个字符,最多包含 1 000 000 个字符。每个字符要么是小写字母,要么是逗号、句号或空格。我们将“单词”定义为文本中连续字母构成的最大序列。文本遵循以下约束:

  • 文本以一个单词开头。
  • 文本中每两个单词之间,要么是一个空格,要么是一个逗号后跟一个空格,要么是一个句号后跟一个空格(表示一个句子的结束和新句子的开始)。
  • 文本的最后一个单词后面跟有一个句号,且没有尾随空格。

输出格式

显示应用 Sprinkler 博士的算法处理原始文本后的结果。

样例

输入格式 1

please sit spot. sit spot, sit. spot here now here.

输出格式 1

please, sit spot. sit spot, sit. spot, here now, here.

输入格式 2

one, two. one tree. four tree. four four. five four. six five.

输出格式 2

one, two. one, tree. four, tree. four, four. five, four. six five.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.