Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
k = int(input())

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

count = []
tmp = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    if s[i] != s[i + 1]:
        count.append(tmp + 1)
        tmp = 0
count.append(tmp + 1)
#print(count)

ans = 0
if s[0] == s[-1]:
    ans += k - 1
    count[-1] -= 1
for i in range(len(count)):
    if count[i] > 1:
        ans += (count[i] // 2) * k
if s[0] == s[-1] and count[-1] >= 1:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
k = int(input())

count = []
tmp = 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    if s[i] != s[i + 1]:
        count.append(tmp + 1)
        tmp = 0
count.append(tmp + 1)
#print(count)

ans = 0
if s[0] == s[-1]:
    ans += k - 1
    count[-1] -= 1
for i in range(len(count)):
    if count[i] > 1:
        ans += (count[i] // 2) * k
if s[0] == s[-1] and count[-1] >= 1:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
if len(S) >= 2:
    ans = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = "?"
            ans += 1
    if len(S) >= 3:
        if S[0] == S[-1]:
            if S[1] == "?" and S[2] != S[0]:
                print(ans*K)
            else:
                print(ans*K + (K-1))
        else:
            print(ans*K)
    else:
        print(ans*K)
        
else:
    print(K//2)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
if len(S) >= 2:
    ans = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = "?"
            ans += 1
    print(ans*K + int(S[0]==S[-1])*(K-1))
else:
    print(K//2)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
if len(S) >= 2:
    ans = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = "?"
            ans += 1
    print(ans*K)
else:
    print(K//2)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        S[i] = "?"
        ans += 1
print(ans*K)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

if len(set(S))==1:
  ans = int(len(S)*K/2)
else:
  cnt = 0
  serial = 1
  for i in range(len(S)-1,1,-1):
    if S[i]==S[i-1]:
      serial += 1
    else:
      break
  #print(serial)
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      serial += 1
    else:
      cnt += int(serial/2)
      serial = 1
    #print(cnt)
      
  #print(cnt)
  if S[0]==S[-1] and S[-1]!=S[-2]:
    ans = cnt*K-1
  
  else:
    ans = cnt*K

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def splitSeries(s:str):
    result = []
    last = ""
    count = 0
    for c in s:
        if last == c:
            count+=1
        else:
            if last != "":
                result.append((last,count))
            last = c
            count = 1
    if last != "":
        result.append((last,count))
    return result



def solve(S: str, K: int):
    if len(S) == 1:
        print(K//2)
    else:
        count = 0
        series = splitSeries(S)
        sp = 0
        if len(series) > 1:
            if series[0][0] == series[-1][0]:
                count += series[0][1]//2 + series[-1][1]//2
                if K > 1:
                    count+= (series[-1][1] + series[0][1])*(K-1)//2
            series = series[1:len(series)-1]
            for s in series:
                if s[1] > 1:
                    sp += s[1]//2
            count += sp * K
            print(count)
        else:
            print(series[0][1]*K//2)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(S, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

def splitSeries(s:str):
    result = []
    last = ""
    count = 0
    for c in s:
        if last == c:
            count+=1
        else:
            if last != "":
                result.append((last,count))
            last = c
            count = 1
    if last != "":
        result.append((last,count))
    return result



def solve(S: str, K: int):
    if len(S) == 1:
        print(K//2)
    else:
        count = 0
        if S[0] == S[-1]:
            count += K
        S = S[1:len(S)-1]
        series = splitSeries(S)
        sp = 0
        for s in series:
            if s[1] > 1:
                sp += s[1]//2
        count += sp * K
        print(count)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(S, K)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt)



def check_odd(string):
    n = len(string)
    left = 1
    right = 1
    
    for i in range(n - 1):
        if (string[i] == string[i + 1]):
            left += 1
        else:
            break
    
    for i in range(n - 1):
        index = n - 1
        if (string[index - i] != string[index - i - 1]):
            
    # for i in reversed(range(1, n)):
        # if (string[i] == string[i - 1]):
            right += 1
        else:
            break
    
    if (right % 2 != 1 and left % 2 != 1):
        return (True)
    else:
        return (False)


count = check_diff(S)


if (len(set(S)) == 1):
    ans = K * N // 2
elif (S[0] != S[N - 1]):
    ans = count * K
elif (not check_odd(S)):
    ans = count * K
else:
    ans = count * K + K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt)



def check_odd(string):
    n = len(string)
    left = 1
    right = 1
    
    for i in range(n - 1):
        if (string[i] == string[i + 1]):
            left += 1
        else:
            break
    
    for i in reversed(range(1, n)):
        if (string[i] == string[i - 1]):
            right += 1
        else:
            break
    
    if (right % 2 != 1 and left % 2 != 1):
        return (True)
    else:
        return (False)


count = check_diff(S)


if (len(set(S)) == 1):
    ans = K * N // 2
elif (S[0] != S[N - 1]):
    ans = count * K
elif (not check_odd(S)):
    ans = count * K
else:
    ans = count * K + K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt)


def check_odd(string):
    n = len(string)
    left = 1
    right = 1
    
    for i in range(n - 1):
        if (string[i] == string[i + 1]):
            left += 1
        else:
            break
    
    for i in reversed(range(0, n - 1)):
        if (string[i] == string[i + 1]):
            right += 1
        else:
            break
    
    if (right % 2 != 1 and left % 2 != 1):
        return (True)
    else:
        return (False)


count = check_diff(S)


if (len(set(S)) == 1):
    ans = K * N // 2
elif (S[0] != S[N - 1]):
    ans = count * K
elif (not check_odd(S)):
    ans = count * K
else:
    ans = count * K + K - 1

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt)


def check_odd(string):
    n = len(string)
    left = 1
    right = 1
    
    for i in range(n - 1):
        if (string[i] == string[i + 1]):
            left += 1
        else:
            break
    
    for i in reversed(range(0, n - 1)):
        if (string[i] == string[i + 1]):
            right += 1
        else:
            break
    
    if (right % 2 != 1 and left % 2 != 1):
        return (True)
    else:
        return (False)


count = check_diff(S)


if (len(set(S)) == 1):
    ans = K * N // 2
elif (S[0] != S[N - 1] or not check_odd(S)):
    ans = count * K
else:
    ans = count * K + K - 1

print(ans)

Submission status: Wrong Answer

Solution code:

S = input()
K = int(input())
N = len(S)

# 全部同じ文字の場合。Sが1文字の場合もここ
if len(set(S)) == 1:
    print((N*K) // 2)
    exit()

# Sの両端の文字が異なるので、Sの中の文字を置き換える回数 * K の場合
if S[0] != S[-1]:
    change = 0
    cnt = 1
    for i in range(1,N):
        if S[i] != S[i-1]:
            change += cnt // 2
            cnt = 1
        else:
            cnt += 1
    ans = change * K
    print(ans)
# 両端の文字が同じなので、Kこ並べた時の結合部分について考えなければならない場合
else:
    change = 0
    head = 1
    tail = 1
    # 先頭の重複を調べる
    for i in range(1,N):
        if S[i] == S[i-1]:
            head += 1
        else:
            break
    # 後ろの重複を調べる
    for i in range(N-2,-1,-1):
        if S[i] == S[i+1]:
            tail += 1
        else:
            break
    cnt = 1
    # 間の部分の変更するべき個数を調べる
    for i in range(head+1, N-tail+1):
        #print("--", i, S[i], S[i-1])
        if S[i] == S[i-1]:
            cnt += 1
        else:
            change += cnt // 2
            cnt = 1

        

    ans = ((head + tail) // 2) * (K - 1) + head // 2 + tail // 2 + change * K
    #print(head, tail, change)
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    l = []
    t = 1
    m = S[0]
    if len(S) == 1:
        print(K//2)
        return
    if len(S) == 2:
        if S[0] == S[1]:
            print(K)
            return
        else:
            print(0)
            return
    if len(S) == 3:
        if S[0] == S[1] == S[2]:
            print(3*K//2)
            return
        elif S[0] == S[1] or S[1] == S[2]:
            print(K)
            return
        elif S[0] == S[2]:
            print(K-1)
            return
        else:
            print(0)
            return
    for v in S[1:]:
        if v == m:
            t += 1
        elif t != 1:
            l.append(t)
            t = 1
        m = v
    if t != 1:
        l.append(t)
    ls = 0
    if S[0] == S[-1]:
        if len(l) == 1 and l[0] == len(S):
            print(l[0] * K//2)
            return
        elif len(l) == 1:
            print(((l[0] + 1)//2) *(K-1) + (l[0] // 2))
            return
        if S[0] == S[1] == S[-2]:
            ls = l[-1] + l[0]
            l = l[1:-1]
        elif S[-1] == S[-2]:
            ls = l[-1] + 1
            l = l[:-1]
        elif S[0] == S[1]:
            ls = l[0] + 1
            l = l[1:]
        else:
            ls = 2
    r = 0
    for i in l:
        if i % 2 == 0:
            r += i // 2
        else:
            r += (i - 1)//2
    r *= K
    if ls % 2 == 0:
        r += (K-1) * (ls // 2)
    else:
        r += (K-1) * ((ls - 1) // 2)
    if ls > 2 and ls % 2 == 0:
        r += ls  // 2
    elif ls > 2:
        r += (ls - 1) // 2
    print(r)
main()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    l = []
    t = 1
    m = S[0]
    if len(S) == 1:
        print(K//2)
        return
    if len(S) == 2:
        if S[0] == S[1]:
            print(K)
            return
        else:
            print(0)
            return
    if len(S) == 3:
        if S[0] == S[1] == S[2]:
            print(3*K//2)
            return
        elif S[0] == S[1] or S[1] == S[2]:
            print(K)
            return
        elif S[0] == S[2]:
            print(K-1)
            return
        else:
            print(0)
            return
    for v in S[1:]:
        if v == m:
            t += 1
        elif t != 1:
            l.append(t)
            t = 1
        m = v
    if t != 1:
        l.append(t)
    ls = 0
    if S[0] == S[-1]:
        if len(l) == 1 and l[0] == len(S):
            print(l[0] * K//2)
            return
        elif len(l) == 1:
            print(((l[0] + 1)//2) *(K-1) + (l[0] // 2))
            return
        if S[0] == S[1] == S[-2]:
            ls = l[-1] + l[0]
            l = l[1:-1]
        elif S[-1] == S[-2]:
            ls = l[-1] + 1
            l = l[:-1]
        elif S[0] == S[1]:
            ls = l[0] + 1
            l = l[1:]
        else:
            ls = 2
    r = 0
    for i in l:
        if i % 2 == 0:
            r += i // 2
        else:
            r += (i - 1)//2
    r *= K
    if ls % 2 == 0:
        r += (K-1) * (ls // 2)
    else:
        r += (K-1) * ((ls - 1) // 2)
    if ls > 2 and ls % 2 == 0:
        r += ls  // 2
    elif ls > 2:
        r += (ls - 1) // 2
    print(ls)
    print(r)
main()

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    l = []
    t = 1
    m = S[0]
    if len(S) == 1:
        print(K//2)
        return
    if len(S) == 2:
        if S[0] == S[1]:
            print(K)
            return
        else:
            print(0)
            return
    if len(S) == 3:
        if S[0] == S[1] == S[2]:
            print(3*K//2)
            return
        elif S[0] == S[1] or S[1] == S[2]:
            print(K)
            return
        elif S[0] == S[2]:
            print(K-1)
            return
        else:
            print(0)
            return
    for v in S[1:]:
        if v == m:
            t += 1
        elif t != 1:
            l.append(t)
            t = 1
        m = v
    if t != 1:
        l.append(t)
    ls = 0
    if S[0] == S[-1]:
        if len(l) == 1:
            print(len(l)*K//2)
            return
        if S[0] == S[1] == S[-2]:
            ls = l[-1] + l[0]
            l = l[1:-1]
        elif S[-1] == S[-2]:
            ls = l[-1] + 1
            l = l[:-1]
        elif S[0] == S[1]:
            ls = l[0] + 1
            l = l[1:]
        else:
            ls = 2
    r = 0
    for i in l:
        if i % 2 == 0:
            r += i // 2
        else:
            r += (i - 1)//2
    r *= K
    if ls % 2 == 0:
        r += (K-1) * (ls // 2)
    else:
        r += (K-1) * ((ls - 1) // 2)
    if ls > 2 and ls % 2 == 0:
        r += ls  // 2
    elif ls > 2:
        r += (ls - 1) // 2
    print(r)
main()

Submission status: Wrong Answer

Solution code:
def main():
    S = [i for i in input()]
    O = S[:]
    K = int(input())
    if len(S) == 1:
        return K // 2
    r = 0
    for i in range(1, len(S)):
        if S[i] == S[i - 1]:
            r += 1
            S[i] = S[i].upper()
    if S[-1] != S[0]:
        return r * K

    S = S + O
    K, k = K // 2, K % 2
    p = 0
    for i in range(len(O), len(S)):
        if S[i] == S[i - 1]:
            p += 1
            S[i] = S[i].upper()
    if S[-1] == S[0]:
        return (r + p) + (r + p + 1) * (K - 1) + k * p
    return (r + p) * K + r * k
   

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    O = S = [i for i in input()]
    K = int(input())
    if len(S) == 1:
        return K // 2
    r = 0
    for i in range(1, len(S)):
        if S[i] == S[i - 1]:
            r += 1
            S[i] = S[i].upper()
    if S[-1] != S[0]:
        return r * K

    S = S + O
    K, k = K // 2, K % 2
    p = 0
    for i in range(len(O), len(S)):
        if S[i] == S[i - 1]:
            p += 1
            S[i] = S[i].upper()
    if S[-1] == S[0]:
        return (r + p) + (r + p + 1) * (K - 1) + k * p
    return (r + p) * K + r * k
   

print(main())

Submission status: Wrong Answer

Solution code:
def main():
    S = [i for i in input()]
    K = int(input())
    if len(S) == 1:
        return K // 2
    r = 0
    for i in range(1, len(S)):
        if S[i] == S[i - 1]:
            r += 1
            S[i] = S[i].upper()
    r *= K
    if S[-1] == S[0]:
        r += K - 1
    return r
   

print(main())

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def count_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt)




def check_odd(string):
    n = len(string)
    cnt = 0
    
    if (len(set(string)) == 1):
        return (False)
    elif (string[0] != string[n - 1]):
        return (False)

    else :
        cnt_left = 1
        cnt_right = 1
        
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt_left += 1                
            else:
                break
        for i in range(0, n, -1):
            if (string[i] == string[i + 1]):
                cnt_right += 1
            else:
                break
        
        if (cnt_left % 2 != 0 and cnt_right % 2 != 0):
            return (True)
        else:
            return(False)


judge = check_odd
cntr = count_diff(S)


if (N == 1):
    ans = K // 2
elif (len(set(S)) > 1 and judge):
    ans = cntr * K + K - 1
else:
    ans = cntr * K

print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt, string)


if (N == 1):
    ans = K // 2
else:
    cntr, tmp_str = check_diff(S)
    S = ''.join(tmp_str)
    
    
    if (S[0] != S[N - 1]):
        ans = cntr * K
    else:
        ans = cntr * K + K - 1


print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                cnt += 1
                tmp = str(cnt % 10)
                string[i + 1] = tmp
    
    return (cnt, string)


if (N == 1):
    ans = K // 2
else:
    cntr, tmp_str = check_diff(S)
    S = ''.join(tmp_str)
    
    
    if (S[0] != S[N - 1]):
        ans = cntr * K
    else:
        ans = cntr * K + K // 2


print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)

def check_diff(string):
    n = len(string)
    cnt = 0
    string = list(string)
    
    
    if (n == 1):
        return (0)
    else:
        for i in range(n - 1):
            if (string[i] == string[i + 1]):
                tmp = str(cnt % 10)
                string[i + 1] = tmp
                cnt += 1
    
    string
    
    return (cnt, string)


if (N == 1):
    ans = K // 2
else:
    cntr, tmp_str = check_diff(S)
    S = ''.join(tmp_str)
    
    
    if (S[0] != S[N - 1]):
        ans = cntr * K
    else:
        ans = cntr * K + K // 2

print(ans)

Submission status: Wrong Answer

Solution code:
S = str(input())
S_plus1 = S + S[0]
K = int(input())
SS = []
SSS = []
if len(S) == 1:
  print(K//2)
  exit()
for i in range(len(S_plus1)):
  SS.append(S_plus1[i])
  SSS.append(S_plus1[i])

for i in range(len(S)):
  if SS[i] == SS[i+1]:
    SS[i+1] = "1"
for i in range(1,len(S_plus1)):
  if SSS[i] == SSS[i-1]:
    SSS[i-1] = "1"
ansS = min(SS.count("1"),SSS.count("1"))

print(ansS*K)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
SS = []
SSS = []
if len(S) == 1:
  print(K//2)
  exit()
for i in range(len(S)):
  SS.append(S[i])
  SSS.append(S[i])

for i in range(len(S)-1):
  if SS[i] == SS[i+1]:
    SS[i+1] = "1"
for i in range(1,len(S)):
  if SSS[i] == SSS[i-1]:
    SSS[i-1] = "1"
ansS = min(SS.count("1"),SSS.count("1"))

if S[0] == S[-1] and S[-1] != S[-2]:
  print(ansS.count("1")*K-1)
else:
  print(ansS*K)

Submission status: Wrong Answer

Solution code:
# Author: cr4zjh0bp
# Created: Sat Mar 14 20:41:10 UTC 2020
import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

from collections import Counter

s = ns()
n = len(s)
s = s + "$"
k = ni()

c = []
cnt = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    elif cnt > 1:
        c.append(cnt)
        cnt = 1

if len(c) == 1:
    print(n * k // 2)
elif s[0] != s[-1]:
    print(sum([x // 2 for x in c[:]]) * k)
else:
    a, b = 1, 1
    if s[0] == s[1]:
        a = c[0]
    if s[-1] == s[-2]:
        b = c[-1]
    print(sum([x // 2 for x in c[:]]) * k - (a // 2 + b // 2 - (a + b) // 2))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
h=1
b=0
l=[]
f=False
c1=0
c2=0
c3=0
c4=0
r=[]
v=[]
vf=False
vf1=False
vf2=False
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        if i==0:
            c1=1
            c3=1
        if (i+1)==(len(S)-1):
            c2=1
            c4=1
        h+=1
        f=True
    else:
        f=False
    if not f:
        if h!=1:
            l.append(h)
            if c3==1:
                r.append(h)
                c3=0
            if c4==1:
                r.append(h)
                c4=0
        h=1
#print(l)
if f:
    l.append(h)
    #print(l)
    if c4==1:
        r.append(h)
        c4=0
#print(l)
if S[0]==S[-1]:
    if c1==1 and c2==1:
        if len(l)==1:
            s=len(S)*K
            print(s//2)
            exit()
        else:
            v.append(l[0])
            v.append(l[-1])
            l[-1]+=l[0]
            del l[0]
            vf=True
    elif c1==1:
        #l[0]+=1
        v.append(l[0])
        #v.append(l[-1])
        l[0]+=1
        v.append(l[0])
        del l[0]
        vf1=True
    elif c2==1:
        #l[-1]+=1
        #v.append(l[0])
        v.append(l[-1])
        l[-1]+=1
        v.append(l[-1])
        del l[-1]
        vf2=True
    else:
        print(K-1)
        exit()
h=0
if vf:
    h+=v[0]//2
    h+=v[-1]//2
    for i in range(len(l)):
        if i==(len(l)-1):
            h+=(l[i]//2)*(K-1)
        else:
            h+=(l[i]//2)*K
elif vf1:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
elif vf2:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
else:
    for i in range(len(l)):
        h+=(l[i]//2)*K

#print(l)
print(h)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K // 2
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(i, S*i, count(S*i))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    # ans = (K + 1) // 2
    ans = K // 2
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(i, S*i, count(S*i))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
h=1
b=0
l=[]
f=False
c1=0
c2=0
c3=0
c4=0
r=[]
v=[]
vf=False
vf1=False
vf2=False
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        if i==0:
            c1=1
            c3=1
        if (i+1)==(len(S)-1):
            c2=1
            c4=1
        h+=1
        f=True
    else:
        f=False
    if not f:
        if h!=0:
            l.append(h)
            if c3==1:
                r.append(h)
                c3=0
            if c4==1:
                r.append(h)
                c4=0
        h=1
#print(l)
if f:
    l.append(h)
    #print(l)
    if c4==1:
        r.append(h)
        c4=0
if S[0]==S[-1]:
    if c1==1 and c2==1:
        if len(l)==1:
            s=len(S)*K
            print(s//2)
            exit()
        else:
            v.append(l[0])
            v.append(l[-1])
            l[-1]+=l[0]
            del l[0]
            vf=True
    elif c1==1:
        #l[0]+=1
        v.append(l[0])
        #v.append(l[-1])
        l[0]+=1
        v.append(l[0])
        del l[0]
        vf1=True
    elif c2==1:
        #l[-1]+=1
        #v.append(l[0])
        v.append(l[-1])
        l[-1]+=1
        v.append(l[-1])
        del l[-1]
        vf2=True
h=0
if vf:
    h+=v[0]//2
    h+=v[-1]//2
    for i in range(len(l)):
        if i==(len(l)-1):
            h+=(l[i]//2)*(K-1)
        else:
            h+=(l[i]//2)*K
elif vf1:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
elif vf2:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
else:
    for i in range(len(l)):
        h+=(l[i]//2)*K

#print(l)
print(h)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) // 2
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(S*i, count(S*i))

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            if c != 1:
                a.append(c)
            c = 1
        b = si
    if c != 1:
        a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    # print(s, a, r)
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = (K + 1) // 2
elif K == 2:
    ans = count(S*2)
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

# for i in range(1, 10):
#     print(S*i, count(S*i))

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
h=1
b=0
l=[]
f=False
c1=0
c2=0
c3=0
c4=0
r=[]
v=[]
vf=False
vf1=False
vf2=False
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        if i==0:
            c1=1
            c3=1
        if (i+1)==(len(S)-1):
            c2=1
            c4=1
        h+=1
        f=True
    else:
        f=False
    if not f:
        if h!=0:
            l.append(h)
            if c3==1:
                r.append(h)
                c3=0
            if c4==1:
                r.append(h)
                c4=0
        h=1
#print(l)
if f:
    l.append(h)
    #print(l)
    if c4==1:
        r.append(h)
        c4=0
if S[0]==S[-1]:
    if c1==1 and c2==1:
        if len(l)==1:
            pass
        else:
            v.append(l[0])
            v.append(l[-1])
            l[-1]+=l[0]
            del l[0]
            vf=True
    elif c1==1:
        #l[0]+=1
        v.append(l[0])
        #v.append(l[-1])
        l[0]+=1
        v.append(l[0])
        del l[0]
        vf1=True
    elif c2==1:
        #l[-1]+=1
        #v.append(l[0])
        v.append(l[-1])
        l[-1]+=1
        v.append(l[-1])
        del l[-1]
        vf2=True
h=0
if vf:
    h+=v[0]//2
    h+=v[-1]//2
    for i in range(len(l)):
        if i==(len(l)-1):
            h+=(l[i]//2)*(K-1)
        else:
            h+=(l[i]//2)*K
elif vf1:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
elif vf2:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
else:
    for i in range(len(l)):
        h+=(l[i]//2)*K

#print(l)
print(h)