题解 | #字符串加密#

字符串加密

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

#include <iostream>
using namespace std;
#include <string>
#include <sstream>
#include <map>
#include <cctype>
int main() {
    string str1;
    string str2;
    getline(cin, str1);
    getline(cin, str2);
    map<char, char> keyMap;
    map<char, bool> wordMap;
    istringstream is(str1);
    char word;
    char key;
    int cnt = 0;
    while (is >> word) {
        if (wordMap.count(word) == 0) {
            wordMap.insert(pair<char, bool>(word, true));
            if (islower(word)) {
                key = 'a' + cnt;
                keyMap.insert(pair<char, char>(key, word));
                word = toupper(word);
                key = toupper(key);
                keyMap.insert(pair<char, char>(key, word));
                wordMap.insert(pair<char, bool>(word, true));
            } else if (isupper(word)) {
                key = 'A' + cnt;
                keyMap.insert(pair<char, char>(key, word));
                word = tolower(word);
                key = tolower(key);
                keyMap.insert(pair<char, char>(key, word));
                wordMap.insert(pair<char, bool>(word, true));
            }
            cnt++;
        }
    }
    for (int i = 0; i <= 25; i++) {
        word = 'a' + i;
        if (wordMap.count(word) == 0) {
            key = 'a' + cnt;
            wordMap.insert(pair<char, bool>(word, true));
            keyMap.insert(pair<char, char>(key, word));
            word = toupper(word);
            key = toupper(key);
            keyMap.insert(pair<char, char>(key, word));
            wordMap.insert(pair<char, bool>(word, true));
            cnt++;
        }
    }

    for (int i = 0; i < str2.size(); i++) {
        cout <<keyMap[str2[i]];
    }
}

用两个图,一个图存曾经输入过的字母,另一个存字母密文的对应关系就行,记得把大小写都存进去

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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