QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 2048 MB Puntuación total: 100

#9759. 跳蛙加密

Estadísticas

我们提出了一种新的加密方法,称为“跳蛙加密”(Leapfrog Encryption)。这是一种基于密钥的加密方案,其中字母密钥规定了明文(待加密文本)中的字母如何放置在密文(加密后的字符串)中。以下是跳蛙加密的工作原理:

  1. 从明文中删除所有非字母字符,并将所有剩余字母转换为小写。
  2. 将密钥中的字母转换为它们在字母表中的位置 $+1$(即 'a' 转换为 2,'b' 转换为 3,依此类推)。这产生了一个数字序列 $d_1, d_2, \dots, d_n$,其中 $n$ 是密钥的长度。
  3. 从左到右,将明文中的前几个字母放置在密文的每一个第 $d_1$ 个位置,直到密文中没有位置可用(密文的长度等于明文中字母的数量)。例如,如果 $d_1 = 5$,则明文中的第一个字母放在密文的第 5 个位置(密文的第一个位置编号为 1),明文中的第二个字母放在密文的第 10 个位置,依此类推。
  4. 对 $d_2$ 重复此过程,但这次从右到左遍历密文,仅计数空位置(跳过密文中已有的字母)。
  5. 继续使用 $d_3, d_4$ 等,每次改变遍历密文的方向。
  6. 如果在使用 $d_n$ 后明文中仍有剩余字母,则将这些剩余字母填入密文中剩余的空位置,再次沿与前一次遍历相反的方向进行(这等同于 $d_{n+1} = 1$)。

例如,如果我们的明文是 “Send more monkeys!”,密钥是 “bea”,加密过程如下:

b → 3,从左到右:_ _ s _ _ e _ _ n _ _ d _ _ m e → 6,从右到左:_ _ s _ _ e o _ n _ _ d _ _ m a → 2,从左到右:_ r s _ e e o _ n m _ d o _ m 最后一次,从右到左:s r s y e e o e n m k d o n m

解密过程由……嘿,你知道吗?我们打算让你自己去弄清楚。

输入格式

输入的第一行包含两个字符串 $t$ 和 $k$,其中 $t$ 为 'E' 或 'D',表示执行加密还是解密,$k$ 是小写字母密钥。$k$ 的长度在 1 到 100 之间(含边界)。输入的第二行(也是最后一行)包含要加密的明文(如果 $t$ 为 'E')或要解密的密文(如果 $t$ 为 'D')。该字符串非空,最大长度为 2000。密文字符串仅由小写字母组成,而明文字符串可能包含大写字母、数字、标点符号和空格(所有这些都计入字符串长度),并保证至少包含一个字母。

输出格式

输出加密或解密后的文本。你的输出应仅包含小写字母。

样例

样例输入 1

E bea
Send more monkeys!

样例输出 1

srsyeeoenmkdonm

样例输入 2

D bea
srsyeeoenmkdonm

样例输出 2

sendmoremonkeys

样例输入 3

D zyxwvutsrqponmlkjihgfedcba
lafogrpe

样例输出 3

leapfrog

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.