B4500 GESP202603 三级 凯撒密码 详细解题报告
1. 题目分析与样例模拟
1.1 题目需求概括
凯撒密码是一种将字母按固定偏移量循环替换的加密技术。本题给定一组已破解的明文与密文 ,以及一段待解密的密文。要求:
- 利用已知的明密文对,反推出统一的偏移量 ddd。
- 使用相同的偏移量 ddd,将待解密的密文还原为明文。
数据范围 :所有字符串仅由大写字母组成,长度均不超过 10310^3103。偏移量在 0∼250 \sim 250∼25 之间(若密文由明文向前偏移得到,则视为等价于向后偏移 26−d26 - d26−d 位)。
1.2 样例模拟
输入:
text
ABCDEFGVWXYZ
DEFGHIJYZABC
WKHTXLFNEURZQIRAMXPSVRYHUWKHODCBGRJ
第一步:求偏移量
取已破解明文的第一个字符 A 与密文的第一个字符 D。
密文 D 是由明文 A 向后 偏移 333 位得到的,因此偏移量 d=3d = 3d=3。
第二步:解密待解密的密文
对密文串的每个字符,向前 偏移 333 位:
W向前 3 位:W → V → U → TK向前 3 位:K → J → I → HH向前 3 位:H → G → F → E- ......
逐字符处理后得到明文:
text
THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG
与样例输出一致。
2. 暴力写法(直接模拟字符偏移)
由于数据规模极小(字符串长度不超过 10310^3103),最直接的暴力做法是:
- 求出偏移量
d。 - 对于待解密密文的每一个字符,反复向前移动
d次 (每次-1,若小于'A'则跳回'Z')。
暴力代码(逐字符循环移动,仅作对拍基准):
cpp
#include <iostream>
#include <string>
using namespace std;
const int N = 1e3 + 10;
int main()
{
string plain, cipher, target;
cin >> plain >> cipher >> target;
int d = cipher[0] - plain[0];
if (d < 0)
{
d += 26;
}
for (int i = 0; i < (int)target.size(); i++)
{
for (int step = 1; step <= d; step++)
{
target[i]--;
if (target[i] < 'A')
{
target[i] = 'Z';
}
}
}
cout << target << "\n";
return 0;
}
复杂度 :O(∣target∣⋅d)O(|target| \cdot d)O(∣target∣⋅d),最坏 103×25≈2.5×10410^3 \times 25 \approx 2.5 \times 10^4103×25≈2.5×104 次操作,瞬间完成。这份暴力代码本身即可 AC 本题。
3. 特殊性质写法
无
4. 正解推导:模运算一步到位
4.1 核心观察:字母表是模 26 的循环群
大写字母 A ~ Z 可以映射为整数 0∼250 \sim 250∼25。凯撒密码的"向后偏移"与"向前偏移"本质上都是模 26 意义下的加法与减法。
- 加密 (明文 → 密文):C=(P+d) mod 26C = (P + d) \bmod 26C=(P+d)mod26
- 解密 (密文 → 明文):P=(C−d+26) mod 26P = (C - d + 26) \bmod 26P=(C−d+26)mod26
其中 +26+26+26 是为了保证减法结果非负,再对 262626 取模,确保结果落在 0∼250 \sim 250∼25 内。
4.2 偏移量的计算
已知明文字符 p0p_0p0 与密文字符 c0c_0c0,偏移量:
d=(c0−p0+26) mod 26 d = (c_0 - p_0 + 26) \bmod 26 d=(c0−p0+26)mod26
4.3 解密公式(一步完成)
对于待解密密文中的每个字符 ccc:
- 转为数值:
val = c - 'A' - 解密:
val = (val - d + 26) % 26 - 转回字符:
char('A' + val)
4.4 复杂度
- 求偏移量:O(1)O(1)O(1)。
- 遍历待解密密文一次:O(∣target∣)O(|target|)O(∣target∣)。
- 总时间 O(∣target∣)O(|target|)O(∣target∣),空间 O(1)O(1)O(1)(原地修改)。
5. 样例模拟(正解视角)
plain[0] = 'A'(数值 0),cipher[0] = 'D'(数值 3)。
d=(3−0+26) mod 26=3 d = (3 - 0 + 26) \bmod 26 = 3 d=(3−0+26)mod26=3
待解密密文首字符 W(数值 22):
P=(22−3+26) mod 26=45 mod 26=19⇒'T' P = (22 - 3 + 26) \bmod 26 = 45 \bmod 26 = 19 \quad \Rightarrow \quad \text{'T'} P=(22−3+26)mod26=45mod26=19⇒'T'
第二字符 K(数值 10):
P=(10−3+26) mod 26=33 mod 26=7⇒'H' P = (10 - 3 + 26) \bmod 26 = 33 \bmod 26 = 7 \quad \Rightarrow \quad \text{'H'} P=(10−3+26)mod26=33mod26=7⇒'H'
依此类推,最终得到完整明文 THEQUICKBROWNFOXJUMPSOVERTHELAZYDOG,与样例一致。
6. 正解代码实现
cpp
#include <iostream>
#include <string>
using namespace std;
// 使用科学计数法定义常量
const int N = 1e3 + 10;
int main()
{
string plain, cipher, target;
cin >> plain >> cipher >> target;
// 1. 计算偏移量
int d = (cipher[0] - plain[0] + 26) % 26;
// 2. 逐字符解密(原地修改)
for (int i = 0; i < (int)target.size(); i++)
{
int val = target[i] - 'A';
val = (val - d + 26) % 26;
target[i] = char('A' + val);
}
cout << target << "\n";
return 0;
}
说明:
- 偏移量计算使用
(cipher[0] - plain[0] + 26) % 26,自动处理"密文由明文向前偏移"导致的负差值。 - 解密时同样使用
+ 26防止负数,再% 26保证结果在 0∼250 \sim 250∼25 内。
7. 对拍验证
7.1 数据生成器
cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main()
{
srand(time(0));
int len1 = rand() % 10 + 1;
int len2 = rand() % 10 + 1;
string plain = "", cipher = "";
for (int i = 0; i < len1; i++)
{
plain += (char)('A' + rand() % 26);
}
int d = rand() % 26;
for (int i = 0; i < len1; i++)
{
int val = (plain[i] - 'A' + d) % 26;
cipher += (char)('A' + val);
}
cout << plain << "\n" << cipher << "\n";
for (int i = 0; i < len2; i++)
{
cout << (char)('A' + rand() % 26);
}
cout << "\n";
return 0;
}
7.2 对拍结果
生成 2000 组随机数据(长度 1∼101 \sim 101∼10,偏移量 0∼250 \sim 250∼25),分别运行正解(模运算)与暴力(逐字符循环移动),输出完全一致。正解正确性得到验证。
8. 出题人目的与考点分析
出题目的 :作为 GESP 三级题目,本题考察字符串处理 与字符的 ASCII 码算术运算。题目以凯撒密码为背景,要求选手从一对已知明密文中提取偏移量,再用模运算完成批量字符映射。
核心考点:
- ASCII 码运算 :字符可直接参与整数运算,
'D' - 'A' = 3。 - 模运算的循环性:字母表长度 26,偏移与还原均需对 26 取模。
- 负数取模的处理 :
(x + 26) % 26是处理"向前偏移"的经典技巧。 - 字符串遍历与原地修改:一次线性扫描即可完成解密。
背后的逻辑 :本题是 GESP 三级中"字符串 + 字符运算 "的典型入门题。出题人希望选手理解"字符在底层就是整数",并掌握模运算在循环映射中的应用。数据范围极小,明确指向 O(∣S∣)O(|S|)O(∣S∣) 的逐字符处理,不需要任何算法优化。
9. 总结与评分
9.1 思路流程
- 读入已知明文
plain、已知密文cipher、待解密密文target。 - 计算偏移量:
d = (cipher[0] - plain[0] + 26) % 26。 - 遍历
target的每个字符:val = (target[i] - 'A' - d + 26) % 26target[i] = 'A' + val
- 输出
target。
9.2 类似题目推进
- 洛谷 P1914 小书童------凯撒密码:给定偏移量,直接加密,是本题的"正向"版本。
- LeetCode 848. 字母移位:多次字符偏移的累积,同样依赖模运算与字符的整数化处理。