题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <unordered_map>
using namespace std;
int main(){
string key;
string str;
cin >>key >> str;
vector<int>keyBucket(26);
unordered_map<char, char> map;
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string reAlphabet;
// key去重
for(char c : key){
if( !keyBucket[c - 'a'] ){
reAlphabet += c;
keyBucket[c-'a']++;
}
}
// // cout << "clear key : " << endl;
// for(auto x : key)
// cout << x;
// 补全新字母表
for(int i = 0; i < 26; i++){
if( !keyBucket[i] )
reAlphabet += i + 'a';
}
// cout << "new Alphabet: "<< endl;
// for(auto x : reAlphabet)
// cout << x ;
// 新旧字母表映射
for(int i = 0 ; i < 26; i++){
map[alphabet[i]] = reAlphabet[i];
}
// cout << "alphabet map : " << endl;
// for(auto x : map)
// cout << x.first << ',' << x.second << endl;
for(char cur : str)
cout << map[cur];
}

查看11道真题和解析