#C167. Find and Replace Words in a Document
Find and Replace Words in a Document
Find and Replace Words in a Document
You are given a document, a list of target words, and a corresponding list of replacement words. Your task is to replace every occurrence of each target word in the document with its corresponding replacement word. The replacements should be done in the order the words are provided.
For example, given a document D
, and target word t
with replacement r
, the updated document is obtained by replacing all occurrences of t
with r
.
In mathematical terms, for a target word \(t\) and its replacement \(r\), the transformation is:
\[ D := replace(D, t, r) \quad \text{for each pair } (t, r)\]Note that the replacement is case-sensitive.
inputFormat
The input is given via standard input (stdin) and has the following format:
D
n
t_1
t_2
...
t_n
r_1
r_2
...
r_n
Here, D
is a single line string representing the document. The integer n
denotes the number of target words. The next n
lines contain the target words t_1, t_2, ... , t_n
and the following n
lines contain the corresponding replacement words r_1, r_2, ... , r_n
.
outputFormat
The output should be printed to standard output (stdout) and is the updated document after all replacements have been made.
## sampleThe quick brown fox jumps over the lazy dog.
3
quick
brown
lazy
swift
dark
sluggish
The swift dark fox jumps over the sluggish dog.