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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值