Problem:


Problem A: Unreliable Messengers

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

         A           J           M           P
“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

Output for the Sample Input

32Bad
AE86
7
EC302QTWaEa
TurnAGundam0isdefferentfr



Sample input:
5
AJMP
aB23d
E
86AE
AM
6
JPEM
WaEaETC302Q
CP
rTurnAGundam1isdefferentf


Sample output:
32Bad
AE86
7
EC302QTWaEa
TurnAGundam0isdefferentfr

Submission status: Accepted

Solution code:
def main():
    s = input()
    ans = list(input())
    s = reversed(s)
    for si in s:
        if si == "C":
            ans = ans[1:] + [ans[0]]
        if si == "J":
            ans = [ans[-1]] + ans[:-1]
        if si == "E":
            tmp = [ans[len(ans) // 2]] if len(ans) & 1 else []
            ans = ans[len(ans) // 2 + (len(ans) & 1):] + tmp + ans[: len(ans) // 2]
        if si == "A":
            ans = ans[::-1]
        if si == "M":
            for i in range(len(ans)):
                if ans[i].isdecimal():
                    ans[i] = int(ans[i])
                    ans[i] = str((ans[i] + 1) % 10)
        if si == "P":
            for i in range(len(ans)):
                if ans[i].isdecimal():
                    ans[i] = int(ans[i])
                    ans[i] = str((ans[i] - 1) % 10)

    print("".join(ans))



if __name__ == "__main__":
    for _ in range(int(input())):
        main()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    for i in range(n):
        s = input()
        t = S()
        for k in s[::-1]:
            if k == "J":
                t = [t[-1]]+t[:-1]
            elif k == "C":
                t = t[1:]+[t[0]]
            elif k == "E":
                m = len(t) >> 1
                if len(t)&1:
                    t = t[m+1:]+[t[m]]+t[:m]
                else:
                    t = t[m:]+t[:m]
            elif k == "A":
                t = t[::-1]
            elif k == "M":
                for j in range(len(t)):
                    if t[j].isdecimal():
                        t[j] = str(int(t[j])+1)[-1]
            else:
                for j in range(len(t)):
                    if t[j].isdecimal():
                        if t[j] == "0":
                            t[j] = "9"
                        else:
                            t[j] = str(int(t[j])-1)
        print(*t,sep = "")
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
for loop in range(int(raw_input())):
    manu = raw_input()
    msg = raw_input()
    size = len(msg)
    
    for s in manu[::-1]:
        if   s == "J":
            msg = msg[-1]+msg[:-1]
        elif s == "C":
            msg = msg[1:]+msg[0]
        elif s == "E":
            msg = msg[size/2+size%2:]+msg[:size/2]
        elif s == "A":
            msg = msg[::-1]
        elif s == "P":
            for i in range(len(msg)):
                if msg[i].isdigit():
                    msg = msg[:i]+str((int(msg[i])-1+10)%10)+msg[i+1:]
        elif s == "M":
            for i in range(len(msg)):
                if msg[i].isdigit():
                    msg = msg[:i]+str((int(msg[i])+1)%10)+msg[i+1:]
    
    print msg

Submission status: Runtime Error

Solution code:
R = 2**15+1
dp = [[0]*R for i in range(4)]
for i in range(1,182):
	dp[0][i**2] += 1
for k in range(1,4):
	for i in range(1,182):
		for j in range(i**2,R-i**2):
			dp[k][j+i**2] += dp[k-1][j]

while 1:
	n = input()
	if n == 0: break
	print sum(dp[i][n] for i in range(4))