题目描述
有一种技巧可以对数据进行加密,它使用一个单词作为它的密匙。下面是它的工作原理:首先,选择一个单词作为密匙,如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。
请实现下述接口,通过指定的密匙和明文得到密文。
详细描述:
接口说明
原型:
voidencrypt(char * key,char * data,char * encrypt);
输入参数:
char * key:密匙
char * data:明文
输出参数:
char * encrypt:密文
返回值:
void
输入描述:
先输入key和要加密的字符串
输出描述:
返回加密后的字符串
输入例子:
nihao ni
输出例子:
le
做这个题收获的最大经验就是制表法,通过四张string类型的表轻松解决,分别为大小写字母表和加密后的大小写字母表,然后要加密的字符串根据加密表查找就行了。
完整AC过的代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string key,str;
while(cin>>key>>str){
string uniqueStr;
for(auto c:key){//去重
if(uniqueStr.find(c)==string::npos)
uniqueStr+=c;
}
string littleForm="abcdefghijklmnopqrstuvwxyz";
string bigForm="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string encryptLittleForm=littleForm;
string encryptBigForm=bigForm;
if(islower(uniqueStr[0])){//小写
encryptLittleForm.erase(0,uniqueStr.size());
encryptLittleForm.insert(0,uniqueStr);
int j=uniqueStr.size();
for(int i=0;i<26;i++){
if(encryptLittleForm.find(littleForm[i])==string::npos){
encryptLittleForm[j]=littleForm[i];
j++;
}
}
for(auto &c:str){
int pos=littleForm.find(c);
c=encryptLittleForm[pos];
}
}
else{//大写
encryptBigForm.erase(0,uniqueStr.size());
encryptBigForm.insert(0,uniqueStr);
int j=uniqueStr.size();
for(int i=0;i<26;i++){
if(encryptBigForm.find(bigForm[i])==string::npos){
encryptBigForm[j]=bigForm[i];
j++;
}
}
for(auto &c:str){
int pos=bigForm.find(c);
c=encryptBigForm[pos];
}
}
//cout<<encryptBigForm<<endl<<encryptLittleForm<<endl;
cout<<str<<endl;
}
return 0;
}