Problem p02891 (python)

Fixed submission:
S = input().strip()
K = int(input())
if len(S)==1:
    cnt = K//2
else:
    A = []
    cur = 0
    for i in range(1,len(S)):
        if S[i]!=S[i-1]:
            if i-cur>=2:
                A.append([S[i-1],cur,i])
            cur = i
    if S[-1]==S[-2]:
        A.append([S[-1],cur,len(S)])
    cnt = 0
    for j in range(len(A)):
        cnt += (A[j][2]-A[j][1])//2
    if S[0]!=S[-1]:
        cnt = cnt*K
    else:
        if S[0]!=S[1] and S[-1]!=S[-2]:
            cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]!=S[-2]:
            if (A[0][2]-A[0][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]!=S[1] and S[-1]==S[-2]:
            if (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]==S[-2] and len(A)>1:
            if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        else:
            cnt = (len(S)*K)//2
print(cnt)

Buggy submission (verdict: Runtime Error):
S = input().strip()
K = int(input())
T = S*K
N = len(T)
cur = T[0]
cnt = 1
ans = 0
for i in range(1,N):
    if T[i]==cur:
        cnt += 1
    else:
        ans += cnt//2
        cur = T[i]
        cnt = 1
ans += cnt//2
print(ans)

Buggy submission (verdict: Runtime Error):
().strip()
K = int(input())
if len(S)==1:
    cnt = K//2
else:
    A = []
    cur = 0
    for i in range(1,len(S)):
        if S[i]!=S[i-1]:
            if i-cur>=2:
                A.append([S[i-1],cur,i])
            cur = i
    if S[-1]==S[-2]:
        A.append([S[-1],cur,len(S)])
    cnt = 0
    for j in range(len(A)):
        cnt += (A[j][2]-A[j][1])//2
    if S[0]!=S[-1]:
        cnt = cnt*K
    else:
        if S[0]!=S[1] and S[-1]!=S[-2]:
            cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]!=S[-2]:
            if (A[0][2]-A[0][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]!=S[1] and S[-1]==S[-2]:
            if (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]==S[-2]:
            if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
print(cnt)

Buggy submission (verdict: Runtime Error):
S = input().strip()
K = int(input())
A = []
cur = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        if i-cur>=2:
            A.append([S[i-1],cur,i])
        cur = i
if S[-1]==S[-2]:
    A.append([S[-1],cur,len(S)])
cnt = 0
for j in range(len(A)):
    cnt += (A[j][2]-A[j][1])//2
if S[0]!=S[-1]:
    cnt = cnt*K
else:
    if S[0]!=S[1] and S[-1]!=S[-2]:
        cnt = cnt+(cnt+1)*(K-1)
    elif S[0]==S[1] and S[-1]!=S[-2]:
        if (A[0][2]-A[0][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
    elif S[0]!=S[1] and S[-1]==S[-2]:
        if (A[-1][2]-A[-1][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
    elif S[0]==S[1] and S[-1]==S[-2]:
        if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
print(cnt)