华为机考入门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)
相关推荐
雨墨✘12 小时前
PHP怎么执行Shell命令_exec与shell_exec区别说明【说明】
jvm·数据库·python
人工智能培训12 小时前
如何将高层任务分解为可执行的动作序列?
大数据·人工智能·算法·机器学习·知识图谱
罗湖老棍子12 小时前
Power Strings(信息学奥赛一本通- P1457)
算法·字符串·哈希
MIngYaaa52012 小时前
The 2025 Sichuan Provincial Collegiate Programming Contest 复盘
算法
网域小星球13 小时前
C 语言从 0 入门(二十一)|typedef 类型重定义:简化复杂类型,代码更清爽
c语言·算法·类型重定义·结构体简化·函数指针简化
2201_7568473313 小时前
mysql字段长度不够用了怎么办_使用alter table扩大varchar长度
jvm·数据库·python
XWalnut13 小时前
LeetCode刷题 day10
数据结构·算法·leetcode
祁_z13 小时前
Python项目依赖管理:venv与conda
python
overmind13 小时前
oeasy Python 120[专业选修]列表_直接赋值_浅拷贝_shallowcopy_深拷贝_deepcopy
linux·windows·python
ZC跨境爬虫13 小时前
海南大学交友平台开发实战 day11(实现性别图标渲染与后端数据关联+Debug复盘)
前端·python·sqlite·html·json