第一行输入一个长度为
,仅由小写字母构成的字符串
,代表待构建的新字母表底串。
第二行输入一个长度为
,仅由小写字母构成的字符串
,代表需要加密的明文。
在一行上输出一个字符串,代表加密后的密文。
trailblazers attackatdawn
tpptadtpitvh
在这个样例中,加密的操作如下:
对
进行去重
,得到
;
随后从
开始依次在字符串末尾补充
中未出现的字母,得到
;
最后,对于
中的每个字母,替换为
构建得到的新字母表中相同位置的字母。我们可以列出对照表:
最后,对于
中的每个字母,替换为
构建得到的新字母表中相同位置的字母,得到
。
nihao ni
le
secretKey = input()#密钥 word = input()#需要加密的内容 set1 = []#本来想用集合去重,发现去重后会打乱排列顺序,只能用列表去循环判断去重了 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for i in secretKey.upper(): #去重 if i not in set1: set1.append(i) # print('去重后输出:',set1) alphabet = list(alphabet) list1 = alphabet.copy()#转换为列表后复制一个新的,用作处理得到译文的尾缀 for j in set1: list1.remove(j) # print('加密的译文尾缀',list1) list2 = list(set1) + list1 #去重的密钥加上尾缀形成加密用的译文 # print('译文',list2) #加密过程,区分大小写和空格字符处理,最后join串联成字符串输出 encrypWord = [] for k in word: if k.isalpha() and k.isupper(): # print('密文大写字母',k) encrypWord.append(list2[alphabet.index(k)]) elif k.isalpha and k.islower(): # print('密文小写字母',k) encrypWord.append(list2[alphabet.index(k.upper())].lower()) else: # print('密文其他字符',k) encrypWord.append(list2[alphabet.index(k)]) # print(encrypWord) print("".join(encrypWord))
s1,alpb,alpN,s2 = '','abcdefghijklmnopqrstuvwxyz','','' for i in input(): if i not in s1: s1 += i for i in alpb: if i not in s1: alpN += i oringin = input() for i in oringin: for j in range(len(alpb)): if i == alpb[j]: s2 += (s1+ alpN)[j] print(s2) #申请了好多个字符串,但感觉这样直观一点就是
n=input() m=input() list1=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] res='' a=1 for i in n[::-1]: list1.remove(i) list1.insert(0,i) for i in m: a=ord(i)-97 res+=list1[a] print(res)
key=input() s=input() lkey=[] es='' le= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] for i in key: if i not in lkey: lkey.append(i) for i in le: if i not in lkey: lkey.append(i) for i in s: es+=lkey[le.index(i)] print(es)
‘’‘ 这题不难,但是需要理清思路。 1. 明确密钥只能包含重复字母中的第一个。因此遍历密钥,将其中第一个字母记录下来,形成uniq_key列表 2. 使用uniq_key作为新字母表的开头,并使用其它未出现的字母填充。因为需要考虑大小写, 所以生成两个新字母表full_list_az和full_list_AZ,前者存放uniq_key和小写字母,后者存放uniq_key和大写字母 3. 构建大小写字母对应的查找表。大写字母表[A, Z]对应full_list_AZ,小写字母表[a, z]对应full_list_az 4. 遍历明文,并将每个字母依次按照查找表转换。 ’’‘ import copy while True: try: # 得到密钥和铭文 secrect_key = input() plain = input() uniq_key = [] for k in secrect_key: if k not in uniq_key: # 得到没有重复的密钥 uniq_key.append(k) full_list_az = [chr(x) for x in range(ord('a'), ord('z')+1)] full_list_AZ = [chr(x) for x in range(ord('A'), ord('Z')+1)] uniq_key_az = copy.deepcopy(uniq_key) for kaz in full_list_az: if kaz.lower() not in uniq_key_az and kaz.capitalize() not in uniq_key_az: uniq_key_az.append(kaz) uniq_key_AZ = copy.deepcopy(uniq_key) for kAZ in full_list_AZ: if kAZ.lower() not in uniq_key_AZ and kAZ.capitalize() not in uniq_key_AZ: uniq_key_AZ.append(kAZ) assert len(uniq_key_az) == len(full_list_az) and len(uniq_key_AZ) == len(full_list_AZ) look_up_dict = {} for kaz, uniq_kaz, kAZ, uniq_kAZ in zip(full_list_az, uniq_key_az, full_list_AZ, uniq_key_AZ): look_up_dict[kaz] = uniq_kaz look_up_dict[kAZ] = uniq_kAZ res = '' for k in plain: if k.isalpha(): res += look_up_dict[k] else: res += k print(res) except: break
while True: try: temp_key, key = input(), "" for letter in temp_key: if letter not in key: key += letter temp_mapp, mapp = "abcdefghijklmnopqrstuvwxyz", "" for letter in temp_mapp: if letter not in key: mapp += letter mapp = sorted(mapp) mapp = "".join(mapp) key = key + mapp origin_map, key_map = {}, {} for i in range(26): origin_map[temp_mapp[i]] = i key_map[i] = key[i] code, output = input(), "" for letter in code: letter = origin_map[letter] letter = key_map[letter] output += letter print(output) except: break
key = input() str1 = input() list1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] list2 = [] for i in key: if i not in list2: list2.append(i) for i in list1: if (i not in list2) and (i.lower() not in list2): list2.append(i) for i in str1: if 'a'<= i <= 'z': index = list1.index(i.upper()) print(list2[index].lower(),end='') elif 'A'<= i <= 'Z': index = list1.index(i) print(list2[index].upper(),end='') else: print(i,end='')
alpha_lst = [chr(ord('a')+ i) for i in range(26)] key = input() strs = input() key_list = [] for i in key: if i not in key_list: key_list.append(i) else: continue alpha_lst_ = alpha_lst[:] for i in key_list: if i in alpha_lst: alpha_lst.remove(i) alpha_lst_encode = key_list+alpha_lst res = '' dic = dict(zip(alpha_lst_, alpha_lst_encode)) # print(dic) for i in strs: res= res+dic[i] print(res)
key = input() s = list(key) + [chr(ord('a') + i) for i in range(26)] key_list = [] for item in s: if str(item).lower() not in key_list: key_list.append(str(item).lower()) upperKey = ''.join(key_list).upper() lowerKey = ''.join(key_list).lower() enkey_str = input() for item in enkey_str: index = ord(str(item).upper()) - ord('A') if str(item).isupper(): print(upperKey[index:index + 1], end='') else: print(lowerKey[index:index + 1], end='')
while True: try: ss = [x.lower() for x in 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split(' ')] s1, s2 = input(), input() s_1 = [] for s in s1: if s not in s_1: s_1.append( s ) s1 = ''.join(s_1) + ''.join([x for x in ss if x not in s_1]) ssr = '' for s22 in s2: ssr += s1[ss.index( s22 )] print(ssr) except: break
while True: try: k = input() s = input() ke = "" for i in k: #单词字符串去重 if i not in ke: ke = ke + i lib = "abcdefghijklmnopqrstuvwxyz" key = "" for i in lib: #生成单词字符串之外的字母 if i not in ke: key = key + i key = ke + key #组合成密钥 for i in s: print(key[ord(i) - 97],end="") #按字母ASIIC数字大小,定位输出 except: break#就这样咯
import string a = input().lower() # 接受密钥 b = list(input()) # 接受明文 a1 = [] for i in a: if i not in a1: a1.append(i) # 密钥查重 c = list(string.ascii_lowercase) # 获得字母表 c1 = [] for j in c: if j not in a1: c1.append(j) # 获得修改后的字母表 c2 = a1 + c1 # 将修改后的字母表加入到密钥后来获取新字母表 d = [] for k in b: if k in c: d.append(c.index(k)) # 获得明文在新字母表中的位置 d1 = [] for m in d: m = int(m) d1.append(c2[m]) # 用该位置在新字母表中获得字符串 d1 = "".join(d1) print(d1) # 输出该字符串