TnT/Multi_CodeNet4Repair huggingface dataset:
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 queue
import numpy as np
n = int(input())
class Node:
    def __init__(self):
        self.nextNode = []
        self.checkNgo = -1
        self.group = -1
        
Nodes = [Node() for i in range(n)]
for i in range(n):
    s = input()
    for j in range(len(s)):
        if s[j] == '1':
            Nodes[i].nextNode.append(j)
            
def check(node,ngo):
    if node.checkNgo == -1:
        node.checkNgo = ngo
    elif node.checkNgo != ngo:
        return False
    elif node.checkNgo == ngo:
        return True
    for i in node.nextNode:
        if not check(Nodes[i],(ngo+1)%2):
            return False
    return True
    
def ngoCheck():
    stack = [Nodes[0]]
    Nodes[0].checkNgo = 0
    while stack:
        node = stack.pop()
        for i in node.nextNode:
            if Nodes[i].checkNgo != -1 and Nodes[i].checkNgo == node.checkNgo:
                return False
            if Nodes[i].checkNgo == -1:
                Nodes[i].checkNgo = (node.checkNgo+1)%2
                stack.append(Nodes[i])
    return True
    
if not ngoCheck():
    print(-1)
    exit(0)
    
def solv():
    d = [[0 for i in range(n)] for j in range(n)]
    for i in range(n):
        for j in range(n):
            if j in Nodes[i].nextNode:
                d[i][j] = 1
            else:
                d[i][j] = np.inf
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k]+d[k][j]:
                    d[i][j] = d[i][k]+d[k][j]
    return d
    
def saidai(d):
    maxi = 0
    for i in range(n):
        for j in range(n):
            maxi = max(maxi,d[i][j])
    return maxi
    
d = solv()
print(saidai(d)+1)

s = input()
k = int(input())

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

if len(list(set(s))) == 1 and len(s)%2 == 1:
    print((len(s)//2)*k+k//2)
    exit(0)

count = 0
    
ngo = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ngo+=1
    else:
        count += ngo//2
        ngo=1
count += ngo//2

ngongo = 0
while ngongo < len(s) and s[len(s)-ngongo-1] == s[-1]:
    ngongo+=1

ngongongo = 0
while ngongongo < len(s) and s[ngongongo] == s[0]:
    ngongongo += 1
    
f = 0
if ngongo %2 == 1 and ngongongo %2 == 1 and s[0] == s[-1]:
    count += 1
    f += 1

print(count*k-f)

Verdicts: Runtime Error

Accepted
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

# A - Connection and Disconnection
import string

S = input()
K = int(input())
T = S + S[0]

lower = string.ascii_lowercase
ans = 0
is_specila_case = False

for c in lower:
	key = c * 2
	ans += T.count(key)

	if T.endswith(key):
		is_specila_case = True

ans *= K

if is_specila_case:
	print(ans - 1)
else:
	print(ans)

import sys

# A - Connection and Disconnection
import string

S = input()
K = int(input())
T = S + S[0]

lower = string.ascii_lowercase
ans = 0
is_specila_case = False

for c in lower:
	key = c * 2
	ans += T.count(key)

	if T.endswith(key):
		is_specila_case = True

ans *= K

if is_specila_case:
	print(ans - 1)
else:
	print(ans)import sys

# A - Connection and Disconnection
import string

S = input()
K = int(input())
T = S + S[0]

lower = string.ascii_lowercase
ans = 0
is_specila_case = False

for c in lower:
	key = c * 2
	ans += T.count(key)

	if T.endswith(key):
		is_specila_case = True

ans *= K

if is_specila_case:
	print(ans - 1)
else:
	print(ans)

# A - Connection and Disconnection
def count(s, c):
	count_ = s.count(c*2)
	
	while c*3 in s:
		s = s.replace(c*3, c+'_'+c)
	
	while c*2 in s:
		s = s.replace(c*2, c+'_')
	
	return min(count_, s.count('_'))


def get_startswith(s, c):
	key = c
	
	while s.startswith(key+c):
		key += c
	
	return len(key)
	
	
def get_endswith(s, c):
	key = c
	
	while s.endswith(key+c):
		key += c
	
	return len(key)


import string

S = input()
K = int(input())

lower = string.ascii_lowercase
ans = 0

if S[0] == S[-1:]:
	start = get_startswith(S, S[0])
	end = get_endswith(S, S[0])	
	
	if start == end == len(S):
		print(len(S) * K // 2)
	else:
		ans = 0
		
		ans += start // 2
		ans += end // 2
		ans += ((start + end) // 2) * (K - 1)
		
		for c in lower:
			ans += count(S[start:len(S)-end], c) * K

		print(ans)	
else:
	for c in lower:
		ans += count(S, c)

	print(ans * K)

Verdicts: Wrong Answer

Runtime Error

Accepted
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):
# A - Connection and Disconnection
S = input()
K = int(input())
S = S + '1'


cnt = []
conti = 1
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        conti += 1
    else:
        cnt.append(conti)
        conti = 1


if S[0] == S[-2]:
    
    if cnt[0]%2 == 1 and cnt[-1]%2 == 1:
        comp = K - 1
        
ans = [a//2 for a in cnt if a >= 2]
ans = sum(ans)*K + comp
print(ans)        

# A - Connection and Disconnection
S = input()
K = int(input())
S = S + '1'


cnt = []
conti = 1
for i in range(1,len(S)):
    if S[i-1] == S[i]:
        conti += 1
    else:
        cnt.append(conti)
        conti = 1
        

comp = 0
if S[0] == S[-2]:
    
    if cnt[0]%2 == 1 and cnt[-1]%2 == 1:
        comp = K - 1
    
    if len(set(S[:-1])) == 1 and len(S[:-1])%2 == 1:
        comp = K // 2
        
ans = [a//2 for a in cnt if a >= 2]
ans = sum(ans)*K + comp
print(ans)        

Verdicts: Runtime Error

Accepted
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):
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
def get_abc(s1,s2):
    if s1 == 'a':
        if s2 == 'b':
            return 'c'
        else:
            return 'b'
    elif s1 == 'b':
        if s2 == 'a':
            return 'c'
        else:
            return 'a'
    else:
        if s2 == 'a':
            return 'b'
        else:
            return 'a'
        
s_len = len(S)
count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S_list[0])
    pre_character = S_list[i]
if S_list[0] == S_list[-1]:
    print((count+1)*K - 1)
else:
    print(count*K)
print(S_list)

S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def try_count(pre_character):
    S_list=list(S)
    count = 0
    for i in range(s_len):
        if pre_character == S_list[i]:
            count += 1
            if (s_len - 1) != i:
                S_list[i] = '' #get_abc(pre_character,S_list[i+1])
            else:
                S_list[i] = '' #get_abc(pre_character,S[0])
        pre_character = S_list[i]
    return count,S_list
first_count,fist_list = try_count('')
#print(first_count,fist_list)
second_count,second_list = try_count(fist_list[-1])
#print(second_count,second_list)
third_count,third_list = try_count(second_list[-1])
#print(third_count,third_list)

if second_count == third_count:
    print(first_count+ (second_count*(K-1)))
else:
    gusu = K//2
    kisu = K - gusu
    print((first_count*kisu)+ (second_count*gusu))

Verdicts: Wrong Answer

Accepted
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):
#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline

def countProcedure(string):
  res = 0
  strCount = 1
  prev = "" 
  for i in range(len(string)):
    if string[i] == prev:
      strCount += 1
    else:
      res += strCount // 2
      strCount = 1
    prev = string[i]

  if strCount != 1:
    res += strCount // 2
  return res

def solve(s,k):
  if s[0] != s[-1]:
    return countProcedure(s)*k
  if all(x == s[0] for x in s):
    return countProcedure(s)*k
  else:
    leftLength = max(i for i in range(1,len(s)) if s[:i]== s[0]*i)
    rightLength = max(i for i in range(1,len(s)) if s[::-1][:i] == s[-1]*i)
    left = s[0]*leftLength
    center = s[leftLength:-rightLength]
    right = s[-1]*rightLength
    ans = countProcedure(left) + countProcedure(center)*k + countProcedure(right) + countProcedure("".join([right,left]))*(k-1)
    return ans


def main():
  S = input()
  K = int(input())
  # N,M = map(int,input().split())
  # a = list(map(int,input().split()))
  
  
if __name__ == '__main__':
  main()

#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline

def countProcedure(string):
  res = 0
  strCount = 1
  prev = "" 
  for i in range(len(string)):
    if string[i] == prev:
      strCount += 1
    else:
      res += strCount // 2
      strCount = 1
    prev = string[i]

  if strCount != 1:
    res += strCount // 2
  return res

def solve(s,k):
  if s[0] != s[-1]:
    return countProcedure(s)*k
  if all(x == s[0] for x in s):
    return len(s)*k//2
  else:
    leftLength = max(i for i in range(1,len(s)) if s[:i]== s[0]*i)
    left = s[0]*leftLength
    right = s[leftLength:]
    ans = countProcedure(left) + countProcedure(right) + countProcedure("".join([right,left]))*(k-1)
    return ans


def main():
  S = input()
  K = int(input())
  # N,M = map(int,input().split())
  # a = list(map(int,input().split()))
  print(solve(S,K))
  
  
if __name__ == '__main__':
  main()


Verdicts: Wrong Answer

Accepted
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
s=input()
k=int(input())
lengthofs=len(s)

if lengthofs == 1:
	print(k//2)
	sys.exit()

i=0
j=0
while i+1 < lengthofs :
	if s[i] != s[i+1]:
		j=1
	i=i+1
if j == 0:
	print((lengthofs*k)//2)
	sys.exit()

i=0
j=1
ans=0
while i+1 < lengthofs :
	if s[i] == s[i+1]:
		j=j+1
	else:
		ans=ans+(j//2)
		j=1
	i=i+1
ans=ans+(j//2)
ans=ans*k

if s[0]!=s[lengthofs-1]:
	print(ans)
else:
	i=0
	while s[i]==s[i+1]:
		i=i+1
	i=(i+1)%2
	j=1
	while s[lengthofs-j] == s[lengthofs-j-1]:
		j=j+1
	j=j%2
	if i*j==1:
		ans=ans+k-1
		print(ans)

import sys
s=input()
k=int(input())
lengthofs=len(s)

if lengthofs == 1:
	print(k//2)
	sys.exit()

i=0
j=0
while i+1 < lengthofs :
	if s[i] != s[i+1]:
		j=1
	i=i+1
if j == 0:
	print((lengthofs*k)//2)
	sys.exit()

i=0
j=1
ans=0
while i+1 < lengthofs :
	if s[i] == s[i+1]:
		j=j+1
	else:
		ans=ans+(j//2)
		j=1
	i=i+1
ans=ans+(j//2)
ans=ans*k

if s[0]!=s[lengthofs-1]:
	print(ans)
else:
	i=0
	while s[i]==s[i+1]:
		i=i+1
	i=(i+1)%2
	j=1
	while s[lengthofs-j] == s[lengthofs-j-1]:
		j=j+1
	j=j%2
	if i*j==1:
		ans=ans+k-1
	print(ans)

Verdicts: Wrong Answer

Accepted
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 math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0

for i in range(1,len(S)):
    if S[i-1] == S[i]:
      count += 1
    else:
      if flag == False:
        firstCount = count
        flag = True
      ans += ((count+1)//2)*K
      count = 0
ans += ((count+1)//2)*K
if S[0]==S[len(S)-1]:
  ans -= ((count+firstCount+2)//2)*(K-1)

print(ans)

import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0

if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)
    ans += math.ceil(count/2)
    ans += math.ceil(firstCount/2)
  else:
    ans += math.ceil(count/2)*K
    ans += math.ceil(firstCount/2)*K
else:
  ans += (count+1)*K//2
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K//2))
else:
  print(ans)

Verdicts: Wrong Answer

Accepted
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
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  print(cnt,cntb)
  if cnt==cntb==len(S)-1 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)


import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N-1)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  ans+=1
  if S[0]==S[1]:
    S[0]="aaa"
  else:
    S[-2]="aaaa"
    if S[-2]!=S[-3]:
      flag=1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1)

import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  ans+=1
  if S[0]==S[1]:
    S[0]="aaa"
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if S[0]==S[-2]:
  if S[-2]!=S[-3]:
    flag=1
    S[-2]="aaaa"
if flag==0:
	print(ans*N)
else:
  print(ans*N-1)

import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cnt==cntb==len(S)-1 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted
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
input = sys.stdin.readline

s = str(input()).replace('\n', '')
k = int(input())

s = s + s[0]

lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)

    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k
if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2

if s[0] == s[-2] and (lst[0]+lst[-1])%2 == 0:
  answer -= k//2
  

print(answer)

import sys
input = sys.stdin.readline

s = str(input()).replace('\n', '')
k = int(input())

s = s + s[0]

lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)

    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k
if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2
  
if s[0] == s[-2]:
  answer -= k//2
  

print(answer)

import sys
input = sys.stdin.readline
 
s = str(input()).replace('\n', '')
k = int(input())
 
s = s + s[0]
 
lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)
 
    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k

if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2
elif s[0] == s[-2] and lst[-1]%2 == 0 and lst[0]%2 == 0:
  answer -= k-1

print(answer)

Verdicts: Wrong Answer

Wrong Answer

Accepted
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):
from collections import Counter
S=input()
K=int(input())
S_double = S*2
n = 0
if len(set(list(S)))==1:
    print(len(S)*K//2)
    exit()
while n< len(S_double)-1:
    if S_double[n] ==S_double[n+1]:
        S_double = S_double[:n+2]+' '+S_double[n+3:]
    n += 1
ans = Counter(S_double[:len(S)])[' '] + Counter(S_double[len(S):])[' ']*(K-1)
print(ans)


from collections import Counter
S=input()
K=int(input())
S_double = S*2
n = 0
if len(set(list(S)))==1:
    print(len(S)*K//2)
    exit()
while n< len(S_double)-1:
    if S_double[n] ==S_double[n+1]:
        S_double = S_double[:n+1]+' '+S_double[n+2:]
    n += 1
ans = Counter(S_double[:len(S)])[' '] + Counter(S_double[len(S):])[' ']*(K-1)
print(ans)


Verdicts: Wrong Answer

Accepted
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 numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
b=[]
b.append(pp)
b+=a[1:n-2]
    
arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr//2
print(np.sum(a)+(k-1)*np.sum(b))

import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
b=[]
b.append(pp)
b+=a[1:len(s)-1]
arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr2//2
if len(s)>=2:
    print(np.sum(a)+(k-1)*np.sum(b))
else:
    print(k//2)

import numpy as np
s=input()
k=int(input())
cnt=1
a=[]
ss=s+"."
for i in range(len(s)):
    if ss[i]==ss[i+1]:
        cnt+=1
    else:
        a.append(cnt)
        cnt=1

pp=0
if s[0]==s[len(s)-1]:
    pp=a[0]+a[-1]
    
    b=[]
    if len(a)>1:
        b.append(pp)
        b+=a[1:len(a)-1]

    else:
        a=[len(s)*k]
        b=[]

else:
    
    b=a
arr=np.array(a)
arr2=np.array(b)

a=arr//2
b=arr2//2
if len(s)>=2:
    print(np.sum(a)+int((k-1)*np.sum(b)))
else:
    print(k//2)

Verdicts: Runtime Error

Wrong Answer

Accepted
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):
def f(S, K):
    cnt = 0
    p = ''
    for i, s in enumerate(S):
        if p == s:
            cnt += 1
            p = ''
        else:
            p = s
    cnt *= K
    if p == S[0]:
        cnt += K - 1
    return cnt


if __name__ == '__main__':
    S = input()
    K = int(input())
    cnt = 0
    if len(S) % 2 == 1 and K > 1:
        S2 = S * 2
        K2 = K // 2
        cnt = min(f(S2, K2), f(S2[::-1], K2))
        K = 1

    print(cnt + min(f(S, K), f(S[::-1], K)))

def f(S, K):
    if K == 0: return 0
    cnt = 0
    p = ''
    for i, s in enumerate(S):
        if p == s:
            cnt += 1
            p = ''
        else:
            p = s
    cnt *= K
    if p == S[0]:
        cnt += K - 1
    return cnt


if __name__ == '__main__':
    S = input()
    K = int(input())
    cnt1 = 0
    cnt2 = 0

    if len(S) % 2 == 1 and K > 1:
        S2 = S * 2
        K2 = K // 2
        cnt1 = f(S2, K2)

        S2 = S[::-1] * 2
        cnt2 = f(S2, K2)

        K = K % 2
    cnt1 += f(S, K)
    cnt2 += f(S[::-1], K)

    print(min(cnt1, cnt2))

Verdicts: Wrong Answer

Accepted
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):
s = list(input())
k = int(input())
s += '+'+'-'

releki = []
cnt = 1
pre = s[0]
for i in range(1,len(s)):
    if pre==s[i]:
        cnt += 1
    else:
        releki.append([pre,cnt])
        pre = s[i]
        cnt = 1
        
ans = 0
for i in range(len(releki)):
    ans += releki[i][1]//2

if s[0]!=s[-3]:
    print(ans*k)
else:
    a = releki[0][1]
    b = releki[-1][1]
    ans = ans*k - (k-1)*(a//2 +b//2 -(a+b)//2)
    print(ans) 



s = list(input())
k = int(input())
s += '+'

releki = []
cnt = 1
pre = s[0]
for i in range(1,len(s)):
    if pre==s[i]:
        cnt += 1
    else:
        releki.append([pre,cnt])
        pre = s[i]
        cnt = 1
        
ans = 0
for i in range(len(releki)):
    ans += releki[i][1]//2
if len(releki)==1:
    print((len(s)-1)*k//2)
    exit()
if s[0]!=s[-3]:
    print(ans*k)
else:
    a = releki[0][1]
    b = releki[-1][1]
    ans = ans*k - (k-1)*(a//2 +b//2 -(a+b)//2)
    print(ans) 



Verdicts: Wrong Answer

Accepted
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):
S = list(input())
K = int(input())
l = len(S)

if len(set(S)) == 1:
    print(l*K//2)
    exit()
else:
    ans = 0
    tmp = ""
    count = [1]
    for i in range(l-1):
        if S[i] == S[i+1]:
            count[-1] += 1
        else:
            count.append(1)
    
    for i in count:
        ans += i//2*K

    if S[0] == S[-1]:
        ans += K-1

    print(ans)

s = input()
n = int(input())

cnt = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = "1"
    cnt += 1
print(cnt*n)

S = list(input())
K = int(input())
l = len(S)

if len(set(S)) == 1:
    print(l*K//2)
    exit()
else:
    ans = 0
    tmp = ""
    count = [1]
    for i in range(l-1):
        if S[i] == S[i+1]:
            count[-1] += 1
        else:
            count.append(1)
    
    for i in count:
        ans += i//2*K

    if S[0] == S[-1] and count[0]%2 == count[-1]%2:
        ans += K-1

    print(ans)

Verdicts: Wrong Answer

Runtime Error

Accepted
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):
S = list(input()) * 2
K = int(input())
Len_S2 = len(S)
Len_S1 = Len_S2 // 2
count1 = 0
count2 = 0
tmp = all(c == S[0] for c in S[:Len_S1:])
for i in range(Len_S2-1):
    if S[i] == S[i+1]:
        if i < Len_S1 - 1:
            count1 += 1
        if i < Len_S2:
            count2 += 1
        S[i+1] = "1"
diff = count2 - count1
print(diff)
if tmp and K >= 3 and Len_S1 % 2 != 0:
    print((count1 + (diff - 1) * (K-1)) + (1 * (K // 2)))
else:
    print(count1 + diff * (K-1))


S = list(input()) * 2
K = int(input())
Len_S2 = len(S)
Len_S1 = Len_S2 // 2
count1 = 0
count2 = 0
tmp = all(c == S[0] for c in S[:Len_S1:])
for i in range(Len_S2-1):
    if S[i] == S[i+1]:
        if i < Len_S1 - 1:
            count1 += 1
        if i < Len_S2:
            count2 += 1
        S[i+1] = "1"
diff = count2 - count1
if tmp and K >= 3 and Len_S1 % 2 != 0:
    print((count1 + (diff - 1) * (K-1)) + (1 * (K // 2)))
else:
    print(count1 + diff * (K-1))


Verdicts: Wrong Answer

Accepted
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):
S = input()
K = int(input())

count = 0
l = len(S)
check = True
i = 0
while i < l:
    if i + 1 == l:
        if not check:
            if S[0] == S[-1]:
                count += 1
        break
    if S[i] == S[i + 1]:
        count += 1
        i += 2
        if i + 1 == l:
            check = False
    else:
        i += 1

print(count)
print(count * K)


S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l//2 * K)
    exit()

i = ff
c = 0
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 0
    i += 1



print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)


S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l * K // 2)
    exit()

i = ff
c = 1
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 1
    i += 1
count += c // 2



print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)


Verdicts: Wrong Answer

Wrong Answer

Accepted
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):
S=list(input())
K=int(input())

for i in range(len(S)-1):
  if S[i]==S[i+1]:
    continue
else:
  print((len(S)*K)//2)
  exit()

cnt=0
for i in range(len(S)-1):
  if S[i]==S[i+1]:
    S[i+1]='0'
    cnt+=1
#print(S)
cnt2=0
if S[-1]==S[0]:
  cnt2+=1
ans=cnt*K+cnt2*(K-1)
print(ans)

from itertools import groupby

s1=input()
k=int(input())

s2=[[h, len(list(g))] for h, g in groupby(s1)] 

c=0
if len(s2)!=1:
    
    for i in range(len(s2)):
        
        if i!=0 and i!=len(s2)-1:
            c+=(s2[i][1]//2)*k
    
    if s2[0][0]==s2[-1][0]:
        c+=((s2[0][1]+s2[-1][1])//2)*(k-1)
        c+=(s2[0][1]//2+s2[-1][1]//2)
    else:
        c+=(s2[0][1]//2+s2[-1][1]//2)*k

else:
    
    c+=(s2[0][1]*k)//2

print(c)



Verdicts: Wrong Answer

Accepted
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):
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
if len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
if len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[-i] == s[-i-1]:
            cnt3 = cnt3 + 1
    
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
print(ans_fin)

s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
#print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
elif len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
elif len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[len(s)-1-i] == s[len(s)-i-2]:
            cnt3 = cnt3 + 1
        else:
            break
#    print(cnt2,cnt3)
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
    if cnt2 == 0 and cnt3 == 0:
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Verdicts: Wrong Answer

Accepted
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):
s = input()
k = int(input())
sign = 1

num_s = len(list(s))
count = 0
temp_count = 1
count_1 = 0
count_2 = 0

if s[0] != s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      count += temp_count//2
      temp_count = 1
  ans = count*k
  
elif s[0] == s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      if sign == 1:
        count_1 += temp_count
        sign = 0
      elif sign == 0:
        count += temp_count//2
     
    temp_count = 1

  count_2 += temp_count
  
  ans = count*k + (count_1 + count_2)*(k-1)//2 + (count_1 //2) + (count_2//2)

  if sign == 1:
    ans = (k//2)*2*(num_s//2)
    if k%2 == 1:
      ans += num_s//2
    
if num_s == 1:
  ans = k//2
  
print(ans)

s = input()
k = int(input())
sign = 1

num_s = len(list(s))
count = 0
temp_count = 1
count_1 = 0
count_2 = 0

if s[0] != s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      count += temp_count//2
      temp_count = 1
      
  ans = (count+temp_count//2)*k
  
elif s[0] == s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      if sign == 1:
        count_1 = temp_count
        sign = 0
      elif sign == 0:
        count += temp_count//2
     
      temp_count = 1

  count_2 = temp_count
  
  ans = count*k + ((count_1 + count_2)//2)*(k-1) + (count_1//2) + (count_2//2)

  if sign == 1:
    ans = (k*num_s)//2
    
if num_s == 1:
  ans = k//2
  
print(ans)

Verdicts: Wrong Answer

Accepted
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):
from collections import Counter

s = list(input())
k = int(input())

ans = 0
s_d = s*2
for i in range(1,len(s_d)):
    if s[i-1]==s[i]:
        s[i] = '_'
        if i>=len(s)//2:
            ans+=k-1
        else:
            ans+=1
s_s = set(s)
if len(s_s)==1 and len(s)%2!=0:
    ans = len(s)*(k//2)+(k%2==1)*(len(s)//2)
print(ans) 


from collections import Counter

s = list(input())
k = int(input())

ans = 0
s_d = s*2
for i in range(1,len(s_d)):
    if s_d[i-1]==s_d[i]:
        s_d[i] = '_'
        if i>=len(s_d)//2:
            ans+=k-1
        else:
            ans+=1
s_s = set(s)
if len(s_s)==1 and len(s)%2!=0:
    ans = len(s)*(k//2)+(k%2==1)*(len(s)//2)
print(ans) 

Verdicts: Runtime Error

Accepted
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):
S = list(input())
K = int(input())

moji = []

count = 1
for i in range(len(S)-1): 
  if(S[i] == S[i+1]):
    count += 1
  else:
    moji.append(count)
    count = 1
moji.append(count)
print(moji)

ans = 0
if len(moji) == 1:
  ans = (moji[len(moji)-1] * K) //2
elif len(moji) != 1 and S[0] == S[len(S)-1]:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
  ans += ((moji[0] + moji[len(moji)-1]) // 2) * (K-1)
  ans += moji[0] // 2
  ans += moji[len(moji)-1] // 2

else:
  for i in range(len(moji)):
    ans += (moji[i] // 2) * K
    
print(ans)


S = list(input())
K = int(input())

moji = []

count = 1
for i in range(len(S)-1): 
  if(S[i] == S[i+1]):
    count += 1
  else:
    moji.append(count)
    count = 1
moji.append(count)


ans = 0
if len(moji) == 1:
  ans = (moji[len(moji)-1] * K) //2
elif len(moji) != 1 and S[0] == S[len(S)-1]:
  for i in range(1, len(moji)-1):
    ans += (moji[i] // 2) * K
    
  ans += ((moji[0] + moji[len(moji)-1]) // 2) * (K-1)
  ans += moji[0] // 2
  ans += moji[len(moji)-1] // 2

else:
  for i in range(len(moji)):
    ans += (moji[i] // 2) * K
    
print(ans)



Verdicts: Wrong Answer

Accepted
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):
from itertools import groupby
s=input()
k=int(input())
l=len(s)
g=groupby(s)
a=[]
for i,j in g:
  a.append(len(list(j)))
if len(a)==1:
  x=len(s)*k
  if x%2==0:
    print(x//2)
  else:
    print((x-1)//2)
else:
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  if s[0]==s[l-1] and k!=1:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
    z=0
    for i in a:
      if i%2==0:
        t=i//2
      else:
        t=(i-1)//2
      z+=t
    print(z*k+c)
  else:
    print(c*k)  

from itertools import groupby
s=input()
k=int(input())
l=len(s)
g=groupby(s)
a=[]
for i,j in g:
  a.append(len(list(j)))
if len(a)==1:
  x=len(s)*k
  if x%2==0:
    print(x//2)
  else:
    print((x-1)//2)
else:
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  if s[0]==s[l-1] and k!=1:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
    z=0
    for i in a:
      if i%2==0:
        t=i//2
      else:
        t=(i-1)//2
      z+=t
    print(z*(k-1)+c)
  else:
    print(c*k)  

Verdicts: Wrong Answer

Accepted
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):
S=input()
K=int(input())
li=[0]*3
a=True
ans=0
SSS=S+S+S

if K>=2:
    for i in range(3*len(S)-1):
        print(i,li)
        if SSS[i]==SSS[i+1] and a==True:
            a=False
            ans+=1
            li[i//len(S)]+=1
        else:
            a=True
    print(li[0]+li[1]*(K-2)+li[2])
if K==1:
    for i in range(len(S)-1):
        print(i,li)
        if S[i]==S[i+1] and a==True:
            a=False
            ans+=1
            li[i//len(S)]+=1
        else:
            a=True
    print(li[0])


S=input()
K=int(input())
import copy
num=1
liS=[]

for i in range(len(S)-1):
    if S[i]==S[i+1]:
        num+=1
    else:
        liS.append(num)
        num=1
liS.append(num)



liSR=copy.copy(liS)
liSR[0]+=liSR[-1]
del liSR[-1]



if liSR!=[] and S[0]==S[-1]:
    for j in range(len(liS)):
        liS[j]=liS[j]//2
    for k in range(len(liSR)):
        liSR[k]=liSR[k]//2

    print(sum(liSR)*(K-1)+sum(liS))

elif liSR!=[] and S[0]!=S[-1]:
    for j in range(len(liS)):
        liS[j]=liS[j]//2
    print(sum(liS)*K)

else:
    print((sum(liS)*K)//2)



Verdicts: Wrong Answer

Accepted
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):
S = str(input())
K = int(input())
n = len(S)
s = [ ]
for i in range(n):
    s.append(S[i])
s.append(S[0])
t = 0
for i in range(n):
    if s[i] == s[i+1]:
        s[i+1] = A
        t = t + 1
if s[n] == s[0]:
    T = t*K
else:
    if K == 1
    T = (t*K) - 1
print(T)

S = str(input())
K = int(input())
s = [ ]
for i in range(K):
    s.append(S[i])
s.append(S[0])
t = 0
for i in range(K):
    if s[i] == s[i+1]:
        s[i+1] = s_i
        t = t + 1
if s[K] == s[0]:
    T = (t*K) - 1
else:
    T = t*K
print(T)

S = input()
S2 = S + S
K = int(input())
n = len(S2)
i = 0
s = 0
while i < n-1 :
    i += 1
    x = S2[i]
    y = S2[i+1]
    if x == y:
        s += 1
        i += 1
p = s//2
q = s-p
ans = p + q*(K-1)

A = input()
n = len(A)
S = []
R = [A[n-1]]
for i in range(n):
    S.append(A[i])
    R.append(A[i])
K = int(input())
s = 0
p = S[0]
q = S[n-1]
for i in range(n-1):
    x = S[i]
    y = S[i+1]
    if x == y:
        S[i+1] = 1
        s += 1
t = 0
for j in range(n):
    x = R[j]
    y = R[j+1]
    if x == y:
        R[j+1] = 1
        t += 1
if p != q:
    ans = s*K
else:
    if S[n-1] == 1:
        ans = s*K
    else:
        if R[n] == 1:
            k = K//2
            ans = (K-k)*s + k*t
        else:
            ans = s + (K-1)*t
print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
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):
s = input() + "🐧"
k = int(input())

if len(set(s)) == 1:
    print(len(s) * k // 2)
    exit()

wow = 0
yeah = []
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        wow += 1
        yeah.append((s[i], wow))
        wow = 0
    else:
        wow += 1

ans = sum(c // 2 for _, c in yeah) * k

if s[0] == s[-2]:
    katakuna = yeah[0][1]
    ijippari = yeah[-1][1]
    te = katakuna // 2 + ijippari // 2 - (katakuna + ijippari) // 2
    print(ans, te)
    ans -= te * (k - 1)

print(ans)

s = input()
k = int(input())

if len(set(s)) == 1:
    print(len(s) * k // 2)
    exit()

s += "🐧"

wow = 0
yeah = []
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        wow += 1
        yeah.append((s[i], wow))
        wow = 0
    else:
        wow += 1

ans = sum(c // 2 for _, c in yeah) * k

if s[0] == s[-2]:
    katakuna = yeah[0][1]
    ijippari = yeah[-1][1]
    te = katakuna // 2 + ijippari // 2 - (katakuna + ijippari) // 2
    ans -= te * (k - 1)

print(ans)

Verdicts: Wrong Answer

Accepted
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
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    S = input()
    K = int(input())
    if S[0] != S[-1]:
        ans = 0
        before = 0
        i = 0
        while i + 1 < len(S):
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            ans += (i + 1 - before)//2
            i += 1
            before = i
        print(ans*K)
    else:
        if len(set(S)) == 1:
            print(K*len(S)//2)
        else:
            i = 0
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            cut = i + 1
            L = (i + 1)//2
            i += 1
            
            before = 0
            R = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                R += (i + 1 - before)//2
                i += 1
                before = i
            
            S = S[cut:] + S[:cut]
            print(S)
            ans = 0
            before = 0
            i = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                ans += (i + 1 - before)//2
                i += 1
                before = i
            ans = ans*(K - 1) + R + L
            print(ans)
if __name__ == '__main__':
    main()

import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    S = input()
    K = int(input())
    if S[0] != S[-1]:
        ans = 0
        before = 0
        i = 0
        while i + 1 < len(S):
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            ans += (i + 1 - before)//2
            i += 1
            before = i
        print(ans*K)
    else:
        if len(set(S)) == 1:
            print(K*len(S)//2)
        else:
            i = 0
            while i + 1 < len(S) and S[i] == S[i + 1]:
                i += 1
            cut = i + 1
            L = (i + 1)//2
            i += 1
            
            before = i
            R = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                R += (i + 1 - before)//2
                i += 1
                before = i
            
            S = S[cut:] + S[:cut]
            ans = 0
            before = 0
            i = 0
            while i + 1 < len(S):
                while i + 1 < len(S) and S[i] == S[i + 1]:
                    i += 1
                ans += (i + 1 - before)//2
                i += 1
                before = i
            ans = ans*(K - 1) + R + L
            print(ans)
if __name__ == '__main__':
    main()

Verdicts: Wrong Answer

Accepted
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):
def main():
    def f(s):
        n = len(s)

        ret = 0
        i = 0
        while i < n - 1:
            if s[i] == s[i + 1]:
                ret += 1
                i += 2
                continue
            i += 1

        return ret

    s = input()
    k = int(input())

    if k == 1:
        print(f(s))
        return

    d = f(s * 3) - f(s * 2)
    ret = f(s * 1) + d * (k - 1)

    print(ret)

    for x in range(1, 14):
        print(f(s * x), x)


if __name__ == '__main__':
    main()


def main():
    def f(s):
        n = len(s)

        ret = 0
        i = 0
        while i < n - 1:
            if s[i] == s[i + 1]:
                ret += 1
                i += 2
                continue
            i += 1

        return ret

    s = input()
    k = int(input())

    if k == 1:
        print(f(s))
        return

    if k <= 4:
        print(f(s * k))
        return

    d2 = f(s * 4) - f(s * 3)
    d1 = f(s * 3) - f(s * 2)

    q, r = divmod(k - 1, 2)
    ret = f(s * 1) + (d1 + d2) * q + d1 * r

    print(ret)

    # for x in range(1, 14):
    #     print(f(s * x), x)


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted