#P16620. [GKS 2017 #C] Ambiguous Cipher

    ID: 16575 Type: RemoteJudge 1000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>递推2017Google Kick Start

[GKS 2017 #C] Ambiguous Cipher

题目描述

Susie and Calvin are classmates. Calvin would like to be able to pass notes to Susie in class without their teacher or other classmates knowing what they are talking about, just in case the notes fall into the wrong hands. Calvin has devised the a system to encrypt his messages.

Calvin only passes one word to Susie each time, and that word consists of only uppercase letters, because Calvin is so excited to talk to Susie. Each word is encrypted as follows:

  • Calvin assigns a number to each letter based on the letter's position in the alphabet, where A=0\text{A} = 0, B=1\text{B} = 1, ..., Z=25\text{Z} = 25.
  • For every letter in the word, Calvin determines the encrypted value of the letter by summing the values of the 11 or 22 letter(s) that are adjacent to that letter in the word. He takes that sum modulo 2626, and this is the new value of the letter. Calvin then converts the value back to an uppercase letter based on positions in the alphabet, as before.
  • The encrypted word is determined by encrypting every letter in the word using this method. Each letter's encryption is based only on the letters from the original unencrypted message, and not on any letters that have already been encrypted

Let's take a look at one of the notes Calvin is writing for Susie. Since Calvin is always hungry, he wants to let Susie know that he wants to eat again. Calvin encrypts the word SOUP as follows:

  • S=18\text{S} = 18, O=14\text{O} = 14, U=20\text{U} = 20, and P=15\text{P} = 15.
  • Calvin encrypts each letter based on the values of its neighbor(s):
    • First letter: 14mod26=1414 \bmod 26 = 14.
    • Second letter: (18+20)mod26=12(18 + 20) \bmod 26 = 12.
    • Third letter: (14+15)mod26=3(14 + 15) \bmod 26 = 3.
    • Fourth letter: 20mod26=2020 \bmod 26 = 20.
  • The values 14 12 3 2014\ 12\ 3\ 20 correspond to the letters OMDU, and this is the encrypted word that Calvin will write on the note for Susie.

It is guaranteed that Calvin will not send Susie any words that cannot be decrypted at all. For example, Calvin would not send Susie the word APE, since it does not have any valid decryptions. (That is, there is no word that Calvin could have encrypted to APE.)

However, Calvin's system is not perfect, and some of the words he sends Susie can actually be decrypted to multiple words, creating ambiguity! For example, BCB can be decrypted to ABC or CBA, among other possibilities.

Susie pulled another all-nighter yesterday to finish school projects, and she is too tired to decrypt Calvin's messages. She needs your help!

输入格式

The first line of the input gives the number of test cases, TT. TT test cases follow. Each case is a single line that contains a string WW of uppercase letters: an encrypted word that Calvin has sent.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the decrypted word, or AMBIGUOUS if it is impossible to uniquely determine the decrypted word.

3
OMDU
BCB
AOAAAN
Case #1: SOUP
Case #2: AMBIGUOUS
Case #3: BANANA

提示

Note that the last sample case would not appear in the Small dataset.

Sample Cases #1 & #2 were explained in the problem statement.

In Sample Case #3, BANANA is the only word that encrypts to AOAAAN.

Limits

1T1001 \le T \le 100.

WW consists of only of uppercase English letters.

WW is decryptable to one or more words. (That is, WW is the result of an encryption of some word.)

WW does not decrypt to the word AMBIGUOUS. (You will only output that when the decryption is ambiguous.)

Small dataset (Test set 1 - Visible)

22 \le the length of WW 4\le 4.

Large dataset (Test set 2 - Hidden)

22 \le the length of WW 50\le 50.