字符串加密(C++牛客网)

解题思路:

(1)先构建键值对,再加密

#include<iostream>
#include<unordered_map>


using namespace std;

string helper(string &s1,string &s2) {
    string str="";
    unordered_map<char,int> mp;
    for(int i=0;i<s1.length();i++) {
        s1[i]=tolower(s1[i]);
        if(mp[s1[i]]==0) {
            str+=s1[i];
            mp[s1[i]]=1;
        }
    }
    
    int len=str.length();
    char a='a';
    int i=0;
    while(str.length()<26) {
        a = (char)('a'+i);
        if(mp.find(a)==mp.end()) {
            str+=(a);
            mp[a]=1;
        }
        i++;
    }
    string temp="";
    for(int i=0;i<s2.length();i++) {
        if(islower(s2[i])) {
            temp+=str[s2[i]-'a'];
        } else if(isupper(s2[i])) {
            temp+=toupper(str[tolower(s2[i])-'a']);
        } else temp+=s2[i];
    }
    return temp;
    
}

int main() {
    string s1="",s2="";
    while(cin>>s1) {
        cin>>s2;
        cout<<helper(s1,s2)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coolsunxu/article/details/114937528