题解 | #字符串加密#

字符串加密

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

#include <bits/stdc++.h>

using namespace std;

//encoder
void encoder(string key, string str, string& res){
    //如果单词中包含有重复的字母,只保留第1个,将所得结果作为新字母表开头
    vector<char> s; //s[i]对应的便是26个字母中第i个字母的加密结果
    for(int i = 0; i < key.size(); i++){
        key[i] = tolower(key[i]); //全部转小写
        if(find(s.begin(), s.end(), key[i]) == s.end()) //如果是第一次加入
            s.push_back(key[i]);
    }
    for(char c = 'a'; c <= 'z'; c++){
        if(find(s.begin(), s.end(), c) == s.end()) //如果是第一次加入
            s.push_back(c);
    }
    /*for(char ch : s){
        cout << ch;
    }*/
    
    for(int i = 0; i < str.size(); i++){
        if(isupper(str[i])){
            res += s[str[i] - 'A'] - 32; //需要在转出来的小写字母基础上减32
        }
        else{
            res += s[str[i] - 'a']; //
        }
    }
}

int main(){
    string key = "";
    cin >> key;
    string str = "";
    cin >> str;
    
    string res = "";
    encoder(key, str, res);
    
    cout << res << endl;
    
    return 0;
}

华为题库题解 文章被收录于专栏

牛客华为题库的题解

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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