Problem:
 Score : 900 points    Problem Statement Takahashi has a lot of peculiar devices.
These cylindrical devices receive balls from left and right.
Each device is in one of the two states A and B, and for each state, the device operates as follows:   When a device in state A receives a ball from either side (left or right), the device throws out the ball from the same side, then immediately goes into state B.  When a device in state B receives a ball from either side, the device throws out the ball from the other side, then immediately goes into state A.   The transition of the state of a device happens momentarily and always completes before it receives another ball.  Takahashi built a contraption by concatenating N of these devices. In this contraption,   A ball that was thrown out from the right side of the i -th device from the left ( 1 \leq i \leq N-1 ) immediately enters the (i+1) -th device from the left side.  A ball that was thrown out from the left side of the i -th device from the left ( 2 \leq i \leq N ) immediately enters the (i-1) -th device from the right side.   The initial state of the i -th device from the left is represented by the i -th character in a string S .
From this situation, Takahashi performed the following K times: put a ball into the leftmost device from the left side, then wait until the ball comes out of the contraption from either end.
Here, it can be proved that the ball always comes out of the contraption after a finite time.
Find the state of each device after K balls are processed.      Constraints  1 \leq N \leq 200,000  1 \leq K \leq 10^9  |S|=N  Each character in S is either A or B .         Input The input is given from Standard Input in the following format:  N  K  S       Output Print a string that represents the state of each device after K balls are processed.
The string must be N characters long, and the i -th character must correspond to the state of the i -th device from the left.        Sample Input 1 5 1
ABAAA      Sample Output 1 BBAAA  In this input, we put a ball into the leftmost device from the left side, then it is returned from the same place.       Sample Input 2 5 2
ABAAA      Sample Output 2 ABBBA       Sample Input 3 4 123456789
AABB      Sample Output 3 BABA

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import deque




N,K = map(int,readline().split())
S = read().rstrip()
S = deque(0 if x == b'A'[0] else 1 for x in S)


U = 10**6
if K > U:
    K = U + (K&1)

flip = 0
for _ in range(K):
    top = S[0]^flip
    if top == 0:
        
        S[0] ^= 1
    else:
        
        flip ^= 1
        S.popleft()
        S.append(flip)

if flip == 0:
    A = 'A'; B = 'B'
else:
    A = 'B'; B = 'A'
answer = ''.join(A if x == 0 else B for x in S)
print(answer)

Solution (Python):
n,k=map(int,input().split())
s=list(input())*3
p=['A' if s[i]=='B' else 'B' for i in range(n)]*3
head,tail=0,n
if k>2*n:
	k=2*n+k%2
for i in range(k):
	if s[head]=='A':
		s[head],p[head]='B','A'
	else:
		s,p=p,s
		head+=1
		s[tail],p[tail]='A','B'		
		tail+=1
print(''.join(s[head:tail]))

Solution (Python):
n,k=map(int,input().split())
s=list(input())*3
p=['A' if s[i]=='B' else 'B' for i in range(n)]*3
head,tail=0,n
for i in range(2*n):
	if s[head]=='A':
		s[head],p[head]='B','A'
	else:
		s,p=p,s
		head+=1
		s[tail],p[tail]='A','B'		
		tail+=1
	if i==k-1:
		print(''.join(s[head:tail]))
		break
else:
	if n%2==1 and k%2==1:
		if s[head]=='A':
			s[head],p[head]='B','A'
		else:
			s,p=p,s
			head+=1
			s[tail],p[tail]='A','B'		
			tail+=1
	print(''.join(s[head:tail]))

Solution (Python):
import sys
from itertools import accumulate
from collections import deque

def solve():
    N, K = map(int, input().split())
    S = input()

    '''
    if N == 1:
        if K % 2 == 0:
            print(S)
        else:
            print('A' if S == 'B' else 'B')

        return
    '''

    q = deque([1 if c == 'A' else 0 for c in S])
    cb = 0

    for i in range(min(K, 2 * N + K % 2)):
        if q[0] == (cb ^ 1):
            q[0] ^= 1
        else:
            q.popleft()
            q.append(cb)
            cb ^= 1

    

    ans = ['A' if (c ^ cb) else 'B' for c in q]
    print(''.join(ans))


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    if n == 1:
        if k % 2:
            print 'A' if s[0] == 'B' else 'B'
        else:
            print s[0]
        return
    f = True
    for i in xrange(400020):
        if i >= k:
            if f:
                print ''.join(list(ans))
            else:
                print ''.join('A' if x == 'B' else 'B' for x in ans)
            break
        if (ans[0] == 'A') == f:
            ans[0] = 'B' if f else 'A'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 400020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if n % 2 and k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()