题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
string alphabet = "abcdefghijklmnopqrstuvwxyz"; //普通26个字母表
string key; //密钥key
string s; //要加密的字符串s
cin >> key;
cin >> s;
alphabet = key + alphabet; //首先直接将key拼接到字母表开头
for (int i = 0; i < alphabet.size(); i++) //然后遍历整个拼接后的字母表,每有字母重复出现,将该位置改为'0'
{
for (int j = 0; j < i; j++)
{
if (alphabet[i] == alphabet[j])
{
alphabet[i] = '0';
}
}
}
string newalph = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0,j=0; i < alphabet.size(); i++) //将拼接后的字母表中所有不为'0'的部分按顺序存入新字母表newalph中
{
if (alphabet[i] != '0')
{
newalph[j] = alphabet[i];
j++;
}
}
for(int i = 0; i < s.size(); i++) //按照新字母表newalph,加密字符串s
{
s[i] = newalph[s[i] - 'a'];
}
cout << s << endl;
return 0;
}
传音控股公司福利 356人发布

