2019 Hangzhou Normal University Cup title Little Sub and Enigma I-

问题 I: Little Sub and Enigma
时间限制: 1 Sec 内存限制: 256 MB
提交: 807 解决: 73
[提交] [状态] [命题人: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.

Sample input
copy data sample
Banana
cdfdfd
sample output from
A-> D
B-> C
N-> F

Ideas; for a lowercase x and y have x-> y is encrypted, the encryption to meet one mapping, asked if he could meet, you can press the mapping between corresponding lexicographic output, the output can not just impossible, to note here is that the output there is a pit, the output requirement is all the mappings can be inferred based on corresponding known conditions, when there are 25 letters after the completion of correspondence, then the first 26 can be inferred, so to output 26, know it very well write the

#include<bits/stdc++.h>
using namespace std;
typedef struct{
	int a;
	int b;
}xxx;
xxx x[26];
int vis[26];      **//vis记录s1的小写字母**
int vis2[26];    **//vis2记录s2的小写字母**
char s1[1000005];
char s2[1000005];
int main()
{
	cin>>s1>>s2;
	int len = strlen(s1);
	int flag = 1,flag2=1,flag3=0;
	for (int i = 0; i < len; i++)
	{
		if(s1[i]>='a'&&s2[i]<='z')
		{
			int t = s1[i] - 'a';
			int t2 = s2[i] - 'a';
			if (vis[t] == 0){
				x[t].a = t;
				x[t].b = t2;
				vis[t] = 1;
				vis2[t2]++;flag3++;
			}
			if (x[t].b != t2){
				flag = 0;
			}
			if (vis2[t2] > 1){
				flag2 = 0;
			}
		}
	}
	if (flag&&flag2)
	{
		for (int i = 0; i < 26; i++)
		{
			if (vis[i] == 1)
			{
				printf("%c->%c\n", x[i].a + 'a', x[i].b + 'a');
			}
			if(flag3==25){
				if(vis[i]==0)
				{
					int j;
					for(int i=0;i<26;i++){
						if(vis2[i]==0){
							j=i;break;
						}
					}
					printf("%c->%c\n", i + 'a', j + 'a');
				}
			}
		}
	}
	else{
		cout << "Impossible\n";
	}
	return 0;
}
Published 10 original articles · won praise 6 · views 1797

Guess you like

Origin blog.csdn.net/Kris_Kris/article/details/89813892