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 ...
随机推荐
- [转]各种有用的PHP开源库精心收集
FROM : http://my.oschina.net/caroltc/blog/324024 1.html2ps and html2pdf 下载地址: http://www.tufat.co ...
- PHP 基础笔记
数据类型 字符串 整数 浮点数 布尔值 数组 对象 NULL 未定义的变量,数据类型为 NULL. PHP 中数组和对象是不同的类型,而 js 中数组即为对象.(ps: es6 已经内置了 class ...
- Node基础:url查询参数解析之querystring
模块概述 在nodejs中,提供了querystring这个模块,用来做url查询参数的解析,使用非常简单. 模块总共有四个方法,绝大部分时,我们只会用到 .parse(). .stringify() ...
- 各地IT薪资待遇讨论
作为一个搞.net开发的程序员,在北京混了三年半,最近准备辞职到上海找工作.由于对上海的IT行业还不是很了解,在这里想让上海的同行们说下你们的情况,以方便我对自己在上海的定位,当然,其余城市的的同行们 ...
- spring配置属性的两种方式
spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...
- java中的全等和相似
package pack2; import java.util.*; /*Node 的equals()和hashCode()两个函数缺一不可 * HashSet会通过这两个函数来判断两个元素是否等价 ...
- 屠龙之路_转角遇到服务器大魔王_FourthDay
Day4:将View打败后,View还送了屠龙团一个信物.于是在今天的旅途中,少年们在遇到View的其他兄弟时,以此信物将他们收于麾下,并借助他们的力量打败了服务器大魔王." Fightin ...
- SharedPreference写入-读取
SharedPreference文件保存格式为xml文件. 一.SharedPreference写入 SharedPreferences sharedPreferences = context.get ...
- extJS 创建类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- HTML5基础知识(3)--required属性
1.required属性规定在提交之前要填写输入域(不能为空). 2.代码 <body> <form> 账号:<input type="text" r ...