华为机考入门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)
相关推荐
华如锦8 小时前
面了很多 Java转AI Agent方向,一些面试题总结
java·开发语言·人工智能·python·ai
退休倒计时8 小时前
【每日一题】LeetCode 53. 最大子数组和 TypeScript
数据结构·算法·leetcode·typescript
旖-旎8 小时前
FloodFill(图像渲染)(1)
c++·算法·深度优先·力扣
戴西软件8 小时前
戴西 DLM 许可授权管理系统:破解无网络环境下工业软件授权难题,助力制造企业降本增效
网络·人工智能·python·深度学习·程序人生·算法·制造
Dxy12393102169 小时前
Python线程锁:为什么多线程会“打架“,以及怎么解决
开发语言·前端·python
2601_961875249 小时前
法考资料2026|全套|资料已整理
数据结构·算法·链表·贪心算法·eclipse·线性回归·动态规划
无限码力9 小时前
美团研发岗 4月18号笔试真题 - 坐标
算法·美团笔试真题·美团笔试题·美团研发岗笔试题·美团研发岗4月18号真题
小白学大数据9 小时前
线上故障急救:依托 OpenClaw 日志排查 403 和 503 问题
爬虫·python·selenium·数据分析
有点。9 小时前
C++倍增法(练习题)
c++·算法