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 ...
随机推荐
- GC
垃圾回收机制的优点:释放无用的对象所占用的空间.方式:自动回收.手动回收.使用System.gc实际上是调用Runtime.getRuntime().gc()
- django--主要部分
django URL URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个U ...
- 关于apache做301的问题
http://www.internetmarketingninjas.com/blog/search-engine-optimization/301-redirects/ RedirectMatch ...
- 使用css使textbox输入内容自动变大写
<style type="text/css"> input[type="text"] { text-transform:uppercase; } & ...
- 如何理解C#委托
一:从下面的例子开始,理解委托变量本质 如上图,Condition是我定义的委托变量.这个委托变量的本质就是地址变量(即C语言当中的指针变量),它保存的是方法的入口地址. 当函数的调用者传递实参给这个 ...
- 导入excel2007文件问题
基于oledb方式导入excel2007文件时,使用如下链接字符串: string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Sour ...
- EXC_ARM_DA_ALIGN
ios 版本上的问题 armv7 ipad2 int64 t = *(int64*)pBuff; 如果pBuff不是8字节对齐的地址就 crash 变通的方法是通过memcpy __sync_fe ...
- oracle指令
删除用户和用户下所有的表: drop user user_name cascade; 导入数据库: cd /home/oracle/app/admin/orcl/dpdump impdp dire ...
- js数字位数太大导致参数精度丢失问题
最近遇到个比较奇怪的问题,js函数里传参,传一个位数比较大,打印arguments可以看到传过来的参数已经改变. 然后查了一下,发现确实是js精度丢失造成的.我的解决方法是将数字型改成字符型传输,这样 ...
- 灵活运用SQL Server SSIS变量
在SSIS开发ETL(Extract-Transform-Load),数据抽取.转换.装载的过程.我们需要自己定义变量 一.SSIS变量简介 SSIS(SQL Server Integration S ...