题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string build_key(const string& key) {
string chars = "abcdefghijklmnopqrstuvwxyz";
string mapping(chars);
size_t index = 0;
for (auto c : key) {
int i = c - 'a';
if (chars[i] != '0') {
chars[i] = '0';
mapping[index++] = c;
}
}
for (auto c : chars) {
if (c != '0') {
mapping[index++] = c;
}
}
return mapping;
}
string encrypt(const string& key, const string& text)
{
string data(text);
for (size_t i = 0; i < data.size(); ++i) {
auto upper = isupper(data[i]);
auto index = data[i] - (upper ? 'A' : 'a');
data[i] = upper ? (key[index] - 'a' + 'A') : key[index];
}
return data;
}
int main() {
string text, key;
while (cin >> key >> text) {
key = build_key(key);
text = encrypt(key, text);
cout << text << endl;
}
}
// 64 位输出请用 printf("%lld")


查看10道真题和解析