Codeforces 748B Santa Claus and Keyboard Check

来源:互联网 发布:grub 启动windows 编辑:程序博客网 时间:2024/06/06 08:24

B. Santa Claus and Keyboard Check
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
helloworldehoolwlroz
output
3h el od z
input
hastalavistababyhastalavistababy
output
0
input
merrychristmaschristmasmerry
output
-1

比较两个字符串,找出其可以相互替换的字母,使得两个字符串转换成相同的。

遍历,如果不相同就进行替换。

#include <iostream> #include <cstdio>#include <cstdlib>#include <cmath>#include <fstream>#include <algorithm>#include <queue>#include <climits>#include <cstring>#include <string>#include <set>#include <map>#include <queue>#include <stack>#include <vector>#include <list>const int INF = 1e9;using namespace std;typedef  long long ll;typedef  unsigned long long  ull; string s1,s2;map<char,char> mp;set<char> st;map<char,char>::iterator it;int main(){ios::sync_with_stdio(false);  cin >> s1>>s2;if(s1==s2){cout << "0"<<endl;return 0;}for(int i=0;i<s1.size();i++){if(s1[i]!=s2[i]){if(st.count(s1[i])||st.count(s2[i]))continue;mp[s1[i]] = s2[i];st.insert(s1[i]);st.insert(s2[i]);char t1  = s1[i];char t2  = s2[i];for(int j=0;j<s1.size();j++){if(s1[j]==t1){s1[j] = t2;continue;}if(s1[j]==t2){s1[j] = t1;}}}}//cout << s1<<endl<<s2<<endl;if(s1!=s2){cout <<"-1"<<endl;return 0;} int re = 0;vector<char> a;vector<char> b; for(it = mp.begin();it!=mp.end();it++){//if(mp[it->second]==it->first)//{re++;a.push_back(it->first);b.push_back(it->second);//mp.erase(mp[it->second]);//}}cout <<re<<endl;for(int i=0;i<re;i++){cout << a[i]<<" "<<b[i]<<endl;}return 0;} 




0 0