题面:
B. Santa Claus and Keyboard Check
Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be.
In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.
You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.
Otherwise, the first line of output should contain the only integer k (k ≥ 0) — the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct.
If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair.
helloworld
ehoolwlroz
3
h e
l o
d z
hastalavistababy
hastalavistababy
0
merrychristmas
christmasmerry
-1
题目描述:
题目分析:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <set> 6 #include <map> 7 #include <algorithm> 8 #include <utility> 9 #include <vector> 10 #include <queue> 11 using namespace std; 12 13 map<int, int> mymap; 14 int vec[30]; //保存所有的映射关系 15 int cnt = 0; //计数 16 17 int main(){ 18 string s, t; 19 cin >> s >> t; 20 int n = s.length(); 21 22 for(int i = 0; i < n; i++){ 23 if(mymap[s[i]] == 0 && mymap[t[i]] == 0) { 24 mymap[s[i]] = t[i]; //建立双向映射关系 25 mymap[t[i]] = s[i]; 26 27 if(s[i] != t[i]){ //不相等就建立映射关系(相等就不用修复了,看题) 28 vec[++cnt] = s[i]; 29 } 30 } 31 else if(mymap[s[i]] != t[i] || mymap[t[i]] != s[i]){ //映射关系不对应 32 cout << -1 << endl; 33 return 0; 34 } 35 } 36 37 cout << cnt << endl; 38 for(int i = 1; i <= cnt; i++){ 39 printf("%c %c ", vec[i], mymap[vec[i]]); 40 } 41 return 0; 42 }