Little Sub and Enigma

问题 I: Little Sub and Enigma

时间限制: 1 Sec  内存限制: 256 MB
提交: 934  解决: 112
[提交] [状态] [命题人:admin]

题目描述

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

题目小结:

1.distinct lower-case letter:意为可以与“明文”相同,但作为“密文”,不能出现与之相同的对应关系。

2.deducted :推断,如果有25个对应关系,则可以推出第26个对应关系。

3.比赛时认为有大写与空格存在,但是若全是大写与空格输出什么呢?我想了半天难道输出空?所以题目没有明确要求的就没有这种情况。代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
#include<set>
using namespace std;
map <char ,char> ma;
set <char> st;
int main(){
    string s,t;
    cin>>s>>t;
    bool flag=true;
    for(int i=0;i<26;i++){
        ma[i+'a']='!';
    }
    for(int i=0;i<s.size();i++){
        if((ma[s[i]]=='!'&&st.count(t[i])==0)){
            st.insert(t[i]);
            ma[s[i]]=t[i];
        }
        else if(ma[s[i]]==t[i]) {
        }
        else{
            flag=false;
        }
    }
    if(flag){
        char pos,tmp;
        if(st.size()==25){
           for(map<char,char>::iterator iter=ma.begin();iter!=ma.end();iter++){
                if(st.count(iter->first)==0){
                        tmp=iter->first;
                }
                if(iter->second=='!'){
                    pos=iter->first;
                }
            }
            ma[pos]=tmp;
        }
        for(map<char,char>::iterator iter=ma.begin();iter!=ma.end();iter++){
            if(iter->second!='!')
            cout<<iter->first<<"->"<<iter->second<<endl;
        }
    }
    else{
        printf("Impossible\n");
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值