Decode It


General Statement: Read a letter sequence that was used to encode a secret message.
The first letter replaced each Z, the second letter replaced each Y, and so on.
Then read the encoded message and decode it.

Input: The first line of the data set for this problem is an integer that represents the number of data sets that follow. Each data set consists of 2 lines.
Line #1 is the encoding list, and line #2 is the secret message.

Output: All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: All letters are upper case.
The maximum string length is 80.
Any non-letter characters were not encoded.

Sample Input:
3
NMPOLKRQJITSHGVUFEXWDCZYBA
GCCR GC EH RXC WMG
NMOPQRLKJISTUVHGFEWXYZDCBA
PWT’K AYJAED TY NZBJAK?
MNLOKPJQIRHSGTFUEVDWCXBYAZ
1313 TSYUVGWAVJB FZGX

Sample Output:
MEET ME IN THE GYM
WHO’S AFRAID OF ZEBRAS?
1313 MOCKINGBIRD LANE

Solutions :

#include <cstdio>
#include <map>
#include <cctype>
using namespace std;

int main ()
{
    int dataset;
    scanf ("%d", &dataset);
    getchar ();

    while ( dataset-- ) {
        char str [82];
        gets (str);

        map <char, char> m;
        m.clear ();

        for ( int i = 0; i < 26; i++ )
            m [str [i]] = 90 - i;

        gets (str);
        for ( int i = 0; str [i] != 0; i++ ) {
            if ( isalpha (str [i]) )
                printf ("%c", m [str [i]]);
            else
                printf ("%c", str [i]);
        }
        printf ("\n");
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s