Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B
Description
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
题意:给两个字符串,它们有如样列一样的性质,输出不一样的字母对,注意
ab
aa
不是输出
1
a b
而是-1,因为a,b不成替代关系(第一个字符a==第二个字符a)
解法:自然是依次遍历,找出不同点标记下来,注意相同的情况
#include <bits/stdc++.h>
using namespace std;
int ans=,k=,vis[];
char x[],y[];
map<char,char>q;
string s1,s2;
map<char,char>::iterator it;
int main()
{
cin>>s1>>s2;
int num=;
for(int i=; i<s1.length(); i++)
{
if(s1[i]!=s2[i])
{
if(q.find(s1[i])==q.end()&&q.find(s2[i])==q.end())
{
q[s1[i]]=s2[i];
q[s2[i]]=s1[i];
}
else if(q[s1[i]]!=s2[i]||q[s2[i]]!=s1[i])
{
cout<<"-1"<<endl;
return ;
}
}
else
{
if(q.find(s1[i])==q.end())
{
q[s1[i]]=s1[i];
num++;
}
else if(q[s1[i]]!=s1[i])
{
cout<<"-1"<<endl;
return ;
}
}
}
cout<<(q.size()-num)/<<endl;
for(it=q.begin(); it!=q.end(); it++)
{
char ch1=it->first,ch2=it->second;
if(vis[ch1-'a']==||vis[ch2-'a']==||ch1==ch2) continue;
cout<<ch1<<" "<<ch2<<endl;
vis[ch1-'a']=,vis[ch2-'a']=;
}
return ;
}
Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) B的更多相关文章
- 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 ...
- Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) A
Description Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the f ...
- 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) 圣诞之夜!
A. Santa Claus and a Place in a Class 模拟题.(0:12) 题意:n列桌子,每列m张桌子,每张桌子一分为2,具体格式看题面描述.给出n,m及k.求编号为k的桌子在 ...
- Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)
http://codeforces.com/contest/737 A: 题目大意: 有n辆车,每辆车有一个价钱ci和油箱容量vi.在x轴上,起点为0,终点为s,中途有k个加油站,坐标分别是pi,到每 ...
- codeforces Codeforces Round #380 (Div. 1, Rated, Based on Technocup 2017 - Elimination Round 2)// 二分的题目硬生生想出来ON的算法
A. Road to Cinema 很明显满足二分性质的题目. 题意:某人在起点处,到终点的距离为s. 汽车租赁公司提供n中车型,每种车型有属性ci(租车费用),vi(油箱容量). 车子有两种前进方式 ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) E. Subordinates 贪心
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #380 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 2) D. Sea Battle 模拟
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- 使用sql server 链接服务器
在我们的日常应用场景中经常会碰访问不同服务器上的数据库,即跨服务器访问操作不同的服务器上的SQL Sever数据库, 这个时候Sql Server的链接服务器就非常实用,创建SQL语句如下: --重新 ...
- LeetCode Remove Element
原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...
- 第五篇:白话tornado源码之褪去模板的外衣
上一篇<白话tornado源码之请求来了>介绍了客户端请求在tornado框架中的生命周期,其本质就是利用epoll和socket来获取并处理请求.在上一篇的内容中,我们只是给客户端返回了 ...
- devexpress treelist 过滤
FilterMode.Smart 问题:dev 的treelist加过滤条件后,如果根节点不符合条件,则不显示数据 处理方法:把filterMode设置为smart即可. 备忘.
- 在centos6.7用yum安装redis解决办法
1. centos默认的安装源在官方centos.org上,而Redis在第三方的yum源里,所以无法安装,非官方的yum推荐用fedora的epel仓库 [root@localhost instal ...
- Emmet使用之HTML
前言 前段时间在网上发现一个强大的好玩的东西,emmet,它可以方便我们前端开发者快速编写html和css.可以算是前端开发必备的一款利器,今天先总结一下用emmet写html,有时间再总结下css的 ...
- nodejs require//////////z
背景 这篇文基本都是反对的,反对的很有道理,不是说我这篇文章的内容错误,因为这篇文章是我在健身房学习node的时候写的,这些知识都很粗糙,后来发现官方的稳定更详细:地址:http://nodejs.o ...
- iptables 设置肯限制流量
1.查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L -n Chain INPUT (policy ACCEPT) target prot opt source ...
- swift基础:第五部分:函数与闭包(补充)
由于时间关系,我就不打算再聊天了,直接进入正题吧. 在OC中,匿名函数就是block,也称为代码块,那么在swift中,匿名函数我们称之为“闭包”.函数实际上是一种特殊的闭包,你可以使用{}来创建一个 ...
- 夺命雷公狗----Git---1---安装步骤
除了上面的路径修改一下,别的都用默认的问题即可解决.....