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

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

def Scount():
    cnt_S = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = "1"
            cnt_S += 1
    return cnt_S

if K == 1:
    ans = Scout()

else:
    T = copy.copy(S)
    T.insert(0, T[len(T)-1])
    cnt_T = 0
    for i in range(len(T)-1):
        if T[i] == T[i+1]:
            T[i+1] = "1"
            cnt_T += 1

    if T[i+1] != "1":
        ans = Scount() + (cnt_T * (K-1))
    else:
        TT = S * 2
        cnt_T = 0
        for i in range(len(TT)-1):
            if TT[i] == TT[i+1]:
                TT[i+1] = "1"
                cnt_T += 1
        if K % 2 == 0:
            ans = cnt_T * K / 2
        else:
            ans = (cnt_T * K // 2) + Scount()

print(ans)

import copy

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

def Scount():
    cnt_S = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = "1"
            cnt_S += 1
    return cnt_S

T = copy.copy(S)
T.insert(0, T[len(T)-1])
cnt_T = 0
for i in range(len(T)-1):
    if T[i] == T[i+1]:
        T[i+1] = "1"
        cnt_T += 1

if T[i+1] != "1":
    ans = Scount() + (cnt_T * (K-1))
else:
    TT = S * 2
    cnt_T = 0
    for i in range(len(TT)-1):
        if TT[i] == TT[i+1]:
            TT[i+1] = "1"
            cnt_T += 1
    if K % 2 == 0:
        ans = cnt_T * K / 2
    else:
        ans = (cnt_T * K // 2) + Scount()
    
print(ans)

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

def Scount():
    cnt_S = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] == "1"
            cnt_S += 1
    return cnt_S

T = S
T.insert(0, T[len(T)-1])
cnt_T = 0
for i in range(len(T)-1):
    if T[i] == T[i+1]:
        T[i+1] == "1"
        cnt_T += 1

if T[i+1] != "1":
    ans = Scount() + (cnt_T * (K-1))
else:
    T = S * 2
    cnt_T = 0
    for i in range(len(T)-1):
        if T[i] == T[i+1]:
            T[i+1] == "1"
            cnt_T += 1
    if K % 2 == 0:
        ans = cnt_T * K / 2
    else:
        ans = (cnt_T * K // 2) + Scount()

print(ans)

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

T = list(S)
T.insert(0, T[len(T)-1])
count = 0
for i in range(len(T)-1):
    if T[i] == T[i+1]:
        T[i+1] == "1"
        count += 1

if T[len(T)-1] != "1":
    t = list(S)
    cnt = 0
    for i in range(len(t)-1):
        if t[i] == t[i+1]:
            t[i+1] == "1"
            cnt += 1
    print(cnt + count * (K-1))

else:
    t = list(S * 2)
    cnt = 0
    for i in range(len(t)-1):
        if t[i] == t[i+1]:
            t[i+1] == "1"
            cnt += 1
    if K % 2 == 0:
        print(cnt * K / 2)
    else:
        tt = list(S)
        ccnt = 0
        for i in range(len(tt)-1):
            if tt[i] == tt[i+1]:
                tt[i+1] == "1"
                ccnt += 1
        print(ccnt + cnt * K // 2)

import copy

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

def count(L):
    cnt = 0
    for i in range(len(L)-1):
        if L[i] == L[i+1]:
            L[i+1] = "1"
            cnt += 1
    return cnt

T = copy.copy(S)
T.insert(0, T[len(T)-1])
cnt_T = count(T)

if T[-1] != "1":
    ans = count(S) + (cnt_T * (K-1))
else:
    TT = S * 2
    cnt_TT = count(TT)
    if K % 2 == 0:
        ans = cnt_TT * (K // 2)
    else:
        ans = (cnt_TT * (K // 2)) + count(S)

print(ans)

Verdicts: Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted