fork download
  1. /*
  2.   _.-- ,.--.
  3.   .' .' /
  4.   @ '..--------._
  5.   / \._/ '.
  6.   / .-.- \
  7.   ( / \ \
  8.   \\ '. | #
  9.   \\ \ -. /
  10.   :\ | )._____.' \
  11.   " | / \ | \ )
  12.   | |./' :__ \.-'
  13.   '--'
  14.   ...-###########+.. ..
  15.   .=##############%###..
  16.   .=#############%########..
  17.   ...#########################:..
  18.   .*##-.....#########-....:###.
  19.   .-##........#######........=#*.
  20.   .*#...%--:.:#####+..%:+:..*#.
  21.   .##...+%@...=----:..+%%...=#.
  22.   .*%:......-====------.......*#..
  23.   :##:.....=+===----==-.....=#*.
  24.   ..*##::.....==++===:.....:*##:.
  25.   ..%#%*:::.....=:.....::=#####. .
  26.   .#%#%%*:::::::::::=########*...
  27.   ...##%#%#%+.............:########+.
  28.   ..#%%#%#*.................-#######:.
  29.   ....##%%#=....................%#####%=..
  30.   ..:%%%%-......................######%+. .
  31.   ..##%%%%+........................%####%%#-.
  32.   .+###%%%#.........................:%%#%%###....
  33.   ..%##%%%%:..........................*%###%#%#=. .
  34.   .-##%%%%%*............................%%%%%###...
  35.   ..=#%%%%%%:............................#%%%%%###...
  36.   ...+###%%%%%.............................=%%%%%%#.
  37.   ..=##%%%%%%#..............................%%%%%%%#....
  38.   .:%##%%%-#%%*..............................%%%.*%%%#%*...
  39.   .##%%%-..*%%*..............................%%%...%%%#:.
  40.   ..+#%%*... +%%#.............................:%%%.. .:%%##.
  41.   ..#%%.... :%%#.............................-%%*. .#%%:
  42.   ..%%-..... ..%%%:............................*%%: .....%#+.
  43.   ..-... .. ..+%%=:..........................:%%#. .......-.
  44.   ..%%%::........................:=%%-.
  45.   .:%%=:::....................:::%%+...
  46.   .:%%:::::...............:::::#%%*..
  47.   ...%%-::::::::::::::::::::::#%%=..
  48.   ....+%%=::::::::::::::::::*%%......
  49.   ...........+%%%#=:::::::-+#%%%#:............
  50.   .......:-======+#%%%%%%%%*=-=====:...........
  51.   .........:-============+-:::-+============-:...........
  52.   ...........:-==-====+=:.......:======--==-.............
  53.   ..............................................
  54.   ................................
  55.   AUTHOR: KDUCKP -- EMAIL: ducphatkieu@gmail.com
  56.   VERSION: 1.0 - NOT UPDATED ANYTHING
  57. */
  58.  
  59. #include <bits/stdc++.h>
  60.  
  61. using namespace std;
  62. using ll = long long;
  63. using ull = unsigned long long;
  64. using ld = long double;
  65. using pii = pair<int, int>;
  66. using pll = pair<ll, ll>;
  67. using vi = vector<int>;
  68. using vll = vector<ll>;
  69. using vii = vector<pii>;
  70. using vvi = vector<vi>;
  71.  
  72. #define endl "\n"
  73. #define fi first
  74. #define se second
  75. #define pb push_back
  76. #define eb emplace_back
  77. #define all(x) (x).begin(), (x).end()
  78. #define rall(x) (x).rbegin(), (x).rend()
  79. #define sz(x) ((int)(x).size())
  80. #define FOR(i, n) for (int i = 0; i < (n); ++i)
  81. #define REP(i, a, b) for (int i = (a); i <= (b); ++i)
  82. #define PER(i, a, b) for (int i = (a); i >= (b); --i)
  83. #define EACH(x, a) for (auto& x : a)
  84. #define fast ios::sync_with_stdio(false); cin.tie(nullptr);
  85. #define YESNO(x) cout << ((x) ? "YES\n" : "NO\n")
  86. #define bitcount(x) __builtin_popcountll(x)
  87. #define clz(x) __builtin_clzll(x)
  88. #define ctz(x) __builtin_ctzll(x)
  89. #define lb lower_bound
  90. #define ub upper_bound
  91. #define __TOISETHIVOI__ int32_t main()
  92. #define vec2d(type, name, r, c, val) vector<vector<type>> name((r), vector<type>((c), (val)))
  93. #define vec3d(type, name, x, y, z, val) vector<vector<vector<type>>> name((x), vector<vector<type>>((y), vector<type>((z), (val))))
  94.  
  95.  
  96. const int MOD = 1e9 + 7;
  97. const int MOD2 = 998244353;
  98. const ll INF = 2e18;
  99. const int N = 2e5 + 5;
  100. const int LOG = 20;
  101. const double PI = acos(-1);
  102. const double EPS = 1e-9;
  103.  
  104. const int MAXN = 1e5;
  105. int par[MAXN + 5];
  106. int szz[MAXN + 5];
  107. int findset(int u)
  108. {
  109. if(u == par[u])return u;
  110. return par[u] = findset(par[u]);
  111. }
  112. void join(int u, int v)
  113. {
  114. u = findset(u);
  115. v = findset(v);
  116. if(u != v)
  117. {
  118. if(u < v)swap(u, v);
  119. szz[u] += szz[v];;
  120. par[v] = u;
  121. }
  122. }
  123. __TOISETHIVOI__ {
  124. //freopen("input.inp", "r", stdin);
  125. //freopen("output.out", "w", stdout);
  126. fast;
  127. int n;
  128. cin >> n;
  129. string s1, s2;
  130. cin >> s1 >> s2;
  131.  
  132. for (int i = 0; i < 26; ++i) {
  133. par[i] = i;
  134. szz[i] = 1;
  135. }
  136.  
  137.  
  138. for (int i = 0; i < n; ++i) {
  139. int u = s1[i] - 'a';
  140. int v = s2[i] - 'a';
  141. if (u != v) join(u, v);
  142. }
  143.  
  144.  
  145. vector<bool> visited(26, false);
  146. vector<pii> res;
  147.  
  148. for (int i = 0; i < 26; ++i) {
  149. int r = findset(i);
  150. if (!visited[r]) {
  151. visited[r] = true;
  152.  
  153. vector<int> group;
  154. for (int j = 0; j < 26; ++j) {
  155. if (findset(j) == r) {
  156. group.pb(j);
  157. }
  158. }
  159.  
  160. for (int k = 1; k < sz(group); ++k) {
  161. res.eb(group[0], group[k]);
  162. }
  163. }
  164. }
  165.  
  166. cout << res.size() << endl;
  167. for (auto& i : res) {
  168. cout << (char)(i.first + 'a') << ' ' << (char)(i.second + 'a') << endl;
  169. }
  170.  
  171. return 0;
  172. }
  173.  
Success #stdin #stdout 0.01s 5320KB
stdin
8
drpepper
cocacola
stdout
7
a c
a d
a e
a l
a o
a p
a r