UPC Contest 1748 Little Sub and Enigma

题目描述

Little Sub builds a naive Enigma machine of his own. It can only be used to encrypt/decrypt lower-case letters by giving each letter a unique corresponding lower-case letter. In order to ensure the accuracy, no contradiction or controversy is allowed in both the decryption and the encryption, which means all lower-case letters can only be decrypted/encrypted into a distinct lower-case letter.
Now we give you a string and its encrypted version. Please calculate all existing corresponding relationship which can be observed or deducted through the given information.

输入

The first line contains a string S, indicating the original message.
The second line contains a string T, indicating the encrypted version.
The length of S and T will be the same and not exceed 1000000.

输出

we use a string like ’x->y’ to indicate that letter x will be encrypted to letter y.
Please output all possible relationships in the given format in the alphabet order.
However, if there exists any contradiction in the given information, please just output Impossible in one line.

样例输入

复制样例数据

banana
cdfdfd

样例输出

a->d
b->c
n->f

思路:

           见代码。

AC代码

#include<iostream>
#include<cstring>
using namespace std;
int vis[200];//记录一一对应的关系
int vis1[200];//记录第一个串a中哪些字母出现过
int vis2[200];//记录第一个串b中哪些字母出现过
string a,b;
int main()
{
    cin>>a;
    cin>>b;
    int lena=a.length();
    int lenb=b.length();
    int flag=0;
    if(lena!=lenb)
        cout<<"Impossible";
    else
    {
        for(int i=0;i<lena;i++)
        {
            if(vis[a[i]]==0&&vis2[b[i]]==0)
            {
                vis[a[i]]=b[i];
                vis1[a[i]]=1;
                vis2[b[i]]=1;
            }
            else
            {
                if(b[i]!=vis[a[i]])
                {
                    flag=1;
                    break;
                }
            }
        }
    }
    int x,y,num=0;
    if(flag==1)
        cout<<"Impossible";
    else
    {
        for(int i='a';i<='z';i++)//遍历一遍寻找多少个字母已被匹配。
        {
            if(vis[i]>0)
                num++;
            if(vis1[i]==0)
                x=i;
            if(vis2[i]==0)
                y=i;
        }
        if(num==25)//如果已有25个被匹配,找到那个没有被匹配了的字母
        {
            vis[x]=y;
        }
            for(int i='a';i<='z';i++)
            {
                if(vis[i]>0)
                    cout<<(char)i<<"->"<<(char)vis[i]<<endl;
            }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ytuyzh/article/details/89817900
今日推荐