【题解】
为什么那时候没有开这道题呢(沉默)
加密串与解密串的字母一一对应。输出可推断出的所有情况,特判25组对应关系推出第26组的情况。
思路:加密串对应的解密串字母一定是同时出现的,vis1[]记录加密串字母出现过与否(出现过则赋值为对应字母的ASCII码),vis2[]记录解密串字母出现过与否。定义计数器判断25->26的情况。没有一一对应就输出Impossible。
【代码】
int main()
{
string a,b; cin>>a>>b;
int l=a.length();
int f=1,vis1[300]={0},vis2[300]={0};
int c=0;
for(int i=0;i<l;i++){
if(vis1[a[i]]==0&&vis2[b[i]]==0){
c++;
vis1[a[i]]=b[i];
vis2[b[i]]=1;
}
else if(vis1[a[i]]==(int)b[i]) continue;
else{
f=0; break;
}
}
if(f){
if(c==25){
for(int i=97;i<97+26;i++){
if(vis1[i]==0){
for(int j=97;j<97+26;j++){
if(vis2[j]==0){
vis1[i]=j;
break;
}
}
break;
}
}
}
for(int i=97;i<97+26;i++){
if(vis1[i])
printf("%c->%c\n",i,vis1[i]);
}
}
else puts("Impossible");
return 0;
}
【题目】
Little Sub and Enigma AC
Time Limit: 1 s Memory Limit: 256 MB
Submission:1833 AC:112 Score:100.00
Description
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.
Input
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.
Output
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.
Samples
input:
banana
cdfdfd
output:
a->d
b->c
n->f
input:
banana
cdfdfs
output:
Impossible