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

题目大意

给你两个字符串,一个是原始串字符串,一个是加密后的目标字符串。输出可以确定的从原始串到目标串的字符对应关系,如果字符对应关系不合理,输出Impossible

解题思路

用数组记录;两个字符串中字符的一一对应关系,注意一下几点

  1. s中的一个字符不能对应t中的两个字符,t中的一个字符也不能被s中的两个字符对应,即s与t中的字符关系必须是一一对应的。
  2. 一共有26个小写英文字母,如果已经有25组关系可以一一对应,那么第26组关系也可以确定,此时可以确定的字符对应关系有26个
  3. 输出字符对应关系时按字典序输出

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e6+100;
char s[N],t[N];
int m1[30],m2[30];
int v1[30],v2[30];
int main()
{
//    fin;
    scanf("%s",s);
    scanf("%s",t);
    int len=strlen(s);
    memset(m1,-1,sizeof(m1));
    memset(m2,-1,sizeof(m2));
    int f=0;
    for(int i=0;i<len;i++){
        if(m1[s[i]-'a']==-1&&m2[t[i]-'a']==-1){
            m1[s[i]-'a']=t[i]-'a';
            m2[t[i]-'a']=s[i]-'a';
        }
        else if(m1[s[i]-'a']!=t[i]-'a'||m2[t[i]-'a']!=s[i]-'a'){
            f=1;
            break;
        }
    }
    if(f==1) printf("Impossible\n");
    else{
        int cnt=0;
        for(int i=0;i<=26;i++){
            if(m1[i]!=-1){
                cnt++;
            }
        }
        if(cnt==25){
            int c1,c2;
            for(int i=0;i<=26;i++){
                if(m1[i]==-1){
                    c1=i;
                    break;
                }
            }
            for(int i=0;i<=26;i++){
                if(m2[i]==-1){
                    c2=i;
                    break;
                }
            }
            m1[c1]=c2;
        }
        for(char i=0;i<=26;i++)
            if(m1[i]!=-1)
                printf("%c->%c\n",i+'a',m1[i]+'a');
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值