Problem p02891 (python)

Fixed submission:
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 0):
        cnt = 0
    elif (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        if (len(l) == 1):
            cnt += l[0][-1] * k // 2
        elif (len(l) == 2):
            cnt += l[0][-1] // 2
            cnt += l[1][-1] // 2
            cnt *= k
        else:
            edge1 = l[0]
            edge2 = l[-1]
            mid = l[1:-1]
            for e in mid:
                cnt += e[1] // 2 * k
            if (edge1[0] == edge2[0]):
                cnt += (edge1[1] + edge2[1]) // 2 * (k-1)
            else:
                cnt += (edge1[1]) // 2 * (k-1)
                cnt += (edge2[1]) // 2 * (k-1)
            cnt += edge1[1] // 2
            cnt += edge2[1] // 2

    print(cnt, end='')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        for e in l:
            cnt += math.floor(e[1]/2)
        if (l[0][0] == l[-1][0]):
            p = l.pop()
            l[0][1] += p[1]
            if (p[1] == 6):
                cnt -= 1
        cnt *= k
    print(cnt, end='')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        for e in l:
            cnt += math.floor(e[1]/2)
        if (l[0][0] == l[-1][0]):
            p = l.pop()
            l[0][1] += p[1]
        cnt *= k
    print(cnt, end='')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
def main():
    s = input()
    k = int(input())
    s = list(s*k)
    cnt = 0
    for i in range(0, len(s), 2):
        target = s[i:i+3]
        if (len(target) == 3):
            if (target[0] == target[1] == target[2]):
                cnt += 1
                s[i+1] = '0'
            if (target[0] != target[1] == target[2]):
                cnt += 1
                s[i+2] = '0'
            if (target[0] == target[1] != target[2]):
                cnt += 1
        else:
            if (len(target) == 2):
                if (target[0] == target[1]):
                    cnt += 1
    print(cnt, end='')


if __name__ == '__main__':
    main()