题解 | #字符串加密#

字符串加密

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")

全部评论

相关推荐

04-28 22:33
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
05-12 17:00
门头沟学院 Java
king122:你的项目描述至少要分点呀,要实习的话,你的描述可以使用什么技术,实现了什么难点,达成了哪些数字指标,这个数字指标尽量是真实的,这样面试应该会多很多,就这样自己包装一下,包装不好可以找我,我有几个大厂最近做过的实习项目也可以包装一下
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务