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



Sample input:
5 1
ABAAA


Sample output:
BBAAA

Submission status: Wrong Answer

Solution code:
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

# f(AX) = XB
# f(BX) = flip(X) + A = flip(XB)

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

# Kが大きいときは、周期で減らせる
if K > 20100 + 100:
    K = 20100 + (K&1)

flip = 0
for _ in range(K):
    top = S[0]^flip
    if top == 0:
        # A
        S[0] ^= 1
    else:
        # B 
        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)

Submission status: Wrong Answer

Solution code:
def solution():
	n,k = [int(x) for x in input().split(' ')] 
	txt = input()
	s = list(txt)
	cycle = []
	nbr = 0
	cache = {}
	suffix = ''
	while (txt+suffix) not in cycle:
		cycle.append(txt+suffix)
		position = 0
		toLeft = True
		suffix = ''
		while txt[len(txt)-len(suffix)-2:] == 'BA':
			suffix+='BA'
			txt = txt[(len(txt)-len(suffix)-2):]
		s = list(txt)
		while position<len(s) and position>=0:

			if s[position] == 'A':
				s[position] = 'B'
				toLeft = not(toLeft)
			else:
				s[position] = 'A'

			position +=1 if toLeft else -1

			
		nbr +=1
		txt = ''.join(s)
		if nbr == k:
			return txt
		

	startingCycle = cycle.index(txt+suffix)
	cycle = cycle[startingCycle:]
	return cycle[(k-startingCycle)%len(cycle)]


print(solution())

Submission status: Wrong Answer

Solution code:
def solution():
	n,k = [int(x) for x in input().split(' ')] 
	txt = input()
	s = list(txt)
	cycle = []
	nbr = 0
	cache = {}
	suffix = ''
	while (txt+suffix) not in cycle:
		cycle.append(txt+suffix)
		position = 0
		toLeft = True
		suffix = ''
		while txt[len(txt)-len(suffix)-2:] == 'BA':
			suffix+='BA'
			txt = txt[(len(txt)-len(suffix)-2):]
		while position<len(s) and position>=0:

			if s[position] == 'A':
				s[position] = 'B'
				toLeft = not(toLeft)
			else:
				s[position] = 'A'

			position +=1 if toLeft else -1

			
		nbr +=1
		txt = ''.join(s)
		if nbr == k:
			return txt
		

	startingCycle = cycle.index(txt+suffix)
	cycle = cycle[startingCycle:]
	return cycle[(k-startingCycle)%len(cycle)]


print(solution())

Submission status: Wrong Answer

Solution code:
def solution():
	n,k = [int(x) for x in input().split(' ')] 
	txt = input()
	s = list(txt)
	cycle = []
	nbr = 0
	cache = {}
	suffix = ''
	while txt not in cycle:
		cycle.append(txt)
		position = 0
		toLeft = True
		while txt[len(txt)-len(suffix)-2:] == 'BA':
			suffix+='BA'
			txt = txt[(len(txt)-len(suffix)-2):]
		while position<len(s) and position>=0:

			if s[position] == 'A':
				s[position] = 'B'
				toLeft = not(toLeft)
			else:
				s[position] = 'A'

			position +=1 if toLeft else -1

			
		nbr +=1
		txt = ''.join(s)+suffix
		if nbr == k:
			return txt
		

	startingCycle = cycle.index(txt)
	cycle = cycle[startingCycle:]
	return cycle[(k-startingCycle)%len(cycle)]


print(solution())

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s,p=list(input()),""
for i in range(k):
	s,p,p2=['A' if s[i]=='B' else 'B' for i in range(1,n)]+['A'],str(s),p
	if str(s)==p:
		print(''.join(s))
		break
	elif str(s)==p2:
		if k%2==i%2:
			print(''.join(s))
		else:
			print(''.join(filter(lambda x:x=='A' or x=='B',list(p))))
		break
else:
	print(''.join(s))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s=list(input())
for i in range(k):
	cur,d,p=0,1,str(s)
	while cur in range(4):
		if s[cur]=='A':
			s[cur]='B'
			d=-d
			cur+=d
		else:
			s[cur]='A'
			cur+=d
	if str(s)==p:
		break
print(''.join(s))

Submission status: Wrong Answer

Solution code:
import numpy as np

###get inputs
N, K = map(lambda x: int(x), raw_input().split())
S = map(lambda x: 0 if x == 'A' else 1, raw_input())
S = np.asarray(S)
###visualize function
def visualize(S):
    # print S, 
    S = map(lambda x: 'A' if x == 0 else 'B', S)
    print "".join(S),

### A:0, B:1
r_times = 0
#for k in range(K):
k=-1
while k <= 2*N+10:
    k+=1
    #print '>>k:', k,
    #visualize(S)
    ball_pos = 0
    ball_direct = 1
    while ball_pos > -1 and ball_pos < N-r_times:
        if S[ball_pos] == 0:
            S[ball_pos] = 1
            ball_pos -= 1 * ball_direct
            ball_direct *= -1
        else:
            S[ball_pos] = 0
            ball_pos += 1 * ball_direct
    if ball_direct == 1:
        r_times += 1
        
    #print 'r' if ball_direct == 1 else 'l',
    #print ball_pos,
    #print '\t',
    #visualize(S)
    #print r_times

    #print N-r_times, range(N-r_times,N,2), range(N-r_times+1,N,2)
    tmp = N - r_times if N%2==0 else N-r_times-1
    S[tmp::2] =  1 if (r_times%2 == 0) else 0
    S[tmp+1::2] =  0 if (r_times%2 == 0) else 1
    
    if r_times == N-1:
        #print 'break'
        break

if r_times == N-1 and (K-k)%2==0:
    S[0]=1

visualize(S)
"""
5 2
ABAAA
"""

Submission status: Wrong Answer

Solution code:
import numpy as np

###get inputs
N, K = map(lambda x: int(x), raw_input().split())
S = map(lambda x: 0 if x == 'A' else 1, raw_input())
S = np.asarray(S)
###visualize function
def visualize(S):
    # print S, 
    S = map(lambda x: 'A' if x == 0 else 'B', S)
    print "".join(S),

### A:0, B:1
r_times = 0
#for k in range(K):
k=-1
while k <= 2*N+10:
    k+=1
    #print '>>k:', k,
    #visualize(S)
    ball_pos = 0
    ball_direct = 1
    while ball_pos > -1 and ball_pos < N-r_times:
        if S[ball_pos] == 0:
            S[ball_pos] = 1
            ball_pos -= 1 * ball_direct
            ball_direct *= -1
        else:
            S[ball_pos] = 0
            ball_pos += 1 * ball_direct
    if ball_direct == 1:
        r_times += 1
        
    #print 'r' if ball_direct == 1 else 'l',
    #print ball_pos,
    #print '\t',
    #visualize(S)
    #print r_times

    #print N-r_times, range(N-r_times,N,2), range(N-r_times+1,N,2)
    tmp = N - r_times #if N-r_times%2==0 else N-r_times-1
    S[tmp::2] =  1 if (r_times%2 == 0) else 0
    S[tmp+1::2] =  0 if (r_times%2 == 0) else 1
    
    if r_times == N-1:
        #print 'break'
        break

if r_times == N-1 and (K-k)%2==0:
    S[0]=1

visualize(S)
"""
5 2
ABAAA
"""

Submission status: Wrong Answer

Solution code:
import numpy as np

###get inputs
N, K = map(lambda x: int(x), raw_input().split())
S = map(lambda x: 0 if x == 'A' else 1, raw_input())
S = np.asarray(S)
###visualize function
def visualize(S):
    # print S, 
    S = map(lambda x: 'A' if x == 0 else 'B', S)
    print "".join(S),

### A:0, B:1
r_times = 0
#for k in range(K):
k=-1
while k < 2*N:
    k+=1
    #print '>>k:', k,
    #visualize(S)
    ball_pos = 0
    ball_direct = 1
    while ball_pos > -1 and ball_pos < N-r_times:
        if S[ball_pos] == 0:
            S[ball_pos] = 1
            ball_pos -= 1 * ball_direct
            ball_direct *= -1
        else:
            S[ball_pos] = 0
            ball_pos += 1 * ball_direct
    if ball_direct == 1:
        r_times += 1
        
    #print 'r' if ball_direct == 1 else 'l',
    #print ball_pos,
    #print '\t',
    #visualize(S)
    #print r_times

    #print N-r_times, range(N-r_times,N,2), range(N-r_times+1,N,2)
    tmp = N - r_times #if N-r_times%2==0 else N-r_times-1
    S[tmp::2] =  1 if (r_times%2 == 0) else 0
    S[tmp+1::2] =  0 if (r_times%2 == 0) else 1
    
    if r_times == N-1:
        #print 'break'
        break

if r_times == N-1 and (K-k)%2==0:
    S[0]=1

visualize(S)
"""
5 2
ABAAA
"""

Submission status: Wrong Answer

Solution code:
import numpy as np

###get inputs
N, K = map(lambda x: int(x), raw_input().split())
S = map(lambda x: 0 if x == 'A' else 1, raw_input())
S = np.asarray(S)
###visualize function
def visualize(S):
    # print S, 
    S = map(lambda x: 'A' if x == 0 else 'B', S)
    print "".join(S),

### A:0, B:1
r_times = 0
#for k in range(K):
k=-1
while k < K-1:
    k+=1
    #print '>>k:', k,
    #visualize(S)
    ball_pos = 0
    ball_direct = 1
    while ball_pos > -1 and ball_pos < N-r_times:
        if S[ball_pos] == 0:
            S[ball_pos] = 1
            ball_pos -= 1 * ball_direct
            ball_direct *= -1
        else:
            S[ball_pos] = 0
            ball_pos += 1 * ball_direct
    if ball_direct == 1:
        r_times += 1
        
    #print 'r' if ball_direct == 1 else 'l',
    #print ball_pos,
    #print '\t',
    #visualize(S)
    #print r_times

    #print N-r_times, range(N-r_times,N,2), range(N-r_times+1,N,2)
    tmp = N - r_times #if N-r_times%2==0 else N-r_times-1
    S[tmp::2] =  1 if (r_times%2 == 0) else 0
    S[tmp+1::2] =  0 if (r_times%2 == 0) else 1
    
    if r_times == N-1:
        #print 'break'
        break

if r_times == N-1 and (K-k)%2==0:
    S[0]=1

visualize(S)
"""
5 2
ABAAA
"""

Submission status: Wrong Answer

Solution code:
import numpy as np

Inp = list(map(int, input().split()))

N = Inp[0]
K = Inp[1]

s = input()

a = []
for i in range(0, len(s)):
    if s[i] == 'A':
        a.append(1)
    else:
        a.append(-1)

r_arr = np.array(a)
if K < 2 * N:
    for i in range(0, K):
        if r_arr[0] == 1:
            r_arr[0] = -1
            continue
        r_arr = np.delete(r_arr, 0)
        r_arr *= -1
        r_arr = np.append(r_arr, 1)
else:
    if len(r_arr) % 2 == 0:
        for i in range(0, N):
            if i % 2 == 0:
                r_arr[i] = -1
            else:
                r_arr[i] = 1
    else:
        r_arr[0] = -1
        for i in range(1, N):
            if i % 2 == 1:
                r_arr[i] = -1
            else:
                r_arr[i] = 1
    if (K - 2 * N) % 2 == 1:
        r_arr[0] *= -1

res = ""
for i in range(0, len(r_arr)):
    if r_arr[i] == 1:
        res += 'A'
    else:
        res += 'B'

print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

n_mac, n_bal = map(int, input().split())
l_mac = list(input())
counter = 0
l_all_mac_state = []
ans_state = 0


def throw_ball(l_state):
    global l_mac
    if l_mac[l_state[0]] == "A":
        l_mac[l_state[0]] = "B"
        if l_state[1] == 0:
            return [(l_state[0] - 1), 1]
        elif l_state[1] == 1:
            return [(l_state[0] + 1), 0]

    elif l_mac[l_state[0]] == "B":
        l_mac[l_state[0]] = "A"
        if l_state[1] == 0:
            return [(l_state[0] + 1), 0]
        elif l_state[1] == 1:
            return [(l_state[0] - 1), 1]


def make_state_num_from_list(l_state):
    num = 0
    for ind, item in enumerate(l_state):
        if item == "A":
            num += 2 ** ind
    return num


for i in range(n_bal):
    now_state = [0, 0]
    counter += 1

    while True:
        now_state = throw_ball(now_state)
        if now_state[0] < 0 or now_state[0] > (len(l_mac) - 1):
            break

    num_state = make_state_num_from_list(l_mac)
    if num_state in l_all_mac_state:
        period = counter - 1
        ans_state = n_bal % period
        break
    l_all_mac_state.append(num_state)
else:
    print (l_mac)

if ans_state != 0:
    for i in range(ans_state):
        now_state = [0, 0]

        while True:
            now_state = throw_ball(now_state)
            if now_state[0] < 0 or now_state[0] > (len(l_mac) - 1):
                break
    print (l_mac)

Submission status: Wrong Answer

Solution code:
import sys

INF = 10**9+7

stdin = sys.stdin
def na(): return map(int, stdin.readline().split())
def nal(): return list(map(int, stdin.readline().split()))
def ns(): return stdin.readline().strip()
def nsl(): return list(stdin.readline().strip())
def ni(): return int(stdin.readline())

n, k = nal()
s = nsl()

if(s[0] == 'A'):
    s[0] = 'B'
    k -= 1

lst = []
st = s[0]
cnt = 0
for i in s:
    if(st == i):
       cnt += 1
    else:
        lst.append(cnt)
        cnt = 1
        st = i
lst.append(cnt)

st = 'B'
en = False
for i in range(k):
    if(st == 'A'):
        if(lst[0] == 1):
            lst.pop(0)
            lst[0] += 1
        else:
            lst.insert(0, 1)
            lst[1] -= 1
        st  = 'B'

    else:
        lst.append(1)
        lst[0] -= 1
        if(lst[0] == 0):
            lst.pop(0)
        else:
            st = 'A'
    for j in lst:
        if(j == 1):
            en = True
        else:
            en = False
            break
    if(en == True):
        break

ans = []
for i in lst:
    for j in range(i):
        ans.append(st)
    if(st == 'A'):
        st = 'B'
    else:
        st = 'A'

print("".join(ans))

Submission status: Wrong Answer

Solution code:
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)):
        if q[0] == (cb ^ 1):
            q[0] ^= 1
        else:
            q.popleft()
            q.append(cb)
            cb ^= 1

    # print(q)

    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()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
from collections import deque

def solve():
    N, K = map(int, input().split())
    S = input()
    q = deque([1 if c == 'A' else 0 for c in S])
    cb = 0

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

    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()

Submission status: Wrong Answer

Solution code:
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 not k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
from collections import deque

def solve():
    N, K = map(int, input().split())
    S = input()
    q = deque([1 if c == 'A' else 0 for c in S])
    cb = 1
    n = 0

    for i in range(K):
        if q[0] == cb:
            q[0] ^= 1
            # cb ^= 1
        else:
            q.popleft()
            q.append(0)
            cb ^= 1
            n += 1

    # print(q)

    ans = ['A' if (c + n) % 2 == 1 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()

Submission status: Wrong Answer

Solution code:
N, K = map(int, raw_input().split())
S = raw_input()
S = [0 if s=='A' else 1 for s in S]

def do_opp():
	pos=0
	while not (pos ==-1 or pos == N):
		if S[pos] == 0:
			S[pos] = 1
			pos -= 1
		else:
			S[pos] = 0
			pos += 1

for _ in xrange(K):
	do_opp()

ret = ""
for s in S:
	ret += 'A' if s==0 else 'B'
print ret

Submission status: Wrong Answer

Solution code:
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(800020):
        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 -= 800020
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()

Submission status: Wrong Answer

Solution code:
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 'A'
        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 k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] ='A'
        print ''.join(ans)
main()

Submission status: Wrong Answer

Solution code:
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 'A'
        else:
            print s[0]
        return
    f = True
    for i in xrange(200010):
        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 -= 200010
        k %= 2
        if not f:
            ans = ['B' if x == 'A' else 'A' for x in ans]
            ans = deque(ans)
        if k:
            if ans[0] == 'A':
                ans[0] = 'B'
            else:
                ans[0] = 'A'
        print ''.join(ans)
main()

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    n, k = map(int, raw_input().split())
    s = raw_input().strip()
    ans = deque(list(s))
    f = True
    for i in xrange(200010):
        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'
        else:
            f = not f
            ans.popleft()
            ans.append('A' if f else 'B')
    else:
        k -= 200010
        if f:
            if k % 2:
                ans[0] = 'B'
            print ''.join(list(ans))
        else:
            if k % 2:
                ans[0] = 'A'
            print ''.join('A' if x == 'B' else 'B' for x in ans)
main()

Submission status: Time Limit Exceeded

Solution code:
#D
#naive simulation, though TLE.
n,k = [int(i) for i in input().split()]
s = input()
s = [1 if v=="A" else 0 for v in s] #A=1, B=0

direct=[1,-1]

for _ in range(k):
    ind = 0
    vec = 0
    while 0<=ind and ind<n:
        vec    = (vec+s[ind])%2
        s[ind] = (s[ind]+1)%2
        ind   += direct[vec]

a = ""
for v in s:
    a += "B" if v==0 else "A"

print(a)

Submission status: Time Limit Exceeded

Solution code:
a,b = map(int,input().split(" "))
ar = list(input())
for j in range(b):
    i = 0
    g = "R"
    while True:
        if i < 0 or i == a:
            break
        else:
            if ar[i] == "A":
                ar[i] = "B"
                if g == "R":
                    i -= 1
                    g = "L"
                else:
                    i += 1
                    g = "R"
            else:
                ar[i] = "A"
                if g == "R":
                    i += 1
                else:
                    i -= 1
print("".join(ar))

Submission status: Time Limit Exceeded

Solution code:
def solution():
	n,k = [int(x) for x in input().split(' ')] 
	s = list(input())
	cycle = []
	txt = ''.join(s)
	nbr = 0
	while txt not in cycle:
		cycle.append(txt)
		position = 0
		toLeft = True
		while position<len(s) and position>=0:
			if s[position] == 'A':
				s[position] = 'B'
				toLeft = not(toLeft)
			else:
				s[position] = 'A'

			position +=1 if toLeft else -1
		nbr +=1
		txt = ''.join(s)
		if nbr == k:
			return txt
		

	startingCycle = cycle.index(txt)
	cycle = cycle[startingCycle:]
	return cycle[(k-startingCycle)%len(cycle)]


print(solution())

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s,p=list(input()),""
def proc(t):
	return ['B']+t[1:] if t[0]=='A' else ['A' if t[i]=='B' else 'B' for i in range(1,n)]+['A']
for i in range(2*n):
	s=proc(s)
	if i==k-1:
		print(''.join(s))
		break
else:
	if n%2==1 and k%2==1:
		s=proc(s)
	print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s,p=list(input()),""
for i in range(k):
	cur,d,p,p2=0,1,str(s),p
	while cur>=0 and cur<n:
		if s[cur]=='A':
			s[cur]='B'
			d=-d
			cur+=d
		else:
			s[cur]='A'
			cur+=d
	if str(s)==p:
		print(''.join(s))
		break
	elif str(s)==p2:
		if k%2==i%2:
			print(''.join(s))
		else:
			print(''.join(filter(lambda x:x=='A' or x=='B',list(p))))
		break
else:
	print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
s=list(input())
for i in range(k):
	cur,d,p=0,1,str(s)
	while cur>=0 and cur<n:
		if s[cur]=='A':
			s[cur]='B'
			d=-d
			cur+=d
		else:
			s[cur]='A'
			cur+=d
	if str(s)==p:
		break
print(''.join(s))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

Inp = list(map(int, input().split()))

N = Inp[0]
K = Inp[1]

s = input()

a = []
for i in range(0, len(s)):
    if s[i] == 'A':
        a.append(1)
    else:
        a.append(-1)

r_arr = np.array(a)
if K < 2 * N:
    for i in range(0, K):
        if r_arr[0] == 1:
            r_arr[0] = -1
            continue
        r_arr = np.delete(r_arr, 0)
        r_arr *= -1
        r_arr = np.append(r_arr, 1)
else:
    if len(r_arr) % 2 == 0:
        for i in range(0, N):
            if i % 2 == 0:
                r_arr[i] = -1
            else:
                r_arr[i] = 1
    else:
        if r_arr[0] == 1:
            r_arr[0] = -1
        for i in range(1, N):
            if i % 2 == 1:
                r_arr[i] = -1
            else:
                r_arr[i] = 1
        if (K - (2 * N)) % 2 == 1:
            r_arr[0] *= -1

res = ""
for i in range(0, len(r_arr)):
    if r_arr[i] == 1:
        res += 'A'
    else:
        res += 'B'

print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

Inp = list(map(int, input().split()))

N = Inp[0]
K = Inp[1]

s = input()

a = []
for i in range(0, len(s)):
    if s[i] == 'A':
        a.append(1)
    else:
        a.append(-1)

r_arr = np.array(a)
n = min(2 * N, K)
if K > 2 * N:
    if (K - 2 * N) % 2 == 1:
        n += 1
for i in range(0, n):
    if r_arr[0] == 1:
        r_arr[0] = -1
        continue
    r_arr = np.delete(r_arr, 0)
    r_arr *= -1
    r_arr = np.append(r_arr, 1)

res = ""
for i in range(0, len(r_arr)):
    if r_arr[i] == 1:
        res += 'A'
    else:
        res += 'B'

print(res)

Submission status: Time Limit Exceeded

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()


def main():
    n,k = LI()
    q = [1 if c == 'A' else 0 for c in S()]
    bq = None
    for _ in range(k):
        i = 0
        t = 1
        while 0 <= i < n:
            if q[i] == 1:
                t = -t
                q[i] = 0
            else:
                q[i] = 1
            i += t
        if q == bq and (_+k)%2 == 0:
            break
        bq = copy.copy(q)
    return ''.join(['A' if c == 1 else 'B' for c in q])

print(main())

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = list(input())
k = 0
N2 = N
for i,a in enumerate(S):
	if a == "A":
		S[i] = 1
	else:
		S[i] = -1
while k < 2*N and k < K:
	i = 0
	j = 1
	I = [-1,0]
	if S[0] == 1:
		S[0] = -1
	else:
		S = list(map(lambda x: -x,S))
		S .pop(0)
		S.append(1)
	k = k + 1
for i,a in enumerate(S):
	if a == 1:
		S[i] = "A"
	else:
		S[i] = "B"
if k == K:
	print("".join(S))
elif N % 2 == 0:
	print("".join(S))
elif S[0] == "A":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] = "B"
		print("".join(S))
elif S[0] == "B":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] ="A"
		print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
import sys
from itertools import accumulate
from collections import deque

def solve():
    N, K = map(int, input().split())
    S = input()
    q = deque([1 if c == 'A' else 0 for c in S])
    cb = 1

    for i in range(K):
        if q[0] == cb:
            q[0] ^= 1
            # cb ^= 1
        else:
            q.popleft()
            q.append(cb ^ 1)
            cb ^= 1

    # print(q)

    ans = ['A' if (c ^ cb ^ 1) 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()

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, raw_input().split())
S = raw_input()
S = [0 if s=='A' else 1 for s in S]

def do_opp():
	pos=0
	left=True
	while not (pos ==-1 or pos == N):
		if S[pos] == 0:
			S[pos] = 1
			if left:
				pos -= 1
			else:
				pos += 1
			left = not left
		else:
			S[pos] = 0
			if left:
				pos += 1
			else:
				pos -= 1

for _ in xrange(K):
	do_opp()

ret = ""
for s in S:
	ret += 'A' if s==0 else 'B'
print ret

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = list(input())
k = 0
N2 = N
while k < 2*N and k < K and len(S) > 2:
	i = 0
	j = 1
	I = [-1,0]
	if len(S) > 3 and S[-1] == "A" and S[-2] == "B" and S[-3] == "A" and S[-4] =="B":
		S = S[:-2]
		N2 = N2 - 2
	while -1<i<N2:
		if S[i] == "A":
			S[i] = "B"
			i = I[j - 1]
			I.append(i)
			j = j + 1
		elif S[i] == "B":
			S[i] = "A"
			i = i + i - I[j - 1]
			I.append(i)
			j = j + 1
	k = k + 1
S = S + ["B","A"]*((N-len(S))//2)
if k == K:
	print("".join(S))
elif N % 2 == 0:
	print("".join(S))
elif S[0] == "A":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] = "B"
		print("".join(S))
elif S[0] == "B":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] ="A"
		print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = list(input())
k = 0
N2 = N
while k < 2*N and k < K:
	i = 0
	j = 1
	I = [-1,0]
	if len(S) > 3 and S[-1] == "A" and S[-2] == "B":
		S = S[:-2]
		N2 = N2 - 2
	while -1<i<N2:
		if S[i] == "A":
			S[i] = "B"
			i = I[j - 1]
			I.append(i)
			j = j + 1
		elif S[i] == "B":
			S[i] = "A"
			i = i + i - I[j - 1]
			I.append(i)
			j = j + 1
	k = k + 1
S = S + ["B","A"]*((N-len(S))//2)
if k == K:
	print("".join(S))
elif N % 2 == 0:
	print("".join(S))
elif S[0] == "A":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] = "B"
		print("".join(S))
elif S[0] == "B":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] ="A"
		print("".join(S))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
S = list(input())
k = 0
while k < 3*N and k < K:
	i = 0
	j = 1
	I = [-1,0]
	while -1<i<N:
		if S[i] == "A":
			S[i] = "B"
			i = I[j - 1]
			I.append(i)
			j = j + 1
		elif S[i] == "B":
			S[i] = "A"
			i = i + i - I[j - 1]
			I.append(i)
			j = j + 1
	k = k + 1
if k == K:
	print("".join(S))
elif N % 2 == 0:
	print("".join(S))
elif k == 3*N and S[0] == "A":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] = "B"
		print("".join(S))
elif k == 3*N and S[0] == "B":
	z = (K - k) % 2
	if z == 0:
		print("".join(S))
	else:
		S[0] ="A"
		print("".join(S))