zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    原生j获取元素的几种方法
    git 解决每次更新代码都要输入用户名密码
    npm/ yarn设置淘宝镜像
    git clone 失败: fatal: unable to access 'https://github.com/liufeifie/xxx.git/': Failed to connect to github.com port 443: Timed out
    git注意事项之|解决remote: User permission denied 问题
    js-input file 文件上传(照片,视频,音频)
    设置 npm 源为淘宝镜像
    vue-cli3安装过程
    PowerShell yarn : 无法加载文件 C:UserspcAppDataRoaming pmyarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参 阅 ....
    http协议简介
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6220883.html
Copyright © 2011-2022 走看看