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
这个题目是必须要满足双射的条件,怎么说么,题目是译码和反译码必须同时满足,这个就必须满足一一对应关系,首先按照一个密码不能同时被两个译码所对应,一个译码也不能被两个密码所对应,最后当满足25个一一对应的关系后,最后一组也就满足了,所以用两个数组来表示来存两者的对应关系后,当不满足一一对应关系之后输出Impossible,否则输出答案

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e6+50;
char s1[N],s2[N];
int a[200],b[200];

int main(){
    int cnt=0;
    scanf("%s%s",s1+1,s2+1);
    int len=strlen(s1+1);
    for(int i=1;i<=len;i++){
        if(!a[s1[i]])++cnt,a[s1[i]]=s2[i];
        else if(a[s1[i]]&&a[s1[i]]!=s2[i]){
        printf("Impossible\n");
        return 0;
       }
    }
    for(int i=1;i<=len;i++){
        if(!b[s2[i]])b[s2[i]]=s1[i];
        else if(b[s2[i]]&&b[s2[i]]!=s1[i]){
            printf("Impossible\n");
            return 0;
        }
    }
    if(cnt==25){
        for(int i='a';i<='z';i++){
            for(int j='a';j<='z';j++){
                if(!a[i]&&!b[j]){
                    a[i]=j;
                }
            }
        }
    }
    for(int i='a';i<='z';i++){
        if(a[i])printf("%c->%c\n",char(i),char(a[i]));
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值