#K5116. Case-Sensitive Word Replacement
Case-Sensitive Word Replacement
Case-Sensitive Word Replacement
In this problem, you are tasked with implementing a case-insensitive word replacement feature with a twist: the replacement word must preserve the case of the first letter of the original occurrence. This feature is designed for text editing applications where precise text modifications are needed.
Given a list of replacement rules and an input text, every occurrence of a word that appears in the rules (matched in a case‐insensitive manner) should be replaced by its corresponding replacement. Moreover, if the found word starts with an uppercase letter then the replacement must also start with an uppercase letter; if the word is in all uppercase, then the replacement should be converted to all uppercase. Otherwise, the replacement is used as provided.
You can consider the matching to be performed using the following regular expression in \(\LaTeX\): pattern = (?i)(word1|word2|...|wordT)
, where the ignore case flag is active.
inputFormat
The input is read from standard input (stdin) and has the following format:
T rule1 rule2 ... ruleT N line1 line2 ... lineN
Here, the first line contains an integer \(T\) denoting the number of replacement rules. Each of the following \(T\) lines contains two words separated by a space: the word to be replaced and the word to replace it with. The next line contains an integer \(N\) which denotes the number of lines of text. The following \(N\) lines form the original text in which the replacements should be performed. The text may span multiple lines.
outputFormat
The output is written to standard output (stdout) and is the modified text after all replacements have been made. The output should exactly match the expected text including any newline characters.
## sample1
hello hi
1
Hello world!
Hi world!