题解 | 字符串加密

#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;

string origin_alphabeta = "abcdefghijklmnopqrstuvwxyz";

string ProcessString(const string &s){
    string temp_str = s;
    // 1. 去重 采用hashmap
    unordered_map<char, int> map;
    string ss; // 保存去重后的字符串
    for (char & ch : temp_str) {
        if (map.find(ch) != map.end()) {
            continue;
        } else {
            map[ch] ++;
            ss += ch;
        }
    }

    // 2. 补充未出现的字母
    string diff_str;
    for (char & ch_alphabeta : origin_alphabeta) {
        if (ss.find(ch_alphabeta) != ss.npos) {
            continue;
        } else {
            diff_str += ch_alphabeta;
        }
    }
    sort(diff_str.begin(), diff_str.end());

    // 3. 组合新的密码表
    return ss + diff_str;

}

string ProcessCode(const string& new_alphabeta, const string & t)
{   
    string res;
    string temp_t = t;
    for (char i : temp_t) {
        int pos = origin_alphabeta.find(i);
        res += new_alphabeta[pos];
    }

    return res;
}

int main() {
    string s, t;
    cin >> s >> t;

    // 1. 处理字符串s
    string new_alphabeta = ProcessString(s);
    // 2. 输出加密后的密文
    string code = ProcessCode(new_alphabeta, t);

    cout << code << endl;

    // while (cin >> a >> b) { // 注意 while 处理多个 case
    //     cout << a + b << endl;
    // }

    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:15
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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