Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check
2 seconds
256 megabytes
standard input
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.
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.
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.
helloworld
ehoolwlroz
3
h e
l o
d z
hastalavistababy
hastalavistababy
0
merrychristmas
christmasmerry
-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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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的桌子在 ...
- Codeforces 784B Santa Claus and Keyboard Check
题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- AlwaysOn--Backup Preference
AlwaysOn group的一个新特性是允许在secondary replica进行backup,将backup的负载从primary replica上移除去. 并且提供了Backup prefer ...
- NET WebApi OWIN 实现 OAuth 2.0
NET WebApi OWIN 实现 OAuth 2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和 ...
- img加载在IE11,chrome,FF下的不同
IE11 img.complete 得不到img的大小,会使用img.onload chrome,ff:img.complete 得不到img的大小,会使用自己创建的img加载方法
- noi题库(noi.openjudge.cn) 1.9编程基础之顺序查找T01——T05
T01 查找特定元素的值 描述 在一个序列(下标从1开始)中查找一个给定的值,输出第一次出现的位置. 输入 第一行包含一个正整数n,表示序列中元素个数.1 <= n <= 10000.第二 ...
- SQL Server 百万级数据提高查询速度的方法
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...
- Google 面试
坚持完成这套学习手册,你就可以去 Google 面试了 系统 指针 value Google 面试 阅读10266 本文为掘金投稿,译文出自:掘金翻译计划 原文地址:Google Intervie ...
- C程序中对时间的处理——time库函数详解
包含文件:<sys/time.h> <time.h> 一.在C语言中有time_t, tm, timeval等几种类型的时间 1.time_t time_t实际上是长整数类型, ...
- <html>中的action
现在发现html中的许多标签都具有重要属性,而且这些属性是需要用于数据的传输的,虽然说html着重在于数据的显示,但是 在浏览器与服务器之间的交互,需要有数据参与传输,而数据传输的标准,就依赖于htm ...
- 品牌OEM信息导入工具(实测支持Win10)
OEM修改,定制专属LOGO. 免费下载:http://yunpan.cn/cmZuTYWLIGX6Q 访问密码 2da7 备用通道: http://pan.baidu.com ...
- win7 IIS 部署-vs2012开发网站-全是问题啊。。。
1.文件夹权限everyone2.aspnet_regiis.exe -i 表现为: