华为机考入门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)
相关推荐
深蓝海拓1 小时前
使matplot显示支持中文和负号
开发语言·python
kevien_G11 小时前
JAVA之二叉树
数据结构·算法
特立独行的猫a2 小时前
鸿蒙PC平台三方库移植实战:以libogg库移植为例(附完整移植流程与工具链配置)
华为·harmonyos·三方库移植·鸿蒙pc·libogg
AntBlack2 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
爱笑的眼睛112 小时前
深入理解HarmonyOS通知渠道与优先级设置:从基础到高级实践
华为·harmonyos
一眼万里*e2 小时前
搭建本地deepseek大模型
python
1***Q7842 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
syt_biancheng2 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法
二进制的Liao2 小时前
【编程】脚本编写入门:从零到一的自动化之旅
数据库·python·算法·自动化·bash
自然数e3 小时前
C++多线程【线程管控】之线程转移以及线程数量和ID
开发语言·c++·算法·多线程