自己写的。
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.Scanner; import java.lang.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String key = in.nextLine(); String key_back = key; String str = in.nextLine(); char[] table = new char[26]; int k = 0; for (int i = 0; i < key.length();) { String temp = key.substring(i, i + 1); String temp2 = key.substring(i + 1); if (temp2 == null || !temp2.contains(temp)) { table[k++] = temp.charAt(0); key = temp2; } else { StringBuffer sb = new StringBuffer(temp2); while (sb.indexOf(temp) != -1) { sb.deleteCharAt(sb.indexOf(temp)); } key = sb.toString(); table[k++] = temp.charAt(0); } } String old = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < k; i++) { StringBuffer sb2 = new StringBuffer(old); sb2.deleteCharAt(sb2.indexOf(Character.toString(table[i]))); old = sb2.toString(); } for (int i = 0; k < 26; k++) { table[k] = old.charAt(i++); } String res = ""; for (int i = 0; i < str.length(); i++) { res += Character.toString((table[str.charAt(i) - 'a'])); } System.out.println(res); } } }