Given a text, repeatedly add commas before or after every occurrence of a word that already has a comma on that side, until nothing changes; print the result.
Medium6GraphBFSStringImplementationNo attempts yetTime limit8sMemory limit1024 MBThe English rules for comma placement are complicated, and they are often ambiguous. Some people ignore them, some invent their own, and some use no commas at all.
Doctor Comma Sprinkler removed the ambiguity with a fixed procedure. Her rules for adding commas to a piece of text that is already written are these.
Write a program that applies the procedure to the given text.
The first example shows how the rules chain. In the second sentence spot is followed by a comma, so a comma goes after spot in the third sentence as well. The spot in the first sentence stays as it is, because it is the last word of that sentence. The sit in the second sentence has a comma before it, so a comma goes before the sit in the first sentence, but not before the sit that opens the second sentence. Once the spot in the third sentence takes a comma, the first here has a comma before it, so the second here takes one too. After that no rule adds anything.
The first and only line contains the text. It has at least 2 and at most 1,000,000 characters, and every character is a lowercase letter, a comma, a period, or a space. A word is a maximal run of letters within the text.
The text satisfies the following.
Print the text after Doctor Sprinkler's procedure has been applied to it.