华为机考入门python3--(36)牛客36-字符串加密

分类:字符串

知识点:

  1. 判断一个元素是否在集合中 if char not in key_set

  2. 计算字母差 index = ord(char) - ord('a')

题目来自【牛客】

python 复制代码
# 生成加密表
def generate_cipher_table(key):
    key_set = set()
    cipher_table = ""

    # 去重
    for char in key:
        if char not in key_set:
            cipher_table += char
            key_set.add(char)

    # 未出现的字母按照正常字母表顺序加入新字母表
    for char in "abcdefghijklmnopqrstuvwxyz":
        if char not in key_set:
            cipher_table += char

    return cipher_table


# 使用给定的密匙加密信息
def encrypt_message(cipher_table, message):
    result = ""
    for char in message:
        # 计算索引
        index = ord(char) - ord('a')
        encrypted_char = cipher_table[index]
        result += encrypted_char
    return result

# 输入
key = input().strip()
message = input().strip()

# 生成加密表和加密信息,然后输出结果
cipher_table = generate_cipher_table(key)
encrypted_message = encrypt_message(cipher_table, message)
print(encrypted_message)
相关推荐
ZhengEnCi36 分钟前
S10-蓝桥杯 17822 乐乐的积木塔
算法
贾斯汀玛尔斯39 分钟前
每天学一个算法--拓扑排序(Topological Sort)
算法·深度优先
大龄程序员狗哥1 小时前
第25篇:Q-Learning算法解析——强化学习中的经典“价值”学习(原理解析)
人工智能·学习·算法
exp_add31 小时前
质数相关知识
算法
lulu12165440781 小时前
Claude Code项目大了响应慢怎么办?Subagents、Agent Teams、Git Worktree、工作流编排四种方案深度解析
java·人工智能·python·ai编程
小辉同志2 小时前
215. 数组中的第K个最大元素
数据结构·算法·leetcode··快速选择
Ares-Wang2 小时前
Flask》》 Flask-Bcrypt 哈希加密
后端·python·flask
小O的算法实验室2 小时前
2025年IEEE TITS,基于矩阵的进化计算+面向无线传感器网络数据收集无人机路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
kongba0072 小时前
项目打包 Python Flask 项目发布与打包专家 提示词V1.0
开发语言·python·flask
OidEncoder2 小时前
编码器分辨率与机械精度的关系
人工智能·算法·机器人·自动化