#C164. String Replacement Challenge

    ID: 44867 Type: Default 1000ms 256MiB

String Replacement Challenge

String Replacement Challenge

You are given a string text and a list of replacement rules. Each rule consists of a target substring and its replacement. Your task is to replace all occurrences of each target substring with its corresponding replacement substring. The replacements should be applied sequentially in the given order.

For example, if text is hello world and the rules are {('hello', 'hi'), ('world', 'everyone')}, the result should be hi everyone.

The problem can be formally described as follows:

Given a string \( T \) and a list of \( n \) pairs \( (s_i, r_i) \), for \( i = 1, 2, \ldots, n \), replace every occurrence of the substring \( s_i \) in \( T \) with \( r_i \). The replacements are applied in the order they are given.

inputFormat

The input is given via standard input (stdin) and has the following format:

  1. The first line contains the original string text.
  2. The second line contains an integer n which represents the number of replacement rules.
  3. The following n lines each contain two substrings separated by a space. The first substring is the target to be replaced and the second is its replacement.

outputFormat

Output the modified string after all the replacement rules have been applied. The output is printed to standard output (stdout).

## sample
hello world
2
hello hi
world everyone
hi everyone