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 copy
def main():
    S = input()
    K = int(input())
    a = list(S)
    b = copy.copy(a)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        cnt_2 = 0
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
        for j in range(len(b)-1):
            if b[j+1] == b[j]:
                b[j] = str(j)
                cnt_2 += 1
        cnt_2 *= K
        if b[0] == b[-1] and len(b) > 1:
            cnt_2 += K-1

        print(min(cnt,cnt_3))

    
if __name__ == '__main__':
    main()

def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    print(cnt)
    elif a[0] == a[-1]:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()


def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 0
        temp_2 = 0
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-2):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    for i in range(len(a)-1):
        if a[i+1] == a[i]:
            a[i+1] = str(i)
            cnt += 1
    cnt *= K
    if a[0] == a[-1] and len(a) > 1:
        cnt += K-1
    else:
        cnt += K//2
    print(cnt)

    
if __name__ == '__main__':
    main()

def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)
        temp_4 = 0
        elif temp > 1:
            temp_4 += temp
        elif temp_2 > 2:
            temp_4 += temp_2
        cnt += temp_4 * (K-1)


        print(cnt)

    
if __name__ == '__main__':
    main()

import sys
input = sys.stdin.readline

s = list(input().rstrip())
k = int(input())
n = len(s)

group = []

cnt = 0

from collections import Counter
c = Counter(s)
if len(c) == 1:
    print(n*k//2)
    sys.exit()

for i in range(n):
    if i == 0:
        cnt += 1
        continue
    elif i == n - 1:
        if s[i] == s[i - 1]:
            cnt += 1
            group.append(cnt)
        else:
            group.append(cnt)
            group.append(1)
        continue
    
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        group.append(cnt)
        cnt = 1
ans = 0
if k == 1:
    for i in group:
        ans += i//2
    print(ans)
    sys.exit()
from copy import deepcopy
first = deepcopy(group)
last = deepcopy(group)
if s[0] == s[-1]:
    first[-1] += last[0]
    last[0] = 0

for i in first:
    ans += i//2
for i in last:
    ans += i//2
if k == 2:
    print(ans)
    sys.exit()
mid = deepcopy(first)
if s[0] == s[-1]:
    mid[0] = 0
temp = 0
for i in mid:
    temp += i//2
ans += temp*(k - 2)
print(ans)




Verdicts: Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted