ICPC 2009 - K. Suffix-Replacement Grammars
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/2009/K-suffix-replacement-grammars. Edit
competitive_programming/icpc/2009/K-suffix-replacement-grammars/solution.tex to update the written solution and
competitive_programming/icpc/2009/K-suffix-replacement-grammars/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 K
Suffix-Replacement Grammars
Input file: suffix.in
As computer programmers, you have likely heard about regular expressions and context-free grammars. These
are rich ways of generating sets of strings over a small alphabet (otherwise known as a formal language). There
are other, more esoteric ways of generating languages, such as tree-adjoining grammars, context-sensitive
grammars, and unrestricted grammars. This problem uses a new method for generating a language: a suffix-
replacement grammar.
A suffix-replacement grammar consists of a starting string S and a set of suffix-replacement rules. Each rule is
of the form X Æ Y, where X and Y are equal-length strings of alphanumeric characters. This rule means that if
the suffix (that is, the rightmost characters) of your current string is X, you can replace that suffix with Y. These
rules may be applied arbitrarily many times.
For example, suppose there are 4 rules A Æ B, AB Æ BA, AA Æ CC, and CC Æ BB. You can then transform
the string AA to BB using three rule applications: AA Æ AB (using the A Æ B rule), then AB Æ BA (using the
AB Æ BA rule), and finally BA Æ BB (using the AÆ B rule again). But you can also do the transformation
more quickly by applying only 2 rules: AA Æ CC and then CC Æ BB.
You must write a program that takes a suffix-replacement grammar and a string T and determines whether the
grammar’s starting string S can be transformed into the string T. If this is possible, the program must also find
the minimal number of rule applications required to do the transformation.
Input
The input consists of several test cases. Each case starts with a line containing two equal-length alphanumeric
strings S and T (each between 1 and 20 characters long, and separated by whitespace), and an integer NR
(0 ≤ NR ≤ 100), which is the number of rules. Each of the next NR lines contains two equal-length alphanumeric
strings X and Y (each between 1 and 20 characters long, and separated by whitespace), indicating that X Æ Y is a
rule of the grammar. All strings are case-sensitive. The last test case is followed by a line containing a period.
Output
For each test case, print the case number (beginning with 1) followed by the minimum number of rule
applications required to transform S to T. If the transformation is not possible, print No solution. Follow the
format of the sample output.
Sample Input Output for the Sample Input
AA BB 4 Case 1: 2
A B Case 2: No solution
AB BA
AA CC
CC BB
A B 3
A C
B C
c B
.
This page intentionally left blank.
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 2009\\K. Suffix-Replacement Grammars}
\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;
}