Problem p02891 (python)

Fixed submission:
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print((n*k)//2)
else:
    arr = []
    cnt = 0
    last = s[0]
    for i in s + '0':
        if i != last:
            arr.append(cnt)
            cnt = 1
            last = i
        else:
            cnt += 1
    ans = 0
    if s[0] == s[-1]:
        ans += (arr[0] + arr[-1])//2*(k-1) + arr[0]//2 + arr[-1]//2
        for i in arr[1:-1]:
            ans += (i//2)*k
    else:
        for i in arr:
            ans += (i//2)*k
    print(ans)

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
elif k == 2:
    s += s
    cnt1 = 0
    cnt2 = 0
    for i in range(1, 2*n-1):
        if i % 2 == 0:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt1 += 1
        else:
            if s[i] == s[i-1] or s[i] == s[i+1]:
                cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s *= 3
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
                    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s2[n:2*n].count('0'), s2[2*n:].count('0')]
    print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
elif k == 2:
    s *= 2
    cnt1 = 0
    cnt2 = 0
    for i in range(2*n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == 2*n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s *= 3
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
                    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s2[n:2*n].count('0'), s2[2*n:].count('0')]
    print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
elif k == 2:
    s += s
    cnt1 = 0
    cnt2 = 0
    for i in range(2*n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == 2*n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s += 2*s
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
                    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s2[n:2*n].count('0'), s2[2*n:].count('0')]
    print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)

if k == 1:
    cnt1 = 0
    cnt2 = 0
    for i in range(n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == 2*n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
elif k == 2:
    s += s
    cnt1 = 0
    cnt2 = 0
    for i in range(2*n):
        if i == 0:
            if s[i] == s[i+1]:
                cnt1 += 1
        elif i == 2*n-1:
            if i%2 == 0:
                if s[i] == s[i-1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1]:
                    cnt2 += 1
        else:
            if i%2 == 0:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt1 += 1
            else:
                if s[i] == s[i-1] or s[i] == s[i+1]:
                    cnt2 += 1
    print(min(cnt1, cnt2))
else:
    s += 2*s
    s1 = ''
    s2 = ''
    for i in range(3*n):
        if i < 3*n - 1 and i > 0:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1] or s[i] == s[i + 1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        elif i == 3*n - 1:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i-1]:
                    s1 += '0'
                else:
                    s1 += s[i]
            else:
                s1 += s[i]
                if s[i] == s[i-1]:
                    s2 += '0'
                else:
                    s2 += s[i]
        else:
            if i % 2 == 0:
                s2 += s[i]
                if s[i] == s[i + 1]:
                    s1 += '0'
                else:
                    s1 += s[i]
                    
    s1_arr = [s1[:n].count('0'), s1[n:2*n].count('0'), s1[2*n:].count('0')]
    s2_arr = [s2[:n].count('0'), s2[n:2*n].count('0'), s2[2*n:].count('0')]
    print(min(s1_arr[0] + s1_arr[1]*(k-2) + s1_arr[2], s2_arr[0] + s2_arr[1]*(k-2) + s2_arr[2]))

Buggy submission (verdict: Runtime Error):
s = input()
k = int(input())
n = len(s)
s1 = s[0]
cnt1 = 0
s2 = s[0]
cnt2 = 0
for i in range(1,n):
    if i%2 == 1:
        s1 += s[i]
        continue
    elif s[i-1] == s[i] or s[i] == s[i + 1]:
        s1 += '0'
        cnt1 += 1
    else:
        s1 += s[i]

for i in range(1,n):
    if i%2 == 0:
        s2 += s[i]
        continue
    elif s[i-1] == s[i] or (i < n-1 and s[i] == s[i + 1]):
        s2 += '0'
        cnt2 += 1
    else:
        s2 += s[i]
s1 += s1
s2 += s2
jdg = [True,True]
for i in range(1,2*n):
    if s1[i] == s1[i-1]:
        jdg[0] = False
    if s2[i] == s2[i-1]:
        jdg[1] = False
if jdg[0] and jdg[1]:
    print(min(cnt1,cnt2)*k)
elif jdg[0]:
    print(cnt1*k)
else:
    print(cnt2*k)