解题思路

  1. 根据密钥构建加密字母表:

    • 去除密钥中重复字母,只保留第一次出现的字母
    • 将处理后的密钥放在新字母表的开头
    • 将剩余未出现在密钥中的字母按字母表顺序依次加入
  2. 使用新的字母表进行加密:

    • 对于明文中的每个字母,在原始字母表中找到其位置
    • 用新字母表中相同位置的字母替换

代码

#include <iostream>
#include <string>
#include <unordered_set>
#include <algorithm>
using namespace std;

string encrypt(string key, string text) {
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    transform(key.begin(), key.end(), key.begin(), ::toupper);
    
    // 构建新密码表
    string cipher;
    unordered_set<char> used;
    
    // 处理密钥
    for (char c : key) {
        if (used.find(c) == used.end() && alphabet.find(c) != string::npos) {
            cipher += c;
            used.insert(c);
        }
    }
    
    // 添加剩余字母
    for (char c : alphabet) {
        if (used.find(c) == used.end()) {
            cipher += c;
            used.insert(c);
        }
    }
    
    // 加密
    string result;
    transform(text.begin(), text.end(), text.begin(), ::tolower);
    for (char c : text) {
        if (isalpha(c)) {
            int idx = c - 'a';
            result += tolower(cipher[idx]);
        } else {
            result += c;
        }
    }
    
    return result;
}

int main() {
    string key, text;
    getline(cin, key);
    getline(cin, text);
    cout << encrypt(key, text) << endl;
    return 0;
}
import java.util.*;

public class Main {
    public static String encrypt(String key, String text) {
        String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        key = key.toUpperCase();
        
        // 构建新密码表
        StringBuilder cipher = new StringBuilder();
        Set<Character> used = new HashSet<>();
        
        // 处理密钥
        for (char c : key.toCharArray()) {
            if (!used.contains(c) && alphabet.indexOf(c) != -1) {
                cipher.append(c);
                used.add(c);
            }
        }
        
        // 添加剩余字母
        for (char c : alphabet.toCharArray()) {
            if (!used.contains(c)) {
                cipher.append(c);
                used.add(c);
            }
        }
        
        // 加密
        StringBuilder result = new StringBuilder();
        for (char c : text.toLowerCase().toCharArray()) {
            if (Character.isLetter(c)) {
                int idx = c - 'a';
                result.append(Character.toLowerCase(cipher.charAt(idx)));
            } else {
                result.append(c);
            }
        }
        
        return result.toString();
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String key = sc.nextLine();
        String text = sc.nextLine();
        System.out.println(encrypt(key, text));
    }
}
def encrypt(key, text):
    # 构建加密字母表
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    key = key.upper()
    
    # 去除密钥中的重复字母
    new_key = ''
    for c in key:
        if c not in new_key:
            new_key += c
    
    # 构建新的字母表
    cipher = ''
    for c in new_key:
        if c not in cipher and c in alphabet:
            cipher += c
    for c in alphabet:
        if c not in cipher:
            cipher += c
            
    # 加密过程
    result = ''
    for c in text.lower():
        if c.isalpha():
            # 找到原字母在原始字母表中的位置
            idx = ord(c) - ord('a')
            # 用新字母表中对应位置的字母替换
            result += cipher[idx].lower()
        else:
            result += c
            
    return result

# 处理输入
key = input()
text = input()
print(encrypt(key, text))

算法及复杂度

  • 算法:字符串处理 + 映射替换
  • 时间复杂度:,其中 n 为明文长度
  • 空间复杂度:,因为字母表大小是固定的(26个字母)