题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {



            //odd 奇数
            //even 偶数
            String key = str;
            str = bf.readLine();
            String cont = str;
            char[] chars = key.toCharArray();
            int j = 0;
            HashMap<String, String> hashMap = new HashMap<>();
            ArrayList<Character> characters = new ArrayList<>();
            for (int i = 0; i < chars.length; i++) {
                if (characters.contains(chars[i])) {
                    continue;
                } else {
                    characters.add(chars[i]);
                }
            }
            for (int i = 0; i < characters.size(); i++) {

                hashMap.put(characters.get(i) + "", String.valueOf((char) ('a' + i)));
                j = i;
            }

            for (int i = 0; i < 26; i++) {
                String s = String.valueOf((char) ('a' + i));

                if (hashMap.containsKey(s)) {

                    continue;
                } else {
                    String temp = String.valueOf((char) ('a' + j + 1));
                    hashMap.put(s, temp);
                    j++;
                }
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cont.length(); i++) {
                sb.append(getKeyByValue(hashMap, String.valueOf(cont.charAt(i))));
            }
            System.out.println(sb.toString());


        }


    }
    public static String getKeyByValue(HashMap<String, String> hashMap,
                                       String value) {
        for (String s : hashMap.keySet()) {
            if (value.equals(hashMap.get(s))) {
                return s;
            }
        }
        return "";
    }



}

全部评论

相关推荐

09-10 15:05
已编辑
门头沟学院 游戏后端
点赞 评论 收藏
分享
09-22 22:22
中山大学 Java
双尔:赌对了,不用经历秋招的炼狱真的太好了,羡慕了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务