8月 05, 2008

【解題】Decode the Mad man

@
ACM Volume CII 10222 - Decode the Mad man


The Problem

Once in BUET, an old professor had gone completely mad. He started talking with some peculiar words. Nobody could realize his speech and lectures. Finally the BUET authority fall in great trouble. There was no way left to keep that man working in university. Suddenly a student (definitely he was a registered author at UVA ACM Chapter and hold a good rank on 24 hour-Online Judge) created a program that was able to decode that professor’s speech. After his invention, everyone got comfort again and that old teacher started his everyday works as before.

So, if you ever visit BUET and see a teacher talking with a microphone, which is connected to a IBM computer equipped with a voice recognition software and students are taking their lecture from the computer screen, don’t get thundered! Because now your job is to write the same program which can decode that mad teacher's speech!


The Input

The input file will contain only one test case i.e. the encoded message.

The test case consists of one or more words.


The Output

For the given test case, print a line containing the decoded words. However, it is not so hard task to replace each letter or punctuation symbol by the two immediately to its left alphabet on your standard keyboard.


Sample Input

k[r dyt I[o


Sample Output

how are you


解題思考

  這一題其實與 10082 是異曲同工。

  找到讀入字元對應的索引值(index)之後,輸出其前兩個索引值的字元就可以了。


參考解答(C++)

#include <iostream>

using namespace std;

// 定義對應表字元陣列
const char word[] = {'`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',
                    'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\',
                    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'',
                    'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/'};

int main(void)
{
    char c;
    while (cin.get(c))
    {
        // 假如是空白或換行就直接輸出
        if (c == ' ')       { cout << c; }
        else if (c == '\n') { cout << endl; }
        else
        {
            // 搜尋輸入字元, 輸出其前兩個字元
            for (int i = 0; i < 47; i++)
            {
                if (word[i] == c)
                {
                    cout << word[i - 2];
                    break;
                }
            }
        }
    }

#ifndef ONLINE_JUDGE
    system("pause");
#endif
}

2 回覆:

kgame 智涵 提到...

用string存這個陣列
使用find()方法可以省下自己寫迴圈
word[word.find(c) - 2]

小參 提到...

說的也是,經過實測用 string::find() 的方式效率也比較高。
感謝你的建議~

張貼留言