题解 | #字符串加密#
字符串加密
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;
}
华为题库题解 文章被收录于专栏
牛客华为题库的题解