题面:

传送门

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 <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <set>
6 #include <map>
7 #include <algorithm>
8 #include <utility>
9 #include <vector>
10 #include <queue>
11 using namespace std;
12
13 map<int, int> 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 }
 
 
 

Codeforces 784B Santa Claus and Keyboard Check的更多相关文章

  1. CodeForces - 748B Santa Claus and Keyboard Check

    题意:给定两个字符串a和b,问有多少种不同的字母组合对,使得将这些字母对替换字符串b后,可以变成字符串a.注意字母对彼此各不相同. 分析:vis[u]记录与u可形成关系的字母,若u与v不同,则形成字母 ...

  2. Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check

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

  3. B. Santa Claus and Keyboard Check 模拟

    http://codeforces.com/contest/752/problem/B uuu yyu xy xx 注意变化了之后,检查一次前面已经变化过的就好.因为可能前面的满足,但是变了后不满足. ...

  4. codeforces 748E Santa Claus and Tangerines

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

  5. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...

  6. Codeforces 748D Santa Claus and a Palindrome

    雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...

  7. CodeForces 748C Santa Claus and Robot (思维)

    题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...

  8. CodeForces - 748E Santa Claus and Tangerines(二分)

    题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...

  9. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

随机推荐

  1. 1.利用consul实现k8s服务自动发现

    标题 : 1.利用consul实现k8s服务自动发现 目录 : 微服务架构设计 序号 : 1 ] } } ] } } ​ - consul自身支持ACL,但目前,Helm图表不支持其中一些功能,需要额 ...

  2. 模板 Dijkstra+链式前向星+堆优化(非原创)

    我们首先来看一下什么是前向星.   前向星是一种特殊的边集数组,我们把边集数组中的每一条边按照起点从小到大排序,如果起点相同就按照终点从小到大排序, 并记录下以某个点为起点的所有边在数组中的起始位置和 ...

  3. l2-002 链表去重 (未解决)

    L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个带整数键值的单链表L,本题要求你编写程序,删除 ...

  4. mybatis(八)手写简易版mybatis

    一.画出流程图 二.设计核心类 二.V1.0 的实现 创建一个全新的 maven 工程,命名为 mebatis,引入 mysql 的依赖. <dependency> <groupId ...

  5. npm & cli & cp: no such file or directory

    npm & cli & cp: no such file or directory empty files bug https://npm.runkit.com/hui-cli htt ...

  6. Flutter: Dismissible 通过在指示的方向上拖动来解除的Widget

    API class _MyHomeState extends State<MyHome> { @override Widget build(BuildContext context) { ...

  7. NGK DeFi Baccarat或将推动BGV成为下一个千倍币!

    目前,已经接近2020年年末,但是DeFi的热潮还在持续.近日,DeFi市场传出一道重磅利好消息,便是NGK DeFi去中心化交易系统Baccarat即将上线.届时,将会引起整个区块链市场的又一次震动 ...

  8. “NGK公链+5G”——打造智慧城市

    智慧城市目前被全球各国当成城市建设的重点,旨在城市在智能化的同时,还能给民众带来幸福感和安全感.随着5G的到来,城市智能化又到了一个新的高度.比如无人驾驶.无人机等方面将会产生质的变化,因为5G的加入 ...

  9. 使用docker mediawiki,搭建网页wiki

    我只是想做一个大家都能访问的wiki,用于成员间共享和维护一些文档.找到了docker的mediawiki,这里记录一下我怎么搭的吧. 首先,如果你在一个局域网里,有公用的可以访问的服务器,那可以直接 ...

  10. 【HTB系列】靶机Access的渗透测试详解

    出品|MS08067实验室(www.ms08067.com) 本文作者:大方子(Ms08067实验室核心成员) Hack The Box是一个CTF挑战靶机平台,在线渗透测试平台.它能帮助你提升渗透测 ...