#P1071. [NOIP 2009 提高组] 潜伏者

[NOIP 2009 提高组] 潜伏者

Description

Countries R and S are at war and have sent spies into each other’s territory. After many hardships, Xiao C (pinyin) from country R, who is undercover in country S, finally figured out the encoding rules of country S’s military cipher:

  1. The plaintext to be sent by country S’s military is encrypted and then sent over the network. Both the plaintext and the ciphertext consist only of uppercase letters AZ\texttt{A}\sim\texttt{Z} (no spaces or other characters).
  2. For each letter, country S specifies a corresponding cipher letter. Encryption replaces every letter in the plaintext with its corresponding cipher letter.
  3. Each letter corresponds to exactly one unique cipher letter, and different letters correspond to different cipher letters. A letter may map to itself.

For example, if the cipher letter for A\tt A is A\tt A, and for B\tt B is C\tt C (others omitted), then the plaintext ABA\tt ABA is encrypted as ACA\tt ACA.

Now Xiao C has, via an inside source, one encrypted message together with its corresponding plaintext. He wants to break the cipher based on this information. His process is: scan the plaintext; for a plaintext letter xx (any uppercase letter), find the corresponding letter yy at the same position in the ciphertext, and conclude that in the cipher, yy is the cipher letter for xx. Continue until one of the following states occurs:

  1. All information has been scanned, and all 2626 letters AZ\texttt{A}\sim\texttt{Z} have appeared in the plaintext and obtained their corresponding cipher letters.
  2. All information has been scanned, but there exists at least one letter that does not appear in the plaintext.
  3. During scanning, an obvious contradiction or error is found (violating the encoding rules of country S’s cipher).

Example:

If some plaintext XYZ\tt XYZ is translated as ABA\tt ABA, it violates the rule “different letters correspond to different cipher letters.”

While Xiao C is overwhelmed, the headquarters of country R sends another telegram asking him to translate a newly intercepted encrypted message from country S. Please help Xiao C: deduce the cipher from the known pair, and then use the deduced cipher to translate the encrypted message in the telegram.

Input Format

There are three lines. Each line is a string with length between 11 and 100100.

  • The first line is an encrypted message known to Xiao C.
  • The second line is the plaintext corresponding to the first line.
  • The third line is the encrypted message that country R’s headquarters asks Xiao C to translate.

The testdata guarantees that all strings consist only of uppercase letters AZ\texttt{A}\sim\texttt{Z}, and that the lengths of the first and second lines are equal.

Output Format

Output one line.

If the cipher deduction stops in cases 22 or 33, output Failed\tt Failed. Otherwise, output the plaintext obtained by translating the encrypted message using the deduced cipher.

AA 
AB 
EOWIE


Failed

QWERTYUIOPLKJHGFDSAZXCVBN 
ABCDEFGHIJKLMNOPQRSTUVWXY 
DSLIEWO

Failed 

MSRTZCJKPFLQYVAWBINXUEDGHOOILSMIJFRCOPPQCEUNYDUMPP
YIZSDWAHLNOVFUCERKJXQMGTBPPKOIYKANZWPLLVWMQJFGQYLL
FLSO 
NOIP

Hint

  • Explanation for Sample 1: Letters A\tt A and B\tt B in the plaintext correspond to the same cipher letter, so output Failed\tt Failed.
  • Explanation for Sample 2: Letter Z\tt Z does not appear in the plaintext, so output Failed\tt Failed.

NOIP 2009 Senior Problem 1.

Translated by ChatGPT 5