POJ - 2041Unreliable Message

来源:互联网 发布:零存整取算法 编辑:程序博客网 时间:2024/05/21 08:04

这里的算法非常简单,就是“模拟”,注意编写每个传令官的算法时先分开测试,放在一起就会混淆。

POJ - 2041Unreliable Message

Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u

Description

The King of a little Kingdom on a little island in the Pacific Ocean frequently has childish ideas. One day he said, "You shall make use of a message relaying game when you inform me of something." In response to the King's statement, six servants were selected as messengers whose names were Mr. J, Miss C, Mr. E, Mr. A, Dr. P, and Mr. M. They had to relay a message to the next messenger until the message got to the King. Messages addressed to the King consist of digits ('0'-'9') and alphabet characters ('a'-'z', 'A'-'Z'). Capital and small letters are distinguished in messages. For example, "ke3E9Aa" is a message. Contrary to King's expectations, he always received wrong messages, because each messenger changed messages a bit before passing them to the next messenger. Since it irritated the King, he told you who are the Minister of the Science and Technology Agency of the Kingdom, "We don't want such a wrong message any more. You shall develop software to correct it!" In response to the King's new statement, you analyzed the messengers' mistakes with all technologies in the Kingdom, and acquired the following features of mistakes of each messenger. A surprising point was that each messenger made the same mistake whenever relaying a message. The following facts were observed. Mr. J rotates all characters of the message to the left by one. For example, he transforms "aB23d" to "B23da". Miss C rotates all characters of the message to the right by one. For example, she transforms "aB23d" to "daB23". Mr. E swaps the left half of the message with the right half. If the message has an odd number of characters, the middle one does not move. For example, he transforms "e3ac" to "ace3", and "aB23d" to "3d2aB". Mr. A reverses the message. For example, he transforms "aB23d" to "d32Ba". Dr. P increments by one all the digits in the message. If a digit is '9', it becomes '0'. The alphabet characters do not change. For example, he transforms "aB23d" to "aB34d", and "e9ac" to "e0ac". Mr. M decrements by one all the digits in the message. If a digit is '0', it becomes '9'. The alphabet characters do not change. For example, he transforms "aB23d" to "aB12d", and "e0ac" to "e9ac". The software you must develop is to infer the original message from the final message, given the order of the messengers. For example, if the order of the messengers is A->J->M->P and the message given to the King is "aB23d", what is the original message? According to the features of the messengers' mistakes, the sequence leading to the final message is "32Bad"->"daB23"->"aB23d"->"aB12d"->"aB23d": As a result, the original message should be "32Bad".

Input

The input format is as follows. n The order of messengers The message given to the King ... The order of messengers The message given to the King The first line of the input contains a positive integer n, which denotes the number of data sets. Each data set is a pair of the order of messengers and the message given to the King. The number of messengers relaying a message is between 1 and 6 inclusive. The same person may not appear more than once in the order of messengers. The length of a message is between 1 and 25 inclusive.

Output

The inferred messages are printed each on a separate line.

Sample Input

5

AJMP

aB23d

E

86AE

AM

6

JPEM

WaEaETC302Q

CP

rTurnAGundam1isdefferentf

Sample Output

32Bad

AE86

7

EC302QTWaEa

TurnAGundam0isdefferent

Source Japan 2003,Aizu

 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<stdlib.h> 5  6 int main() 7 { 8     int n = 0; 9     scanf("%d", &n);10     char str[10], order[30];11     for(int i = 0; i < n; i++) {12         scanf("%s", str);13         scanf("%s", order);14         int r = strlen(str);15         int len = strlen(order);16         char ch;17         int t;18         for(int i = r-1; i >= 0; i--) {19             switch(str[i]){20                 printf("str -- %c\n", str[i]);21                 case 'J':22                     ch = order[len-1];23                     for(int j = len-1; j > 0; j--) {24                         order[j] = order[j-1];25                     }26                     order[0] = ch;27                     break;28                 case 'C':29                     ch = order[0];30                     for(int j = 0; j < len-1; j++) {31                         order[j] = order[j+1];32                     }33                     order[len-1] = ch;34                     break;35                 case 'E':36                     t = len/2;37                     for(int i = 0; i < t; i++) {38                         ch = order[i];39                         if(len%2) {40                             order[i] = order[i+t+1];41                             order[i+t+1] = ch;42                         }43                         else {44                             order[i] = order[i+t];45                             order[i+t] = ch;46                         }47                     }48                     break;49                 case 'A':50                     t = len/2;51                     for(int i = 0; i < t; i++) {52                         ch = order[i];53                         order[i] = order[len-1-i];54                         order[len-1-i] = ch;55                     }56                     break;57                 case 'P':58                     for(int i = 0; i < len; i++) {59                         if(order[i] >= '0' && order[i] <= '9') {60                             if(order[i] == '0') order[i] = '9';61                             else order[i]--;62                         }63                     }64                     break;65                 case 'M':66                     for(int i = 0; i < len; i++) {67                         if(order[i] >= '0' && order[i] <= '9') {68                             if(order[i] == '9') order[i] = '0';69                             else order[i]++;70                         }71                     }72                     break;73             }74         }75         printf("%s\n", order);76     }77     return 0;78 }

 

原创粉丝点击