题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

#include <iostream>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main() {
    string key, str1, key1, res;
    cin >> key >> str1;
    for (auto& i : key) {
        if (isupper(i))
            i = tolower(i);
    }
    unordered_set<char> charSet;
    for (auto c : key) {
        if (charSet.insert(c).second) {
            // 插入成功,说明该字符未出现过
            key1 += c;
        }
    }
    char b = 'a';
    for (int i = 0; i < 26; i++) {
        if (charSet.insert(b).second) {
            // 插入成功,说明该字符未出现过
            key1 = key1 + b;
        }
        b = b + 1;
    }
    //cout << key1 << endl;
    for (auto i : str1) {
        res = res + key1[int (i) - 97];
    }
    cout << res;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务