Rebranding (Codeforces 591B)
Tags: Implement, strings

The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation.Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.Satisfy Arkady's curiosity and tell him the final version of the name.

Input: The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively.The second line consists of n lowercase English letters and represents the original name of the corporation.Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi.

Output: Print the new name of the corporation.

Note: In the second sample the name of the corporation consecutively changes as follows: 

Sample Inputs:
6 1
police
p m

11 6
abacabadaba
a b
b c
a d
e g
f a
b b

Sample Outputs:
molice

cdcbcdcfcdc

Solution (Python):
import sys
import string
read = lambda : list(map(int, sys.stdin.readline().split()))
(n, m) = read()
s = sys.stdin.readline().strip()
design = {c: c for c in string.ascii_lowercase}
for i in range(m):
    (a, b) = sys.stdin.readline().split()
    (design[a], design[b]) = (design[b], design[a])
design2 = {b: a for (a, b) in design.items()}
print(''.join((design2[c] for c in s)))

Solution (Python):
import string
(n, m) = map(int, input().split())
d = {i: i for i in string.ascii_lowercase}
a = [*input()]
for i in range(m):
    (x, y) = input().split()
    (d[x], d[y]) = (d[y], d[x])
d = {d[i]: i for i in d}
a = [d[i] for i in a]
print(''.join(a))

Solution (Python):
(n, m) = map(int, input().split())
s = list(input())
mp = {}
for i in range(n):
    if mp.get(s[i]) is None:
        mp[s[i]] = []
    mp[s[i]].append(i)
for _ in range(m):
    (x, y) = input().split()
    if mp.get(x) is None:
        mp[x] = []
    if mp.get(y) is None:
        mp[y] = []
    (mp[x], mp[y]) = (mp[y], mp[x])
res = [''] * n
for key in mp:
    for ele in mp[key]:
        res[ele] = key
print(''.join(res))