package com.patience.interview.huawei;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* 明文加密
* @author Green.Gee
* @date 2022/9/12 20:06
* @email green.gee.lu@gmail.com
*/
public class LetterEncrypt {
private static final char[] A = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static final String[] a = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
/**
* 密码加密
* 加密key 过滤重复字符
* 将过滤后的字符串追加为完整的英文字母表
* 输入代价密内容对照字母表映射为加密字母表中的字符替换
*
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String key = br.readLine();
// repeat
StringBuffer repeatKey = new StringBuffer(key.charAt(0)+"");
for(int i = 1; i < key.length(); i++){
repeatKey(repeatKey,(key.charAt(i)+""));
}
// whole letters
String[] copy_a = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
StringBuffer wholeLetters = new StringBuffer();
for(int i = 0; i < repeatKey.length(); i++){
for(int j = 0;j < copy_a.length; j++){
if(copy_a[j] != null && copy_a[j].equals((repeatKey.charAt(i) + ""))){
wholeLetters.append(copy_a[j]);
copy_a[j] = null;
}
}
}
for(int j = 0;j < a.length; j++) {
if (copy_a[j] != null) {
wholeLetters.append(copy_a[j]);
}
}
String [] letters = wholeLetters.toString().split("");
String text = br.readLine();
String[] tx = text.split("");
StringBuffer result = new StringBuffer();
for (String item : tx){
for(int i = 0; i < a.length; i++){
if(item.equals(a[i])){
result.append(letters[i]);
}
}
}
System.out.println(result.toString());
}
private static StringBuffer repeatKey(StringBuffer repeatKey,String c){
if(!repeatKey.toString().contains(c)){
repeatKey.append(c);
}
return repeatKey;
}
}
明文加密(密码字符)
最新推荐文章于 2023-08-29 11:31:44 发布