#P6942. Dr. Comma Sprinkler
Dr. Comma Sprinkler
Dr. Comma Sprinkler
Dr. Comma Sprinkler has devised a unique way to eliminate the ambiguity in comma placement by planting commas in a sentence automatically using a simple set of rules. Given a text consisting of multiple sentences, your task is to add commas according to the following rules:
- If any occurrence of a word is preceded by a comma (i.e. the word token starts with a comma), then in every occurrence of that word in the text, add a comma immediately before it unless it is the first word of a sentence or already has a comma before it.
- If any occurrence of a word is succeeded by a comma (i.e. the word token ends with a comma), then in every occurrence of that word in the text, add a comma immediately after it unless it is the last word of a sentence or already has a comma after it.
- Apply rules (1) and (2) repeatedly until no new commas can be added.
Note: Treat commas that are added to the beginning or end of a word as attributes. Do not insert extra commas if one is already present, and keep the sentence boundaries intact.
For example, given the text:
please sit spot. sit spot, sit. spot here now here.
The final processed text should be:
please, sit spot. sit spot, sit. spot, here now, here.
inputFormat
The input will consist of a single text containing one or more sentences. Each sentence ends with a period ('.') and words within sentences are separated by spaces. Some words might already have a comma either immediately before or immediately after them. You must read the entire text from standard input.
outputFormat
Output the transformed text after applying Dr. Comma Sprinkler's rules. The sentences must remain in the same order, with a period at the end of each sentence.
sample
please sit spot. sit spot, sit. spot here now here.
please, sit spot. sit spot, sit. spot, here now, here.