题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <iostream>
#include <string>
using namespace std;
//密钥类
class Keys {
private:
string s;
public:
//构造函数
Keys(const string s);
//析构函数
~Keys();
//去除重复字母
void deduplicate(void);
//补全密码本
void comple(void);
//建立字母间的映射关系
const char correspond(const char ch)const;
//转化字符串
const string converse(const string s)const;
};
Keys::Keys(const string s) {
this->s = s;
return;
}
Keys::~Keys() {
}
void Keys::deduplicate(void) {
//遍历字符串,依次排查每个字母的重复字母
for (int i = 0; i < this->s.size(); i++) {
char ch = this->s[i];
for (int j = i + 1; j < this->s.size(); j++)
if (this->s[j] == ch) {
this->s.erase(j, 1);
j--;
}
}
return;
}
void Keys::comple(void) {
//在后面添加字母表
this->s += string("abcdefghijklmnopqrstuvwxyz");
//去除重复字母
this->deduplicate();
return;
}
const char Keys::correspond(const char ch) const {
//由于输入内容只有小写字母,可以不做判断直接返回密码本中对应的字母
return this->s[ch - 'a'];
}
const string Keys::converse(const string s) const {
//对每个字母依次转换
string s1 = s;
for (int i = 0; i < s1.size(); i++)
s1[i] = this->correspond(s1[i]);
return string(s1);
}
int main() {
string a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
Keys key(a);
key.comple();
cout << key.converse(b) << endl;
}
}
// 64 位输出请用 printf("%lld")
查看22道真题和解析