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. netcore 控制台中文乱码

    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Console.OutputEncoding = Encoding.Get ...

  2. Java 密码扩展无限制权限策略文件

    因为某些国家的进口管制限制,Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件.   官方网站提供了JCE无限制权限策略文件的下载:   ...

  3. Bootstrap系列 -- 10. 网格布局

    一. 实现原理 网格布局是通过容器的大小,平均分为12份(可以修改),再调整内外边距,和表格布局有点类似但是也存在区别. 实现步骤如下: (1) 数据行.row 必须包含在容器.container 中 ...

  4. ArcEngine将线符号化为立方体状

    对于二三维同步中的三维视图肯定是需要通过二维元素来符号化成三维元素的,之前项目测试临时采用这个自代的圆管状: esriSimple3DLineStyle AxisStyle = esriSimple3 ...

  5. Android Intent的几种用法全面总结

    Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...

  6. jQuery jsonp无法捕获404、500状态错误

    转载:http://www.cnblogs.com/pao8041/p/4750403.html 不过上面的这个我用的不好,下次有机会用

  7. BGP--边界网关协议

    要全面了解BGP,首先我们要回答以下看上去很简单的问题:为什么需要BGP,也就是说BGP是如何产生的,它解决了什么问题.带着以上问题,我们先简单的回顾一个路由协议发展的轨迹. 首先路由的实质是描述一个 ...

  8. 中继器、集线器(HUB)、网桥、交换机、路由器比较

    中继器或集线器既不能隔离冲突域又不能隔离广播域,网桥或交换机只能隔离冲突域不能隔离广播域,路由器既能隔离冲突域又能隔离广播域,为什么?[解析] 首先要清楚什么是冲突域和广播域,当一块网卡发送信息时有可 ...

  9. bindService初步了解

    bindService的使用: 当需要调Service里面的方法时,可以用bindService() 首先定义一个类继承于Service,然后配置Manifest.xml文件 public class ...

  10. vi实战记录

    vi编辑器在Unix和Linux中比较早期的,Vim是vi的扩展集,是对vi的加强. 服务器最小化,默认集成vi编辑器!了解vi常用命令,工作起来颇有-洪荒之力!!! 01.关于退出 :wq!  -- ...