• 华为机试 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

    在这里插入图片描述

  • 相关阅读:
    CentOS 7通过yum安装Docker和docker-compose
    需要在js中避免的几个常见错误
    【计算机网络】——前言计算机网络发展的历程概述
    科尔伯格道德发展阶段论:重点识记,比皮亚杰考频更高。
    数据仓库架构详解
    8、MySQL——数据的完整性
    13.前向、反向传播算法
    vue Router从入门到精通
    目标跟踪(DeepSORT)
    叁[3],感兴趣区域ROI
  • 原文地址:https://blog.csdn.net/AK47red/article/details/126208056