题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String key = in.nextLine(); String str = in.nextLine(); decode(key, str); } } public static void decode(String key, String str) { HashSet<Character> set = new HashSet<>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < key.length(); i++) { char ch = key.charAt(i); if (!set.contains(ch)) { set.add(ch); sb.append(ch); } } //去除了重复字母 String newKey = sb.toString(); //System.out.println(newKey); //存储映射后的字符串 StringBuilder mapedStr = new StringBuilder(); mapedStr.append(newKey); for (int i = 0; i < 26; i++) { char ch = (char) ('a' + i); if (!set.contains(ch)) { mapedStr.append(ch); } } //System.out.println(mapedStr); HashMap<Character, Character> resultMap = new HashMap<>(); for (int i = 0; i < 26; i++) { char ch = (char) ('a' + i); resultMap.put(ch, mapedStr.charAt(i)); } StringBuilder result = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); Character newCh = resultMap.get(ch); result.append(newCh); } System.out.println(result.toString()); } }
就是无脑模拟加密后字符串的生成规则即可。
#在找工作求抱抱#