Kickstart Round C 2017 Problem A. Ambiguous Cipher

Problem

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, B = 1, ..., Z = 25.
For every letter in the word, Calvin determines the encrypted value of the letter by summing the values of the 1 or 2 letter(s) that are adjacent to that letter in the word. He takes that sum modulo 26, 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, O = 14, U = 20, and P = 15.
Calvin encrypts each letter based on the values of its neighbor(s):
First letter: 14 mod 26 = 14.
Second letter: (18 + 20) mod 26 = 12.
Third letter: (14 + 15) mod 26 = 3.
Fourth letter: 20 mod 26 = 20.
The values 14 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!

Input

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

Output

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

Limits

1 ≤ T ≤ 100.
W consists of only of uppercase English letters.
W is decryptable to one or more words. (That is, W is the result of an encryption of some word.)
W does not decrypt to the word AMBIGUOUS. (You will only output that when the decryption is ambiguous.)
Small dataset

2 ≤ the length of W ≤ 4.
Large dataset

2 ≤ the length of W ≤ 50.
Sample

Input
3
OMDU
BCB
AOAAAN

Output
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.

分析

通过相邻的字母相加并取余26,得到新的字母。先得出前后两个字母,然后根据其和是否大于其中一个数,来得出另一个数。最后要判断是否能得到结果,如果不能,说明是对称的,出现多个反加密字母,需要舍去。
大小测试数据集都通过:

#include "stdio.h"
int main()
{
    int n,i=0,j=0;
    FILE *fp1, *fp2;
    fp1 = fopen("A-large-practice.in","r");
    fp2 = fopen("A-large-practice.out","w");
    fscanf(fp1,"%d",&n);
    for(i=1;i<=n;i++)
    {
        char a[100];
        char b[100]={0};
        fscanf(fp1,"%s",a);
        int alength=0;
        while(a[alength]!='\0')
            alength++;
        b[1]=a[0];
        b[alength-2]=a[alength-1];
        b[alength]='\0';

        for(j=2;j<alength-1;j=j+2)
        {
            if(a[j]<b[j-1])
            {
                b[j+1]=a[j]+26-b[j-1]+'A';
            }
            else
            {
                b[j+1]=a[j]-b[j-1]+'A';
            }
        }
        for(j=alength-3;j>0;j=j-2)
        {
            if(a[j]<b[j+1])
                b[j-1]=a[j]+26-b[j+1]+'A';
            else
                b[j-1]=a[j]-b[j+1]+'A';
        }
        if(b[0]=='\0')
            fprintf(fp2,"Case #%d: AMBIGUOUS\n",i);
        else
            fprintf(fp2,"Case #%d: %s\n",i,b);
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容