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 sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

for i in range(lenS):
    mo = S[0]*lenS
    if mo == S:
        ans = lenS*K//2
        print(ans)
        exit()
    else:
        pass

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


print(ans)



from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


print(ans)



from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

for i in range(lenS):
    mo = S[0]*lenS
    if mo == S:
        ans = len(mo)//2
        print(ans)
        exit()
    else:
        pass

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


print(ans)



from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

l = len(S)
ans = 0

for i in range(l):
    mo = S[0]*l
    if mo == S:
        ans = l*K//2
        print(ans)
        exit()
    else:
        pass


if S[0] == S[l-1]:
    S2 = S*2
    l2 = len(S2)
    a = 0
    ans2 = 0
    while a < l2-1:
        if S2[a] == S2[a+1]:
            t = S2[a]
            c = 1
            for i in range(1,l2-a):
                if t == S2[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans2 += j
            a += c
        else:
            a += 1
    a = 0
    ans1 = 0
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans1 += j
            a += c
        else:
            a += 1
    differ = ans2-ans1*2
    ans = differ*(K-1) + ans1*K
else:
    a = 0
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans += j
            a += c
        else:
            a += 1

    ans = ans*K

print(ans)


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted