题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3?tpId=37&tqId=21259&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
import sys ''' 先生成26个字母,将key中出现的字母删除构成字母表 ''' key = input() clear_test = input() def StructKey(key,clear_test): ''' 以key为基础构造 ''' L = [] L4 = [] for i in range(ord("a"),ord("z")+1): L.append(chr(i)) #生成一份从a到z的字母表,并不被L改变 L3 =L.copy() #去除L中包含key中的字母 for i in key: if i in L: L.remove(i) #对key进行去重 #key=list(set(key)) #key="".join(key) for i in key: if i not in L4: L4.append(i) key= "".join(L4) #生成字母表 alphapet = key+"".join(L) alphapet = list(alphapet) c = zip(L3,alphapet) #遍历并存入字典中 dict1={} for i in c: dict1[i[0]]=i[1] #print(len(dict1)) #明文中可能带有空格 clear_test_list = clear_test.split() res="" for x in clear_test_list: res+=" " for i in x: res+=dict1[i] #输出的时候去掉空格 print(res.strip()) ''' 调用StructKey ''' StructKey(key,clear_test)