POJ 2041 Unreliable Message

来源:互联网 发布:用python做数据分析 编辑:程序博客网 时间:2024/06/04 19:34

Unreliable Message

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. 


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

5AJMPaB23dE86AEAM6JPEMWaEaETC302QCPrTurnAGundam1isdefferentf

Sample Output

32BadAE867EC302QTWaEaTurnAGundam0isdefferentfr
题目大意:给出一个各种变换操作后的字符串,求原字符串。其中操作包括以下类型:

J:将字符串的各位左移一位

C:将字符串的各位右移一位

E:交换字符串的左半部分与右半部分,如果字符串的长度为奇数,则中间的字符不处理

A:逆置字符串

P:将字符串中的数字加一,特殊情况‘9’变为‘0’

M:将字符串中的数字减一,特殊情况‘0’变为‘9’

解题思路:模拟即可,注意给出的是变换后的字符串,要求出的是原来的字符串,所以要按原来操作的逆操作写,而且操作执行的顺序也是自后向前。

代码如下:

#include <cstdio>#include <cstring>#include <cstdlib>void swap(char *a,char *b){char temp = *a;*a = *b;*b = temp;}char *operate(char *s,char opt){int len = strlen(s);switch(opt){case 'J' :for(int i = len;i > 0;i--){s[i] = s[i - 1];}s[0] = s[len];s[len] = '\0';break;case 'C' :s[len] = s[0];for(int i = 1;i <= len;i++){s[i - 1] = s[i];}s[len] = '\0';break;case 'E' :for(int i = (len - 1 - len % 2) / 2,j = len - 1;i >= 0 && j >= len / 2;i--,j--){swap(&s[i],&s[j]);}break;case 'A' :for(int i = 0,j = len - 1;i <= j;i++,j--){swap(&s[i],&s[j]);}break;case 'P' :for(int i = 0;i < len;i++){if('0' < s[i] && s[i] <= '9'){s[i]--;}else if(s[i] == '0')s[i] = '9';}break;case 'M' :for(int i = 0;i < len;i++){if('0' <= s[i] && s[i] < '9'){s[i]++;}else if(s[i] == '9')s[i] = '0';}break;}return s; }  int main() { char opt[7]; char *str; str = (char *)malloc(30 * sizeof(char)); int n,l; while(scanf("%d",&n) != EOF){ for(int i = 0;i < n;i++){ scanf("%s",opt); scanf("%s",str); l = strlen(opt); for(int j = l - 1;j >= 0;j--){ str = operate(str,opt[j]);}opt[0] = '\0';printf("%s\n",str); } }return 0;  }


0 0
原创粉丝点击