ICPC 2018 - B. Comma Sprinkler
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
Source-first archive entry
This page is built from the copied files in competitive_programming/icpc/2018/B-comma-sprinkler. Edit
competitive_programming/icpc/2018/B-comma-sprinkler/solution.tex to update the written solution and
competitive_programming/icpc/2018/B-comma-sprinkler/solution.cpp to update the implementation.
The website does not replace those files with hand-maintained HTML. It reads the copied source tree during the build and exposes the exact files below.
Problem Statement
Copied statement text kept beside the solution archive for direct reference.
Problem B
Comma Sprinkler
Time limit: 8 seconds
As practice will tell you, the English rules for comma placement are complex, frus-
trating, and often ambiguous. Many people, even the English, will, in practice,
ignore them, and, apply custom rules, or, no rules, at all.
Doctor Comma Sprinkler solved this issue by developing a set of rules that sprinkles
commas in a sentence with no ambiguity and little simplicity. In this problem you
will help Dr. Sprinkler by producing an algorithm to automatically apply her rules.
Dr. Sprinkler’s rules for adding commas to an existing piece of text are as follows: Photo by Tanya Hart. Yarn
pattern by Morgen Dämmerung.
1. If a word anywhere in the text is preceded by a comma, find all occurrences of that word in the text,
and put a comma before each of those occurrences, except in the case where such an occurrence
is the first word of a sentence or already preceded by a comma.
2. If a word anywhere in the text is succeeded by a comma, find all occurrences of that word in the
text, and put a comma after each of those occurrences, except in the case where such an occurrence
is the last word of a sentence or already succeeded by a comma.
3. Apply rules 1 and 2 repeatedly until no new commas can be added using either of them.
As an example, consider the text
please sit spot. sit spot, sit. spot here now here.
Because there is a comma after spot in the second sentence, a comma should be added after spot in
the third sentence as well (but not the first sentence, since it is the last word of that sentence). Also,
because there is a comma before the word sit in the second sentence, one should be added before that
word in the first sentence (but no comma is added before the word sit beginning the second sentence
because it is the first word of that sentence). Finally, notice that once a comma is added after spot
in the third sentence, there exists a comma before the first occurrence of the word here. Therefore, a
comma is also added before the other occurrence of the word here. There are no more commas to be
added so the final result is
please, sit spot. sit spot, sit. spot, here now, here.
Input
The input contains one line of text, containing at least 2 characters and at most 1 000 000 characters.
Each character is either a lowercase letter, a comma, a period, or a space. We define a word to be a
maximal sequence of letters within the text. The text adheres to the following constraints:
• The text begins with a word.
• Between every two words in the text, there is either a single space, a comma followed by a space,
or a period followed by a space (denoting the end of a sentence and the beginning of a new one).
• The last word of the text is followed by a period with no trailing space.
Output
Display the result after applying Dr. Sprinkler’s algorithm to the original text.
Sample Input 1
please sit spot. sit spot, sit. spot here now here.
Sample Output 1
please, sit spot. sit spot, sit. spot, here now, here.
Sample Input 2
one, two. one tree. four tree. four four. five four. six five.
Sample Output 2
one, two. one, tree. four, tree. four, four. five, four. six five.
Editorial
Rendered from the copied solution.tex file. The original TeX source remains
available below.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
Exact copied C++ implementation from solution.cpp.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files
Exact copied source-of-truth files. Edit solution.tex for the write-up and solution.cpp for the implementation.
\documentclass[11pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amssymb,amsthm}
\usepackage{enumitem}
\title{ICPC World Finals 2018\\B. Comma Sprinkler}
\author{}
\date{}
\begin{document}
\maketitle
\section*{Problem Summary}
State the problem in your own words. Focus on the mathematical or algorithmic core rather than repeating the full statement.
\section*{Key Observations}
\begin{itemize}[leftmargin=*]
\item Write the structural observations that make the problem tractable.
\item State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
\item If the constraints matter, explain exactly which part of the solution they enable.
\end{itemize}
\section*{Algorithm}
\begin{enumerate}[leftmargin=*]
\item Describe the data structures and the state maintained by the algorithm.
\item Explain the processing order and why it is sufficient.
\item Mention corner cases explicitly if they affect the implementation.
\end{enumerate}
\section*{Correctness Proof}
We prove that the algorithm returns the correct answer.
\paragraph{Lemma 1.}
State the first key claim.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Lemma 2.}
State the next claim if needed.
\paragraph{Proof.}
Provide a concise proof.
\paragraph{Theorem.}
The algorithm outputs the correct answer for every valid input.
\paragraph{Proof.}
Combine the lemmas and finish the argument.
\section*{Complexity Analysis}
State the running time and memory usage in terms of the input size.
\section*{Implementation Notes}
\begin{itemize}[leftmargin=*]
\item Mention any non-obvious implementation detail that is easy to get wrong.
\item Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
\end{itemize}
\end{document}
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}