#abc342c. C - Many Replacement

C - Many Replacement

Score: 350350 points

问题描述

你将得到一个长度为 NN 的由小写英文字母组成的字符串 SS

你需要对字符串 SS 执行 QQ 次操作。第 ii 次操作 (1iQ)(1\leq i\leq Q) 由一对字符 (ci,di)(c _ i,d _ i) 表示,对应以下操作:

  • 将字符串 SS 中所有出现的字符 cic _ i 替换为字符 did _ i

在所有操作完成后,输出字符串 SS

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a string SS of length NN consisting of lowercase English letters.

You will perform an operation QQ times on the string SS. The ii-th operation (1iQ)(1\leq i\leq Q) is represented by a pair of characters (ci,di)(c _ i,d _ i), which corresponds to the following operation:

  • Replace all occurrences of the character cic _ i in SS with the character did _ i.

Print the string SS after all operations are completed.

Constraints

  • 1N2×1051\leq N\leq2\times10^5
  • SS is a string of length NN consisting of lowercase English letters.
  • 1Q2×1051\leq Q\leq2\times10^5
  • cic _ i and did _ i are lowercase English letters (1iQ)(1\leq i\leq Q).
  • NN and QQ are integers.

Input

The input is given from Standard Input in the following format:

NN

SS

QQ

c1c _ 1 d1d _ 1

c2c _ 2 d2d _ 2

\vdots

cQc _ Q dQd _ Q

Output

Print the string SS after all operations are completed.

Sample Input 1

7
atcoder
4
r a
t e
d v
a r

Sample Output 1

recover

SS changes as follows: atcoderatcodeaaecodeaaecovearecover. For example, in the fourth operation, all occurrences of a in S=S={}aecovea (the first and seventh characters) are replaced with r, resulting in S=S={}recover.

After all operations are completed, S=S={}recover, so print recover.

Sample Input 2

3
abc
4
a a
s k
n n
z b

Sample Output 2

abc

There may be operations where ci=dic _ i=d _ i or SS does not contain cic _ i.

Sample Input 3

34
supercalifragilisticexpialidocious
20
g c
l g
g m
c m
r o
s e
a a
o f
f s
e t
t l
d v
p k
v h
x i
h n
n j
i r
s i
u a

Sample Output 3

laklimamriiamrmrllrmlrkramrjimrial

update @ 2024/3/10 01:36:09