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

题意:原文与加密文一一对应,输出对应关系

注意:如果有25组已经确定了,那么剩下那组也就确定了,QAQ

 

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=1e6+5;
char s[N],t[N];
vector<int> v[30];
int num[30];
int cnt=0;
int main(){
	scanf("%s%s",s,t);
	int len=strlen(s);
	bool flag=true;
	for(int i=0;i<len;i++){
		int a=s[i]-'a';
		int b=t[i]-'a';
		if(v[a].size()==0){
			v[a].push_back(b);
			num[b]++;
			cnt++;
		}
		if(v[a].size()!=0&&b!=v[a][0]){
			flag=false;
			break;
		}
	}
	if(flag){
		for(int i=0;i<26;i++){
			if(num[i]>1){
				flag=false;
				break;
			}
		}
	}
	if(flag){
		if(cnt==25){
			bool f=false;
			for(int i=0;i<26;i++){
				if(v[i].size()==0){
					for(int j=0;j<26;j++){
						if(num[j]==0){
							v[i].push_back(j);
							num[j]++;
							f=true;
							break;
						}
					}
				}
				if(f) break;
			}
		}
		for(int i=0;i<26;i++){
			if(v[i].size()>0){
				printf("%c->%c\n",'a'+i,'a'+v[i][0]);
			}
		}
	}
	else{
		printf("Impossible\n");
	}
	return 0;
}

 

全部评论

相关推荐

比亚迪汽车新技术研究院 硬件工程师 总包21左右 硕士
点赞 评论 收藏
分享
11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务