最近在准备机试,对练习的机试题做个总结。之前没有学过C++,只学过C语言,但是实际用起来的时候发现C++是更适合机试的语言,因为它的库函数更多,能支持更多操作,将一些代码简化。

习惯了C语言定义字符串char s[1000]; 在C++中一个string s就OK了等等。

题目

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


读题读了10分钟才完全懂,真是太慢了。。。

它的大意就是给你两个字符串,可能键帽按错了,导致某两个字母都颠倒了,让你给他恢复成原来的样子,如果能恢复,就输出错误个数,并输出所有错误的键对,如果恢复不了,就输出-1,如果一开始就是完全正确的,就输出0(这其实是能恢复的一种特殊情况,其实可以和第一个合并)

代码

下面是我自己的代码,会在第14个样例出错,我太菜了,暂时还没有找到问题(加黑标注一下,如果网友发现我的代码哪里有问题,可以直接评论告诉我)

点击查看代码
#include<bits/stdc++.h>
using namespace std; int main(){
char s[1000],t[1000],o[2000],temp1,temp2;
int flag[1000]={0};
int sum=0;
gets(s);
gets(t);
int slen = strlen(s);
int i=0,j=0,k=0;
if(strcmp(s,t)==0){
cout<<0<<endl;
return 0;
}
else{
while(i<slen){
if(s[i]==t[i]){
++i;
}
else{
temp1=s[i];
temp2=t[i];
for(j=i+1;j<slen;j++){
if(t[j]==temp2&&s[j]!=temp1||s[j]==temp1&&t[j]!=temp2||t[j]==temp1&&s[j]!=temp2||s[j]==temp2&&t[j]!=temp1){
cout<<-1<<endl;
return 0;
}
}
o[k++]=temp1;
o[k++]=temp2;
sum+=1;
for(j=i;j<slen;j++){
if(t[j]==temp2) flag[j]=1;
if(t[j]==temp1){
t[j]=temp2;
}
}
for(j=i;j<slen;j++){
if(flag[j]==1){
t[j]=temp1;
flag[j]=0;
}
}
}
}
if(strcmp(s,t)==0){
cout<<sum<<endl;
for(i=0;i<k;i=i+2){
cout<<o[i]<<" "<<o[i+1]<<endl;
}
return 0;
}
}
}

下面一个大佬的AC代码(来自:https://www.cnblogs.com/happy-MEdge/p/10455803.html ),看了之后非常敬佩,是我不会用map,其实我一直在想,怎么给他做映射,但是不会写,我可能更应该系统学一下C++,

思路

可以用c++ map来建立这种双向的映射关系。如果后面的组合不符合这种映射关系就输出-1,如果符合就输出之前记录的映射关系。我们可以用数组保存这些映射关系(只需要保存一个字母,就可以通过map来访问另一个字母)。

代码

点击查看代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
map<int, int> mymap;
int vec[30]; //保存所有的映射关系
int cnt = 0; //计数
int main(){
string s, t;
cin >> s >> t;
int n = s.length();
for(int i = 0; i < n; i++){
if(mymap[s[i]] == 0 && mymap[t[i]] == 0) {
mymap[s[i]] = t[i]; //建立双向映射关系
mymap[t[i]] = s[i]; if(s[i] != t[i]){ //不相等就建立映射关系(相等就不用修复了,看题)
vec[++cnt] = s[i];
}
}
else if(mymap[s[i]] != t[i] || mymap[t[i]] != s[i]){ //映射关系不对应
cout << -1 << endl;
return 0;
}
}
cout << cnt << endl;
for(int i = 1; i <= cnt; i++){
printf("%c %c\n", vec[i], mymap[vec[i]]);
}
return 0;
}

机试练习(一)——Codeforces 784B Santa Claus and Keyboard Check的更多相关文章

  1. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  2. CodeForces - 748B Santa Claus and Keyboard Check

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

  3. 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 ...

  4. B. Santa Claus and Keyboard Check 模拟

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

  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 748E Santa Claus and Tangerines

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

  7. Codeforces 748D Santa Claus and a Palindrome

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

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

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

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

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

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

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

随机推荐

  1. 【python爬虫】爬取美女图片

    一,导入包文件 os:用于文件操作.这里是为了创建保存图片的目录 re:正则表达式模块.代码中包含了数据处理,因此需要导入该模块 request:请求模块.通过该模块向对方服务器发送请求获取数据包 l ...

  2. 【ACM算法竞赛日常训练】DAY16【奇♂妙拆分】【区区区间间间】【小AA的数列】数学 | 位运算 | 前缀和

    DAY16共3题: 奇♂妙拆分(简单数学) 区区区间间间(单调栈) 小AA的数列(位运算dp) 作者:Eriktse 简介:19岁,211计算机在读,现役ACM银牌选手力争以通俗易懂的方式讲解算法!️ ...

  3. ROS机器人摄像头寻线

    ROS机器人摄像头寻线 连接小车 注意:必须在同一区域网 ssh clbrobort@clbrobort 激活树莓派主板 roslaunch clbrobot bringup.launch 开启摄像头 ...

  4. 关于PM系统以及OA系统的工作基本心态

    这个系统的目的是什么? 这个系统的初衷是好的,是一个信息化管理的数据科学系统,目的是更好的累计公司的业务数据. 但实际操作过程中,包括推广过程中,你能看到上层人员对于这个系统的态度,更像是一个个人企业 ...

  5. websocket多实例推送解决方案-数据实时展示

    需求 需要前端展示实时的订单数据信息.如下图所示,实时下单实时页面统计更新展示 思路方案 前端使用websocket 建立通信 后端监听数据库的binglog变更,实时得到最新数据,推送到前端 现状及 ...

  6. 2023-02-20:小A认为如果在数组中有一个数出现了至少k次, 且这个数是该数组的众数,即出现次数最多的数之一, 那么这个数组被该数所支配, 显然当k比较大的时候,有些数组不被任何数所支配。 现在

    2023-02-20:小A认为如果在数组中有一个数出现了至少k次, 且这个数是该数组的众数,即出现次数最多的数之一, 那么这个数组被该数所支配, 显然当k比较大的时候,有些数组不被任何数所支配. 现在 ...

  7. 2020-08-19:TCP是通过什么机制保障可靠性的?

    福哥答案2020-08-19: 福哥口诀法:校(jiao)序确重拥流连(tcp可靠性保障机制:校验.序号.确认.重传.拥塞.流量.连接)校验:数据是否正确.序号:对数据编号seq.确认:ACK.重传: ...

  8. 2022-10-07:给定员工的 schedule 列表,表示每个员工的工作时间。 每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序。 返回表示 所有 员工的 共同,正

    2022-10-07:给定员工的 schedule 列表,表示每个员工的工作时间. 每个员工都有一个非重叠的时间段 Intervals 列表,这些时间段已经排好序. 返回表示 所有 员工的 共同,正数 ...

  9. 2022-02-16:将数组分割成和相等的子数组。 给定一个有 n 个整数的数组,你需要找到满足以下条件的三元组 (i, j, k) : 0 < i, i + 1 < j, j + 1 < k < n

    2022-02-16:将数组分割成和相等的子数组. 给定一个有 n 个整数的数组,你需要找到满足以下条件的三元组 (i, j, k) : 0 < i, i + 1 < j, j + 1 & ...

  10. 2021-10-07:将有序数组转换为二叉搜索树。给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。高度平衡 二叉树是一棵满足「每个节点的左右两个子树

    2021-10-07:将有序数组转换为二叉搜索树.给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树.高度平衡 二叉树是一棵满足「每个节点的左右两个子树 ...