• Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017


    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;
    }
  • 相关阅读:
    Linux 性能监测:CPU
    Linux 性能监测:介绍
    数据库设计
    数据库设计
    如何将Virtualbox和VMware虚拟机相互转换
    如何将Virtualbox和VMware虚拟机相互转换
    stat()获得文件信息
    stat()获得文件信息
    网游创业为什么会失败?
    网游创业为什么会失败?
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6220883.html
Copyright © 2020-2023  润新知