Python真正的密码

B站影视 2025-01-17 20:01 3

摘要:def find_true_password(Q):# 将输入字符串数组转换为集合,方便快速查找word_set = set(Q)potential_passwords = # 筛选潜在密码for word in Q:is_potential = Truefo

def find_true_password(Q):# 将输入字符串数组转换为集合,方便快速查找word_set = set(Q)potential_passwords = # 筛选潜在密码for word in Q:is_potential = Truefor i in range(1, len(word) + 1):if word[:i] not in word_set:is_potential = Falsebreakif is_potential:potential_passwords.append(word)# 如果没有潜在密码,返回空字符串if not potential_passwords:return ""# 找到最长的潜在密码max_length = max(len(word) for word in potential_passwords)candidates = [word for word in potential_passwords if len(word) == max_length]# 如果有多个候选,选择字典序最大的true_password = max(candidates, key=lambda x: x)return true_password# 自定义输入def main:# 输入字符串数组 Qinput_str = input("请输入字符串数组(以空格分隔,例如:h he hel hell hello):")Q = input_str.split # 将输入字符串按空格分割为列表# 调用函数并输出结果result = find_true_password(Q)print("唯一的真正密码是:", result)# 运行主函数if __name__ == "__main__":main

来源:芳芳课堂

相关推荐