Problem: Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Submission attempts (in order):
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		
k = int(input())						# 5


ans=0

if len(s)==1:
	ans=(k)//2
	print(ans)
	exit(0)

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)





from itertools import groupby
#s='AAABBCABBB'


m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

if m[0][0]==m[-1][0]:

	for i in range(1,len(m)-1):
		ans+=(m[i][1]//2)

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		#p(a,i)
		ans+=(i//2)*k


print(ans)


#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		
k = int(input())						# 5


ans=0

if len(s)==1:
	ans=(k)//2
	print(ans)
	exit(0)

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)





from itertools import groupby
#s='AAABBCABBB'


m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

if m[0][0]==m[-1][0]:

	for i in range(1,len(m)-1):
		ans+=(m[i][1]//2)

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		print(a,i)
		ans+=(i//2)


print(ans)


#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		
k = int(input())						# 5


ans=0

if len(s)==1:
	ans=(k)//2
	print(ans)
	exit(0)

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)





from itertools import groupby
#s='AAABBCABBB'


m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

if m[0][0]==m[-1][0]:

	for i in range(1,len(m)-1):
		ans+=(m[i][1]//2)

	ans+=(m[0][1]//2)
	ans+=(m[-1][1]//2)

	ans+=((m[0][1]+m[-1][1])//2)*(k-1)

else:
	for a,i in m:
		#print(a,i)
		ans+=(i//2)


print(ans)


#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		
k = int(input())						# 5


ans=0

if len(s)==1:
	ans=(k)//2
	print(ans)
	exit(0)

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)





from itertools import groupby
m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

if len(m)==1:
	i=m[0][1]
	ans+=((i*k)//2)
	print(ans)
	exit(0)
		

for a,i in m:
	#p(a,i)
	ans+=(i//2)*k


if m[0][0]==m[-1][0]:
	if m[0][1]%2==1:
		if m[-1][1]%2==1:
			ans+=(k-1)

print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted