939D.md (2744B)
1 Source: https://codeforces.com/problemset/problem/939/D 2 3 D. Love Rescue 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length *n* consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (*c*1, *c*2) (where *c*1 and *c*2 are some lowercase English letters), which can arbitrary number of times transform a single letter *c*1 to *c*2 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. 26 27 Input 28 29 The first line contains a single integer *n* (1 ≤ *n* ≤ 105) — the length of the letterings. 30 31 The second line contains a string with length *n*, consisting of lowercase English letters — the lettering on Valya's pullover. 32 33 The third line contains the lettering on Tolya's t-shirt in the same format. 34 35 Output 36 37 In the first line output a single integer — the minimum amount of mana *t* required for rescuing love of Valya and Tolya. 38 39 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. 40 41 If there are many optimal answers, output any. 42 43 Examples 44 45 Input 46 47 ``` 48 3 49 abb 50 dad 51 ``` 52 53 Output 54 55 ``` 56 2 57 a d 58 b a 59 ``` 60 61 Input 62 63 ``` 64 8 65 drpepper 66 cocacola 67 ``` 68 69 Output 70 71 ``` 72 7 73 l e 74 e d 75 d c 76 c p 77 p o 78 o r 79 r a 80 ``` 81 82 Note 83 84 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'.