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.

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.

Examples
input
helloworld
ehoolwlroz
output
3
h e
l o
d z
input
hastalavistababy
hastalavistababy
output
0
input
merrychristmas
christmasmerry
output
-1

 题意:给两个字符串,它们有如样列一样的性质,输出不一样的字母对,注意

ab

aa

不是输出

1

a b

而是-1,因为a,b不成替代关系(第一个字符a==第二个字符a)

解法:自然是依次遍历,找出不同点标记下来,注意相同的情况

复制代码
#include <bits/stdc++.h>
using namespace std;
int ans=0,k=0,vis[10000];
char x[10000],y[10000];
map<char,char>q;
string s1,s2;
map<char,char>::iterator it;
int main()
{
    cin>>s1>>s2;
    int num=0;
    for(int i=0; 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 0;
            }
        }
        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 0;
            }
        }
    }
    cout<<(q.size()-num)/2<<endl;
    for(it=q.begin(); it!=q.end(); it++)
    {
        char ch1=it->first,ch2=it->second;
        if(vis[ch1-'a']==1||vis[ch2-'a']==1||ch1==ch2) continue;
        cout<<ch1<<" "<<ch2<<endl;
        vis[ch1-'a']=1,vis[ch2-'a']=1;
    }
    return 0;
}
复制代码

 

 

posted @   樱花落舞  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
历史上的今天:
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1008
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1007
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1006
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1005
2015-12-25 2015苏州大学ACM-ICPC集训队选拔赛(1) 1001 1002 1010
点击右上角即可分享
微信分享提示