#P1071. Decrypting S Country's Military Code

    ID: 12741 Type: Default 1000ms 256MiB

Decrypting S Country's Military Code

Decrypting S Country's Military Code

S country uses a substitution cipher to encode its military messages. Each uppercase letter (A to Z) in the original message is uniquely replaced by another uppercase letter as its cipher equivalent. That is, if letter x maps to letter y, then no other letter maps to y. The encryption rules have been discovered from an intercepted pair of messages: an encrypted message and its corresponding plaintext.

The decryption process is as follows:

  1. Scan the plaintext message. For each letter \(x\) (any letter from A to Z) in the plaintext at position \(i\), note the corresponding uppercase letter \(y\) at the same position in the encrypted sample. This gives the mapping: \(x \rightarrow y\).
  2. If a letter appears multiple times, the corresponding mapping must be consistent; otherwise, the encryption rule has been violated.
  3. After scanning, if the mapping does not include all 26 letters (i.e. some letters never appear in the plaintext), it is considered incomplete.

If an inconsistency is detected or the mapping is incomplete, the decryption is considered to have failed, and you should output FAILED. Otherwise, use the inverse mapping to decrypt a newly intercepted encrypted message.

Input/Output Format

The substitution cipher mapping is extracted from the first two input lines:

  • Line 1: The encrypted sample message.
  • Line 2: The corresponding plaintext message.

The third input line is the new intercepted encrypted message to be decoded. If the sample data produces a complete and consistent mapping (i.e. covers all 26 uppercase letters with a one-to-one mapping), decrypt the intercepted message using the inverse mapping. Otherwise, output FAILED.

Note: All messages consist solely of uppercase letters A to Z without spaces or other characters. Any formulas must be in LaTeX format (for example, \(A \rightarrow Q\)).

inputFormat

The input consists of three lines:

  1. The first line is the encrypted sample message.
  2. The second line is the corresponding plaintext message.
  3. The third line is the intercepted encrypted message that needs to be decrypted.

outputFormat

If the cipher decryption mapping is complete (i.e. all 26 letters are mapped) and consistent, output the decrypted message. Otherwise, output FAILED (without quotes).

sample

QWERTYUIOPASDFGHJKLZXCVBNM
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ITSSG
HELLO