ZOJ-1737 Unreliable Message

Unreliable Message


Time Limit: 2 Seconds      MemoryLimit: 65536 KB


TheKing of a little Kingdom on a little island in the Pacific Ocean frequently haschildish ideas. One day he said, "You shall make use of a message relayinggame when you inform me of something." In response to the King'sstatement, 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 nextmessenger until the message got to the King.

Messages addressedto 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'sexpectations, he always received wrong messages, because each messenger changedmessages a bit before passing them to the next messenger. Since it irritatedthe King, he told you who are the Minister of the Science and Technology Agencyof the Kingdom, "We don't want such a wrong message any more. You shalldevelop software to correct it!" In response to the King's new statement,you analyzed the messengers' mistakes with all technologies in the Kingdom, andacquired the following features of mistakes of each messenger. A surprisingpoint was that each messenger made the same mistake whenever relaying amessage. The following facts were observed.

Mr. J rotates allcharacters of the message to the left by one. For example, he transforms"aB23d" to "B23da".

Miss C rotates allcharacters of the message to the right by one. For example, she transforms"aB23d" to "daB23".

Mr. E swaps theleft half of the message with the right half. If the message has an odd numberof characters, the middle one does not move. For example, he transforms"e3ac" to "ace3", and "aB23d" to"3d2aB".

Mr. A reverses themessage. For example, he transforms "aB23d" to "d32Ba".

Dr. P incrementsby one all the digits in the message. If a digit is '9', it becomes '0'. Thealphabet characters do not change. For example, he transforms "aB23d"to "aB34d", and "e9ac" to "e0ac".

Mr. M decrementsby one all the digits in the message. If a digit is '0', it becomes '9'. Thealphabet characters do not change. For example, he transforms "aB23d"to "aB12d", and "e0ac" to "e9ac".

The software youmust develop is to infer the original message from the final message, given theorder of the messengers. For example, if the order of the messengers isA->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 formatis 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 ofthe 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 theKing. 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. Thelength of a message is between 1 and 25 inclusive.


Output

The inferredmessages 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
TurnAGundam0isdefferentfr


————————————————————————————————————————————————————


#include <stdio.h>
#include <string.h>

void Jprocess(char *str, int len);
void Cprocess(char *str, int len);
void Eprocess(char *str, int len);
void Aprocess(char *str, int len);
void Pprocess(char *str, int len);
void Mprocess(char *str, int len);

int main()
{
	int n, i, j, lenp, lens;
	char str[26], person[7];
	char resstr[26];
	scanf("%d", &n);
	getchar();
	while(n--) {
		gets(person);
		gets(str);
		lenp = strlen(person);
		lens = strlen(str);
		for(i = lenp-1; i >= 0; i--) {
			if(person[i] == 'J') {
				Jprocess(str, lens);
			}
			else if(person[i] == 'C') {
				Cprocess(str, lens);
			}
			else if(person[i] == 'E') {
				Eprocess(str, lens);
			}
			else if(person[i] == 'A') {
				Aprocess(str, lens);
			}
			else if(person[i] == 'P') {
				Pprocess(str, lens);
			} else {
				Mprocess(str, lens);
			}
		}
		printf("%s\n", str);
	}
	return 0;
}

void Jprocess(char *str, int len)
{
	int i;
	char c = str[len-1];
	for(i = len-1; i > 0; i--) {
		str[i] = str[i-1];
	}	
	str[0] = c;
}

void Cprocess(char *str, int len)
{
	int i;
	char c = str[0];
	for(i = 0; i < len-1; i++) {
		str[i] = str[i+1];
	}
	str[len-1] = c;
}

void Eprocess(char *str, int len)
{
	int i, step;
	char c;
	int mid = len / 2;
	step = mid;
	if(len % 2 == 1) {
		step += 1;
	}
	for(i = 0; i < mid; i++) {
		c = str[i];
		str[i] = str[i+step];
		str[i+step] = c;
	}
}

void Aprocess(char *str, int len)
{
	int i, j;
	char c;
	for(i = 0, j = len-1; i < j; i++, j--) {
		c = str[i];
		str[i] = str[j];
		str[j] = c;
	}
}

void Pprocess(char *str, int len)
{
	int i;
	for(i = 0; i < len; i++) {
		if(str[i] >= '0' && str[i] <= '9') {
			if(str[i] == '0') {
				str[i] = '9';
			} else {
				str[i] -= 1;
			}
		}
	}
}

void Mprocess(char *str, int len)
{
	int i;
	for(i = 0; i < len; i++) {
		if(str[i] >= '0' && str[i] <= '9') {
			if(str[i] == '9') {
				str[i] = '0';
			} else {
				str[i] += 1;
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章