酷代码 AI
菜单
服务商

使用C++实现S国军用密码破译及信息翻译

```cpp #include <iostream> #include <string> #include <vector> using namespace std; int main() { string encrypted, original, toTranslate; // 读取输入的加密信息 cin >> encrypted; // 读取输入的原信息 cin >> original; // 读取需要翻译的加密信息 cin >> toTranslate; vector<char> mapping(26, '\0'); vector<bool> used(26, false); for (int i = 0; i < encrypted.length(); i++) { int encryptedIndex = encrypted[i] - 'A'; int originalIndex = original[i] - 'A'; if (mapping[encryptedIndex] != '\0' && mapping[encryptedIndex] != original[i]) { // 违反不同字母对应不同密字的规则 cout << "Failed" << endl; return 0; } if (used[originalIndex] && mapping[encryptedIndex] != original[i]) { // 同一个原字母对应不同的密字 cout << "Failed" << endl; return 0; } mapping[encryptedIndex] = original[i]; used[originalIndex] = true; } for (int i = 0; i < 26; i++) { if (!used[i]) { // 存在字母在原信息中未出现 cout << "Failed" << endl; return 0; } } for (char c : toTranslate) { if (mapping[c - 'A'] == '\0') { // 无法找到对应的原字母 cout << "Failed" << endl; return 0; } cout << mapping[c - 'A']; } cout << endl; return 0; } ``` ######[AI问答 | 447点数解答 | 2026-05-24 15:57:19]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]