题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <cctype> #include <iostream> #include <string> #include <vector> #include<algorithm> using namespace std; int main() { string key; string words; cin >> key; cin >> words; vector<char> dict; for(char & i : key) //遍历密钥key { i = toupper(i); auto it = find(dict.begin(),dict.end(),i); if(it == dict.end()) { dict.push_back(i); } } for(char a = 'A';a <= 'Z';a++) { auto it = find(dict.begin(),dict.end(),a); if(it == dict.end()) { dict.push_back(a); } } string outstr = ""; for (int i = 0;i < words.size();i++) { if(islower(words[i])) { outstr += dict[words[i] - 'a'] + 32; //dict中的索引值为0~25,words[i] - 'a'是找到对应在dict中的索引值,然后取出dict[words[i] - 'a']的大写字母,最后+32转换成小写字母。 } else { outstr += dict[words[i] - 'A']; } } cout << outstr; } // 64 位输出请用 printf("%lld")