Polybius

Time limit: 0.1s Memory limit: 4MB Input: polybius.in Output: polybius.out
Warning: This statement was translated by an LLM (like ChatGPT). Results might not be fully accurate or faithful to the original statement. Please report any potential errors on our Discord.

Polybius

The Polybius Cipher is a cipher used by the Ancient Greeks, based on a 55 row and 55 column table, to encrypt or decrypt a string of characters. For encryption, we take each character from the string and display the row and column in the table where the character is located. For decryption, we display the character that is located in the table at the given row and column.

For example, using the table:

# 1 2 3 4 5
1 A B C D E
2 F G H I K
3 L M N O P
4 Q R S T U
5 V W X Y Z

Along with the string INF, we display 243321243321 because the letter I is located at position (2,4)(2, 4), the letter N is located at position (3,3)(3, 3) and the letter F is located at position (2,1)(2, 1).

Task

You are given a number pp, a string of 2525 characters tt, corresponding to the table and a string of characters ss. You must determine:

  1. For p=1p = 1, encrypt the string ss, consisting only of uppercase letters, using the tt table.
  2. For p=2p = 2, decrypt the string ss, consisting only of digits, using the tt table.

Input data

The input file polybius.in contains on the first line the number pp, on the second line the string tt consisting of 2525 characters, representing all the elements of the table, from left to right and from top to bottom. The third line contains the string of letters ss that need to be encrypted, if p=1p=1, respectively the string of digits that need to be decrypted, if p=2p=2.

Output data

  • For p=1p = 1, the output file polybius.out will contain on the first line the result of encryption. The digits are displayed without any spaces between them.
  • For p=2p = 2, the output file polybius.out will contain on the first line the result of decryption.

Constraints and clarifications

  • 1p21 \leq p \leq 2;
  • the string tt has a length of 2525 characters;
  • 1length of the letter string s500 0001 \leq \text{length of the letter string s} \leq 500 \ 000;
  • It is guaranteed that all characters from ss are included in the string tt and the characters from tt are pairwise distinct.
  • There will always be a letter missing from the table, as the table is 5x5 and the alphabet has 2626 characters.

Example 1

polybius.in

1
ABCDEFGHIKLMNOPQRSTUVWXYZ
ANAAREMERE

polybius.out

11331111421532154215

Example 2

polybius.in

2
ABCDEFGHIKLMNOPQRSTUVWXYZ
11331111421532154215

polybius.out

ANAAREMERE

Explanation

The table corresponding to the string in the examples is:

# 1 2 3 4 5
1 A B C D E
2 F G H I K
3 L M N O P
4 Q R S T U
5 V W X Y Z

Log in or sign up to be able to send submissions!