Problem p02891 (python)

Fixed submission:
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())
ls = len(S)
t1 = []

t = 1
for i in range(1, ls):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1 or t == ls:
    t1.append(t)

ans = 0
for t in t1:
    ans += t // 2

if len(t1) == 1:
    if t1[0] == ls:
        print(ls * K // 2)

else:
    if S[0] != S[-1]:
        print(ans * K)
    
    else:
        ta = t1[0]
        tb = t1[-1]
        print(ans * K - (ta // 2 + tb // 2 - (ta + tb) // 2) * (K - 1))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())
ls = len(S)
t1 = []

t = 1
for i in range(1, ls):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans = 0
for t in t1:
    ans += t // 2

if len(t1) == 1:
    if t1[0] == ls:
        print(ls * K // 2)

else:
    if S[0] != S[-1]:
        print(ans * K)
    
    else:
        ta = t1[0]
        tb = t1[-1]
        print(ans * K - (ta // 2 + tb // 2 - (ta + tb) // 2) * (K - 1))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())
ls = len(S)
t1 = []

t = 1
for i in range(1, ls):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans = 0
for t in t1:
    ans += t // 2

if len(t1) == 1:
    if t1[0] == ls:
        print(ls * K // 2)

else:
    if S[0] != S[1]:
        print(ans * K)
    
    else:
        ta = t1[0]
        tb = t1[-1]
        print(ans * K - (ta // 2 + tb // 2 - (ta + tb) // 2) * (K - 1))

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())

t1 = []

t = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans1 = 0
for t in t1:
    ans1 += t // 2

if K == 1:
    print(ans1)
    exit()

if len(t1) == 1:
    if t1[0] == len(S):
        print(t1[0] * K // 2)
        exit()

ans2 = 0
if S[0] == S[-1]:
    tt = t1.pop(0)
    ttt = t1.pop(len(t1) - 1)
    t = tt + ttt
    t1.append(t)
    for t in t1:
        ans2 += t // 2

else:
    ans2 = ans1 * (K - 1)

print(ans1 + ans2)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3

#import
#import math
#import numpy as np
#= int(input())
S = input()
K = int(input())

t1 = []

t = 1
for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        t += 1
    else:
        if t > 1:
            t1.append(t)
        t = 1

if t > 1:
    t1.append(t)

ans1 = 0
for t in t1:
    ans1 += t // 2

if K == 1:
    print(ans1)
    exit()

if len(t1) == 1:
    if t1[0] == len(S):
        print(t1[0] * K // 2)
        exit()

ans2 = 0
if S[0] == S[-1]:
    t = t1.pop(0) + t1.pop(len(t1) - 1)
    t1.append(t)
    for t in t1:
        ans2 += t // 2

else:
    ans2 = ans1 * (K - 1)

print(ans1 + ans2)