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