D. Love Rescue
time limit per test

2 seconds

memory limit per test

256 megabytes


standard input


standard output

Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn’t want to see him and Tolya is seating at his room and crying at her photos all day long.

This story could be very sad but fairy godmother (Tolya’s grandmother) decided to help them and restore their relationship. She secretly took Tolya’s t-shirt and Valya’s pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya’s grandmother should spend to rescue love of Tolya and Valya.

More formally, letterings on Tolya’s t-shirt and Valya’s pullover are two strings with same length nconsisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya’s t-shirt and Valya’s pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.


The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.

The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya’s pullover.

The third line contains the lettering on Tolya’s t-shirt in the same format.


In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.

In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya’s grandmother should buy. Spells and letters in spells can be printed in any order.

If there are many optimal answers, output any.



a d
b a


l e
e d
d c
c p
p o
o r
r a

In first example it’s enough to buy two spells: (‘a‘,’d‘) and (‘b‘,’a‘). Then first letters will coincide when we will replace letter ‘a‘ with ‘d‘. Second letters will coincide when we will replace ‘b‘ with ‘a‘. Third letters will coincide when we will at first replace ‘b‘ with ‘a‘ and then ‘a‘ with ‘d‘.




  1. As the answer explains, the problem could be reduced to simple DFS. The requirement of this question is basically this. We first make an empty graph containing all English alphabets. Buying a spell of (c1,c2) is equivalent to  connecting c1 and c2 in the graph. Now consider two words s1 and s2 where s1[i] is the i th  character  in s1. If for every i, s1[i] and s2[i] are in a connected component in our graph, the objective is achieved. By adding all edges that connects all pairs of (s1[i],s2[i]), we are guaranteed to achieve such objective however, with unnecessary edges. To discard these edges, we could do a dfs on each node in the graph, this way, we are guaranteed to have traversed all nodes in the graph and put every node that need to be connected in the same connected component.
  2. Use Adjacency List to improve the run time of this solution. Using Adjacency Matrix would require O(N^3) time……
  3. Note that the answer on codeforces is not entirely correct, sometimes using n – k where n is the length of words, and k is the number of connected components in the graph could be wrong. Consider when the word has characters that do not need to be swapped.


Rusty hands… still

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s