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
helloworld
ehoolwlroz
output
3
h e
l o
d z
input
hastalavistababy
hastalavistababy
output
0
input
merrychristmas
christmasmerry
output
-1

开个link数组,判断字符与字符间的对应关系。

如果所有不同的字符对都满足双向对应,那么有解。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
char link[mxn];
char s[mxn];
char c[mxn];
int k=;
char ans1[mxn],ans2[mxn];
bool vis[mxn];
int main(){
int i,j;
scanf("%s%s",s,c);
int len=strlen(s);
if(len!=strlen(c)){printf("-1\n");return ;}
for(i=;i<len;i++){
if(c[i]!=link[s[i]]){
if(!link[s[i]]){
link[s[i]]=c[i];
}
else{printf("-1\n");return ;}
}
if(s[i]!=link[c[i]]){
if(!link[c[i]]){
link[c[i]]=s[i];
}
else {printf("-1\n");return ;}
}
}
for(i=;i<;i++){
int tmp='a'+i;
if(!link[tmp])continue;
if(link[tmp]!=tmp && !vis[tmp]){
ans1[++k]=tmp;
ans2[k]=link[tmp];
vis[tmp]=vis[link[tmp]]=;
}
}
printf("%d\n",k);
for(i=;i<=k;i++){
printf("%c %c\n",ans1[i],ans2[i]);
}
return ;
}

Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check的更多相关文章

  1. Codeforces Round #389 Div.2 D. Santa Claus and a Palindrome

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. Codeforces Round #389 Div.2 E. Santa Claus and Tangerines

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. Codeforces Round #389 Div.2 C. Santa Claus and Robot

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Codeforces Round #389 Div.2 A. Santa Claus and a Place in a Class

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) 圣诞之夜!

    A. Santa Claus and a Place in a Class 模拟题.(0:12) 题意:n列桌子,每列m张桌子,每张桌子一分为2,具体格式看题面描述.给出n,m及k.求编号为k的桌子在 ...

  6. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  7. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  8. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) E. Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  9. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) C

    Description Santa Claus has Robot which lives on the infinite grid and can move along its lines. He ...

随机推荐

  1. NET Core项目定义Item Template

    NET Core项目定义Item Template 作为这个星球上最强大的IDE,Visual Studio不仅仅提供了很多原生的特性,更重要的是它是一个可定制的IDE,比如自定义Project Te ...

  2. LINQ 查询表达式(C# 编程指南)

    语言集成查询 (LINQ) 是一组技术的名称,这些技术建立在将查询功能直接集成到 C# 语言(以及 Visual Basic 和可能的任何其他 .NET 语言)的基础上.  借助于 LINQ,查询现在 ...

  3. climits

    <climits>头文件定义的符号常量 CHAR_MIN  char的最小值SCHAR_MAX  signed char 最大值SCHAR_MIN   signed char 最小值UCH ...

  4. CUDA1-hello world

    电脑配置:windows7 sp1 64bit  + CUDA6.5 + GeForce GTX780 Ti 显卡中的GPU因为多核可以处理很多相同的操作,相比较来说cpu就像个健全的手,什么活都能干 ...

  5. DOM 概况

    DOM(文档对象模型)是针对 HTML 和 XML 文档的一个API(应用程序编程接口).DOM 描绘了一个层次化的节点树,允许开发人员添加.移除和修改页面的某一部分. 层次节点 DOM可以将任何 H ...

  6. 为什么我们的web前端变的越来越复杂

    前端发展了也有些年头了,曾记得很多年前,聊起前端,都觉得是很简单,那个时候都没有前端工程师这个职位.可现在,前端已经逆袭了,已经不是原来的样子了,各种技术层出不穷,显的越来越高深莫测了.前端真的变得那 ...

  7. Sql 2012 远程数据库连接

    新装好的数据库,各种远程连接设置都设置好后.发现还是连接不上. 这时候,问题可能出在端口上,远程连接的时候需要指明端口.动态端口一般都会变.在本机连接时候,会自动选择端口.在远程访问时候,就必须要指明 ...

  8. HBase初探

    string hbaseCluster = "https://charju.azurehdinsight.net"; string hadoopUsername = "账 ...

  9. [BZOJ1116][Poi2008]LCO(并查集)

    题目:http://hzwer.com/3010.html 分析:注意这里无向边是对入度没有贡献的. 那么对于一个n个点的连通块而言,如果它是一颗树(n-1条边),那么把所有边全部从某个根开始向下指, ...

  10. Learning to Rank 之 listwise ranking

    详细文章: http://www.machinelearning.org/proceedings/icml2007/papers/139.pdf