#P2636. 密码破解者

密码破解者

Description

According to intelligence, the Japanese use 33 kinds of encryption methods:

  1. Rail Fence Cipher:

The so‑called rail fence cipher divides the plaintext into groups of LL characters, then concatenates the first character of each group, followed by the second character of each group, and so on, forming a seemingly irregular string. The most common case is the 22‑rail rail fence cipher.

For example, plaintext: THERE IS A CIPHER
After removing spaces: THEREISACIPHER
Group by two: TH ER EI SA CI PH ER
Take the first character of each group: TEESCPE
Then the second character of each group: HRIAIHR
Concatenate them: TEESCPEHRIAIHR.

This yields the ciphertext we need.

There may also be more rails.

Note: If the plaintext length is not divisible by the number of rails, the leftover characters form a shorter group on their own. For example:

For THERE IS A CIPHER, the 33‑rail grouping is: THE REI SAC IPH ER.

Take the first, second, and third characters in order (the last group only has the first two). The encrypted text is: TRSIE HEAPR EICH (remove spaces).

  1. Vigenère Cipher:

The Vigenère cipher first introduced the idea of a “key,” which is of great significance in cryptography.

In cryptography, the information to be encrypted is called the plaintext, denoted by MM; the encrypted information is called the ciphertext, denoted by CC; and the key is a parameter, i.e., the data input to the algorithm that transforms plaintext into ciphertext or ciphertext into plaintext, denoted by kk. In the Vigenère cipher, the key kk is a string of letters, k=k1k2knk = k_1 k_2 \cdots k_n. When the plaintext is M=m1m2mnM = m_1 m_2 \cdots m_n, the resulting ciphertext is C=c1c2cnC = c_1 c_2 \cdots c_n, where ci=mikic_i = m_i \oplus k_i. The operation \oplus follows the table below:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

A -A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

B -B C D E F G H I J K L M N O P Q R S T U V W X Y Z A

C -C D E F G H I J K L M N O P Q R S T U V W X Y Z A B

D -D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

E -E F G H I J K L M N O P Q R S T U V W X Y Z A B C D

F -F G H I J K L M N O P Q R S T U V W X Y Z A B C D E

G -G H I J K L M N O P Q R S T U V W X Y Z A B C D E F

H -H I J K L M N O P Q R S T U V W X Y Z A B C D E F G

I -I J K L M N O P Q R S T U V W X Y Z A B C D E F G H

J -J K L M N O P Q R S T U V W X Y Z A B C D E F G H I

K -K L M N O P Q R S T U V W X Y Z A B C D E F G H I J

L -L M N O P Q R S T U V W X Y Z A B C D E F G H I J K

M -M N O P Q R S T U V W X Y Z A B C D E F G H I J K L

N -N O P Q R S T U V W X Y Z A B C D E F G H I J K L M

O -O P Q R S T U V W X Y Z A B C D E F G H I J K L M N

P -P Q R S T U V W X Y Z A B C D E F G H I J K L M N O

Q -Q R S T U V W X Y Z A B C D E F G H I J K L M N O P

R -R S T U V W X Y Z A B C D E F G H I J K L M N O P Q

S -S T U V W X Y Z A B C D E F G H I J K L M N O P Q R

T -T U V W X Y Z A B C D E F G H I J K L M N O P Q R S

U -U V W X Y Z A B C D E F G H I J K L M N O P Q R S T

V -V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

W -W X Y Z A B C D E F G H I J K L M N O P Q R S T U V

X -X Y Z A B C D E F G H I J K L M N O P Q R S T U V W

Y -Y Z A B C D E F G H I J K L M N O P Q R S T U V W X

Z -Z A B C D E F G H I J K L M N O P Q R S T U V W X Y

Notes for operating Vigenère encryption:

When the length of plaintext MM is greater than the length of key kk, repeat the key kk cyclically.

For example, plaintext M=HelloworldM = Helloworld, key k=abck = abc, ciphertext C=HfnlpyosndC = Hfnlpyosnd.

  1. QWE Keyboard Code:

With the popularization of keyboards, corresponding keyboard codes also appeared.

This is a common keyboard. On the left letter area, there are three rows of letters:

QWERTYUIOP

ASDFGHJKL

ZXCVBNM

Starting from the first row and the first column, replace A with Q, B with W, …, Z with M, and so on.

For example, CODING is encrypted as EGROFU.

This is a simple encryption method today, but during World War II, when keyboards were not widespread, it was a big challenge.

Input Format

  • The first line contains a positive integer NN, the number of encryption layers applied to the intercepted ciphertext.
  • The second line contains a string SS, the ciphertext after encryption.
  • The next NN lines (i.e., lines 33 to N+2N+2) each begin with a positive integer KK with 1K31 \le K \le 3, indicating the encryption method used for that layer.
  • The methods are listed in order: the II‑th given method is the actual II‑th encryption layer in the process, where 1IN1 \le I \le N.
  • If K=1K = 1, the rail fence cipher was used; an integer LL follows, the number of rails used for encryption.
  • If K=2K = 2, the Vigenère cipher was used; a string TT follows, which is the key.
  • If K=3K = 3, the QWE keyboard code was used.

Output Format

Output one line: a string representing the plaintext after decrypting all NN layers of encryption.

2
YSLTRIQXSHTQTR
1 2
3
FULLSPEEDAHEAD

Hint

N1000N \le 1000.

Translated by ChatGPT 5