首页 > 代码库 > Codeforces 748B Santa Claus and Keyboard Check
Codeforces 748B 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.
Input
The input consists of only two strings s and t denoting the favorite Santa‘s patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.
Output
If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print ?-1? (without quotes).
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.
Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.
Example
helloworld
ehoolwlroz
3
h e
l o
d z
hastalavistababy
hastalavistababy
0
merrychristmas
christmasmerry
-1
1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <algorithm> 6 #include <set> 7 #include <vector> 8 #include <map> 9 10 using namespace std; 11 12 int main() 13 { 14 string s,t; 15 int i,j; 16 while(cin >> s >> t) 17 { 18 map<char,char> mm; 19 int cnt = 0; 20 int len = s.length(); 21 for(i=0; i<len; i++) 22 { 23 if(s[i]!=t[i]) 24 { 25 if(!mm.count(s[i])&&!mm.count(t[i])) 26 { 27 mm[s[i]] = t[i]; 28 mm[t[i]] = s[i]; 29 cnt++; 30 } 31 } 32 } 33 int flag = 0; 34 for(i=0; i<len; i++) 35 { 36 if(s[i] == t[i] && (mm.count(s[i])||mm.count(t[i]))) 37 { 38 flag = 1; 39 break; 40 } 41 if(s[i]!=t[i]) 42 { 43 if(mm[s[i]]!=t[i]) 44 { 45 flag = 1; 46 break; 47 } 48 } 49 } 50 if(flag) 51 { 52 cout << -1 << endl; 53 continue; 54 } 55 cout << cnt << endl; 56 for(i=0; i<len; i++) 57 { 58 if(s[i]!=t[i]) 59 { 60 if(mm.count(s[i])) 61 { 62 cout << s[i] << " " << t[i] << endl; 63 mm.erase(s[i]); 64 mm.erase(t[i]); 65 } 66 } 67 } 68 69 } 70 return 0; 71 }
Codeforces 748B Santa Claus and Keyboard Check