Problem p02891 (python)

Fixed submission:
s = input()
k = int(input())

if len(set(list(s)))==1:
    print(len(s)*k//2)

elif s[0] != s[-1]:
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k)
else:
    a,b = 0,0
    c = s[0]
    rev_s = s[::-1]
    for i in range(len(s)):
        if s[i]==c:
            a += 1
        else:
            break
    for i in range(len(s)):
        if rev_s[i]==c:
            b += 1
        else:
            break
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k - (a//2 + b//2 - (a+b)//2)*(k-1))

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

ans = 0

def f(text):
    ret = 0
    if len(set(text))==1:
        return len(text)//2
    else:
        for i in range(len(text)):
            if i==0:
                n=1
                tmp = text[i]
            elif text[i] == tmp:
                n += 1
            else:
                if n!=1:
                    ret += n//2
                tmp = text[i]
                n = 1
        return ret

if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = f(s)
        print(ans*k)
    else:
        c = s[0]
        a, b = 0,0
        ite = 0
        if len(set(s))!=0:
            while s[a] == c:
                a += 1
            while s[len(s)-b-1] == c:
                b += 1
        s1 = s[:a]
        s2 = s[a:len(s)-b]
        s3 = s[len(s)-b:]
        
        A = f(s1)
        B = f(s3)
        C = f(s1+s3)
        X = f(s2)
        
        print(A+B+X*k+C*(k-1))

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

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += n//2
                tmp = s[i]
                n = 1
        print(ans*k)
        exit()
    
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))>=1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

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

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += n//2
                tmp = s[i]
                n = 1
        print(ans*k)
        #exit()
    
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))>=1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

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

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    if s[0] != s[-1]:
        ans = 0
        for i in range(len(s)):
            if i==0:
                n=1
                tmp = s[i]
            elif s[i]==tmp:
                n += 1
            else:
                if n!=1:
                    ans += 1
                tmp = s[i]
                n = 1
        print(ans*k)
        exit()
    a = s[0]
    b = s[-1]
    cnt_a, cnt_b = 0,0
    ite = 0
    
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))==1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    C = (len(s1+s3))//2
    print(A+B+X*k+C*(k-1))

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

edge = 0
ans = 0
if len(set(s))==1:
    s = s*k
    print(len(s)//2)
else:
    a = s[0]
    b = s[-1]
    
    cnt_a, cnt_b = 0,0
    ite = 0
    while s[cnt_a] == a:
        cnt_a += 1
    while s[len(s)-cnt_b-1] == b:
        cnt_b += 1
    
    s1 = s[:cnt_a]
    s2 = s[cnt_a:len(s)-cnt_b]
    s3 = s[len(s)-cnt_b:]
    
    A = cnt_a//2
    B = cnt_b//2    
    X = 0
    if len(set(s2))==1:
        X = len(s2)//2
    else:
        for i in range(len(s2)):
            if i==0:
                n = 1
                tmp = s2[i]
            elif s2[i] == tmp:
                n += 1
            else:
                if n != 1:
                    X += n//2
                tmp = s2[i]
                n = 1
    if a!=b:
        print((A+B+X)*k)
    else:
        C = (len(s1+s3))//2
        print(A+B+X*k+C*(k-1))