Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
s=list(input())*2
p=['A' if s[i]=='B' else 'B' for i in range(n)]*2
head,tail=0,n
for i in range(2*n):
	if s[0]=='A':
		s[0],p[0]='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[0]=='A':
			s[0],p[0]='B','A'
		else:
			s,p=p,s
			head+=1
			s[tail],t[tail]='A','B'
			tail+=1
	print(''.join(s[head:tail]))

Fixed submission:
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]))

Buggy python submission (verdict: Time Limit Exceeded):
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))

Fixed submission:
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]))

Buggy python submission (verdict: Time Limit Exceeded):
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))

Fixed submission:
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]))

Buggy python submission (verdict: Time Limit Exceeded):
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))

Fixed submission:
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]))