博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 784B Santa Claus and Keyboard Check
阅读量:4694 次
发布时间:2019-06-09

本文共 3216 字,大约阅读时间需要 10 分钟。

题面:

B. Santa Claus and Keyboard Check

Input file: standard input
Output file: standard output
Time limit: 2 second
Memory limit: 256 megabytes
 

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.
 
Example
Input

helloworld

ehoolwlroz

Output

3

h e
l o
d z

 
Input

hastalavistababy

hastalavistababy

Output

0

 
Input

merrychristmas

christmasmerry

Output

-1

 
 

题目描述:

主人公拆了键盘的键帽进行清洁。当主人公把键帽装回键盘时,发现有些键帽装错了地方。为了恢复这些装错键帽的原来位置,主人公对着键盘打了一些字符。现在给出主人公对着键盘打的字符和实际屏幕显示的字符,判断哪些键帽的位置“调转”了。
 

题目分析:

这道题关键就是看清楚题:输出能通过交换两个键的键帽来恢复键盘正常键位的组合,如果不能通过交换就输出-1。理解了这个后,我们可以看看样例:第一个样例第一个字母主人公打了h,实际显示了e。如果要符合题意的话就必须除了打入h,显示e外,还要打入e,显示h,才能符合题目的输出要求。这个我们可以用c++ map来建立这种双向的映射关系。如果后面的组合不符合这种映射关系就输出-1,如果符合就输出之前记录的映射关系。我们可以用数组保存这些映射关系(只需要保存一个字母,就可以通过map来访问另一个字母)。
 
 
AC代码:
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 using namespace std;12 13 map
mymap;14 int vec[30]; //保存所有的映射关系15 int cnt = 0; //计数16 17 int main(){18 string s, t;19 cin >> s >> t;20 int n = s.length();21 22 for(int i = 0; i < n; i++){23 if(mymap[s[i]] == 0 && mymap[t[i]] == 0) {24 mymap[s[i]] = t[i]; //建立双向映射关系25 mymap[t[i]] = s[i];26 27 if(s[i] != t[i]){ //不相等就建立映射关系(相等就不用修复了,看题)28 vec[++cnt] = s[i];29 }30 }31 else if(mymap[s[i]] != t[i] || mymap[t[i]] != s[i]){ //映射关系不对应32 cout << -1 << endl;33 return 0;34 }35 }36 37 cout << cnt << endl;38 for(int i = 1; i <= cnt; i++){39 printf("%c %c\n", vec[i], mymap[vec[i]]);40 }41 return 0;42 }

 

 

 
 
 

转载于:https://www.cnblogs.com/happy-MEdge/p/10455803.html

你可能感兴趣的文章
端口扫描base
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
FansUnion:共同写博客计划终究还是“流产”了
查看>>
python 二维字典
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>
记一些从数学和程序设计中体会到的思想
查看>>
题目1462:两船载物问题
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
第二冲刺阶段个人博客5
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
MySQL 网络访问连接
查看>>