• 华为机试 HJ36 字符串加密【Java实现】


    package com.wy.leetcode;
    
    import java.util.Scanner;
    
    /**
     * @author HelloWorld
     * @create 2022/8/7 10:27
     * @email helloworld.dng@gmail.com
     * HJ36 字符串加密
     */
    public class HJ36 {
        /*
                1. 获得新的字母表。输入的 key + [a-z] 并去重得到新的字母表
                2. 取出明文对应的字母得到密文
        */
    
        /** 标准字母表 */
        private static final String STANDARD_TABLE = "abcdefghijklmnopqrstuvwxyz";
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String key = scanner.next();
            String msg = scanner.next();
    
            doDealInput(key, msg);
        }
    
        private static void doDealInput(String key, String msg) {
            // 1. 获取新的字母表
            String newTable = getNewTable(key);
            // 2. 获取密文
            String keyword = getKeyword(msg, newTable);
    
            System.out.println(keyword);
        }
    
        /**
         * @description 获取新的字母表
         * @author HelloWorld
         * @create 2022/8/7 10:39
         * @param key
         * @return java.lang.String
         */
        private static String getNewTable(String key) {
            String temp = "";
            String table = STANDARD_TABLE;
            for (char c : key.toCharArray()) {
                // key 中字母去重
                if (!temp.contains(c + "")) {
                    temp += c;
                }
                table = table.replace(c + "", "");
            }
            return temp + table;
        }
    
        /**
         * @description 加密明文
         * @author HelloWorld
         * @create 2022/8/7 10:41
         * @param msg
         * @param newTable
         * @return java.lang.String
         */
        private static String getKeyword(String msg, String newTable) {
            String keyword = "";
            for (char c : msg.toCharArray()) {
                int index = c - 'a';
                keyword += newTable.toCharArray()[index];
            }
    
            return keyword;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    在这里插入图片描述

  • 相关阅读:
    小迪安全29WEB 攻防-通用漏洞&SQL 注入&增删改查&盲注&延时&布尔&报错
    怎么从零开始搭建配置Windows云服务器的新手入门教程
    [Mysql] 删除数据
    【jmeter】windows下使用 (测试MQTT)
    Docker consul的容器服务更新与发现
    Linux 常用命令练习二 实验二
    【C语言】实现通讯录管理系统
    mac flutter 配置
    Scss--@mixin--使用/实例
    opencv判断灰化情况
  • 原文地址:https://blog.csdn.net/AK47red/article/details/126208056