题目描述
有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:首先,选择一个单词作为密匙,如TRAILBLAZERS。如果单词中包含有重复的字母,只保留第1个,将所得结果作为新字母表开头,并将新建立的字母表中未出现的字母按照正常字母表顺序加入新字母表。如下所示:
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
T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
(实际需建立小写字母的字母表,此字母表仅为方便演示)
上面其他用字母表中剩余的字母填充完整。在对信息进行加密时,信息中的每个字母被固定于顶上那行,并用下面那行的对应字母一一取代原文的字母(字母字符的大小写状态应该保留)。因此,使用这个密匙, Attack AT DAWN (黎明时攻击)就会被加密为Tpptad TP ITVH。
请实现下述接口,通过指定的密匙和明文得到密文。
数据范围:
1
≤
n
≤
100
1≤n≤100
1≤n≤100 ,保证输入的字符串中仅包含小写字母
示例1
输入:nihao
ni
输出:le
题解
下面展示本人题解思路代码。
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
// 得到密钥
function transformKey(key) {
key += "abcdefghijklmnopqrstuvwxyz";
let arr = key.split("");
// 去重
let set = new Set(arr);
let arr1 = [...set];
return arr1;
}
void (async function () {
let key = await readline();
let str = await readline();
let arr = transformKey(key);
//
let result = [];
for (let i = 0; i < str.length; i++) {
if (
str[i].charCodeAt(0) >= "a".charCodeAt(0) &&
str[i].charCodeAt(0) <= "z".charCodeAt(0)
) {
let index = str[i].charCodeAt(0) - "a".charCodeAt(0);
result.push(arr[index].toLowerCase());
}else if (
str[i].charCodeAt(0) >= "A".charCodeAt(0) &&
str[i].charCodeAt(0) <= "Z".charCodeAt(0)
) {
let index = str[i].charCodeAt(0) - "A".charCodeAt(0);
result.push(arr[index].toUpperCase());
}else{
result.push(str[i])
}
}
console.log(result.join(''))
})();
文章系原创,在阅读过程中如若有误,劳请指正;如若有妙解、疑惑也欢迎大家和我交流,感谢!