3 solutions

  • 1
    @ 2025-8-28 18:14:20

    #include #include #include #include #include

    using namespace std;

    // 计算从明文字母到密文字母的偏移量(密钥) int getShift(char plain, char cipher) { return (cipher - plain + 26) % 26; }

    // 检查密钥是否有效 bool isValidKey(const string& T, const string& word, int key) { if (T.length() != word.length()) return false;

    for (int i = 0; i < T.length(); ++i) { char expected = (T[i] - 'a' + key) % 26 + 'a'; if (word[i] != expected) { return false; } } return true; }

    int main() { string T, S; getline(cin, T); getline(cin, S);

    // 将密文按空格分割成单词 vector cipherWords; stringstream ss(S); string word; while (ss >> word) { cipherWords.push_back(word); }

    vector possibleKeys;

    // 检查每个可能的密钥(1-25) for (int key = 1; key <= 25; ++key) { // 检查是否有任何密文单词与T在该密钥下匹配 for (const string& cipherWord : cipherWords) { if (isValidKey(T, cipherWord, key)) { possibleKeys.push_back(key); break; // 找到一个匹配即可确认该密钥有效 } } }

    // 输出结果 if (possibleKeys.empty()) { cout << "Error" << endl; } else { sort(possibleKeys.begin(), possibleKeys.end()); cout << possibleKeys.size() << endl; for (size_t i = 0; i < possibleKeys.size(); ++i) { if (i > 0) cout << " "; cout << possibleKeys[i]; } cout << endl; }

    return 0; }

    • 0
      @ 2026-6-26 21:54:13
      #include #include #include #include #include
      
      using namespace std;
      
      // 计算从明文字母到密文字母的偏移量(密钥) int getShift(char plain, char cipher) { return (cipher - plain + 26) % 26; }
      
      // 检查密钥是否有效 bool isValidKey(const string& T, const string& word, int key) { if (T.length() != word.length()) return false;
      
      for (int i = 0; i < T.length(); ++i) { char expected = (T[i] - 'a' + key) % 26 + 'a'; if (word[i] != expected) { return false; } } return true; }
      
      int main() { string T, S; getline(cin, T); getline(cin, S);
      
      // 将密文按空格分割成单词 vector cipherWords; stringstream ss(S); string word; while (ss >> word) { cipherWords.push_back(word); }
      
      vector possibleKeys;
      
      // 检查每个可能的密钥(1-25) for (int key = 1; key <= 25; ++key) { // 检查是否有任何密文单词与T在该密钥下匹配 for (const string& cipherWord : cipherWords) { if (isValidKey(T, cipherWord, key)) { possibleKeys.push_back(key); break; // 找到一个匹配即可确认该密钥有效 } } }
      
      // 输出结果 if (possibleKeys.empty()) { cout << "Error" << endl; } else { sort(possibleKeys.begin(), possibleKeys.end()); cout << possibleKeys.size() << endl; for (size_t i = 0; i < possibleKeys.size(); ++i) { if (i > 0) cout << " "; cout << possibleKeys[i]; } cout << endl; }
      
      return 0; }
      
      • -1
        @ 2025-7-25 10:23:59

        #include #include #include #include #include

        using namespace std;

        // 计算从明文字母到密文字母的偏移量(密钥) int getShift(char plain, char cipher) { return (cipher - plain + 26) % 26; }

        // 检查密钥是否有效 bool isValidKey(const string& T, const string& word, int key) { if (T.length() != word.length()) return false;

        for (int i = 0; i < T.length(); ++i) {
            char expected = (T[i] - 'a' + key) % 26 + 'a';
            if (word[i] != expected) {
                return false;
            }
        }
        return true;
        

        }

        int main() { string T, S; getline(cin, T); getline(cin, S);

        // 将密文按空格分割成单词
        vector<string> cipherWords;
        stringstream ss(S);
        string word;
        while (ss >> word) {
            cipherWords.push_back(word);
        }
        
        vector<int> possibleKeys;
        
        // 检查每个可能的密钥(1-25)
        for (int key = 1; key <= 25; ++key) {
            // 检查是否有任何密文单词与T在该密钥下匹配
            for (const string& cipherWord : cipherWords) {
                if (isValidKey(T, cipherWord, key)) {
                    possibleKeys.push_back(key);
                    break; // 找到一个匹配即可确认该密钥有效
                }
            }
        }
        
        // 输出结果
        if (possibleKeys.empty()) {
            cout << "Error" << endl;
        } else {
            sort(possibleKeys.begin(), possibleKeys.end());
            cout << possibleKeys.size() << endl;
            for (size_t i = 0; i < possibleKeys.size(); ++i) {
                if (i > 0) cout << " ";
                cout << possibleKeys[i];
            }
            cout << endl;
        }
        
        return 0;
        

        }

        • 1

        Information

        ID
        2610
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        10
        Tags
        (None)
        # Submissions
        26
        Accepted
        1
        Uploaded By