Problem: 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.

Submission attempts (in order):
s = list(input())
k = int(input())
cnt = 0
flow = 0

if len(s) != 1:
    if s[0] == s[-1] and s.count(s[0]) % 2 == 1:
        s[0] = 'A'
    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if s[0] != 'A':
        print(int(cnt * (k / 2)))
    else:
        print(int(cnt * (k / 2) + (k - 1)))

else:
    print(len(s)*k//2)





s = input()
k = int(input())

s_list = []
times = 0
flow = 0


print(s.count(s[len(s)-1]))

for i in range(len(s)):
    s_list.append(s[i])


while True:
    if s_list[flow] == s_list[flow + 1]:
        times += 1
        flow += 2
    elif s_list[flow] != s_list[flow + 1]:
        flow += 1

    if flow >= len(s) - 1:
        break

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)



s = list(input())
k = int(input())
cnt = 0
flow = 0
dummy = 'A'
if len(s) != 1:
    if s[0] == s[-1]:
        s[0] = dummy
    
    if k != 1:
        for i in range(len(s)):
            s.append(s[i])

    for i in range(len(s)):
        if s[flow] == s[flow+1]:
            cnt += 1
            flow += 2
        else:
            flow += 1

        if flow >= len(s)-1:
            break
    if s[0] != dummy:
        print(int(cnt * (k / 2)))
    else:
        print(int(cnt * (k / 2)+(k-1)))
else:
    print(len(s)*k//2)






s = input()
k = int(input())

s_list = []
times = 0
flow = 0



for i in range(len(s)):
    s_list.append(s[i])


while True:
    if s_list[flow] == s_list[flow + 1]:
        times += 1
        flow += 2
    elif s_list[flow] != s_list[flow + 1]:
        flow += 1

    if flow >= len(s) - 1:
        break

if s.count(s[len(s)-1]) % 2 == 1:
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)



s = list(input())
k = int(input())
S = s
cnt = 0
cnt_2 = 0
flow = 0
flow_2 = 0


for i in range(len(s)):
    if s[flow] == s[flow+1]:
        cnt += 1
        flow += 2
    else:
        flow += 1

    if flow >= len(s)-2:
        break
# --------------------------

for i in range(len(s)):
    S.append(s[i])

for i in range(len(S)):
    if S[flow_2] == S[flow_2+1]:
        cnt_2 += 1
        flow_2 += 2
    else:
        flow_2 += 1

    if flow_2 >= len(S)-2:
        break

if k == 1:
    print(cnt)
else:
    print(cnt + (cnt_2 - cnt) * (k-1))


s = input()
k = int(input())

s_list = []
times = 0
flow = 0

#s = s[len(s)-1] + s

print(s.count(s[len(s)-1]))

for i in range(len(s)):
    s_list.append(s[i])


while True:
    if s_list[flow] == s_list[flow + 1]:
        times += 1
        flow += 2
    elif s_list[flow] != s_list[flow + 1]:
        flow += 1

    if flow >= len(s) - 1:
        break

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)



s = input()
k = int(input())

s_list = []
times = 0
flow = 0



for i in range(len(s)):
    s_list.append(s[i])


while True:
    if s_list[flow] == s_list[flow + 1]:
        times += 1
        flow += 2
    elif s_list[flow] != s_list[flow + 1]:
        flow += 1

    if flow >= len(s) - 1:
        break

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)



s = list(input())
k = int(input())
S = s.copy()
S_3 =s.copy()
cnt = 0
cnt_2 = 0
cnt_3 = 0


for i in range(1, len(s)):
    if s[i-1] == s[i]:
        cnt += 1
        s[i] = "A"

# --------------------------

for i in range(len(s)):
    S.append(S[i])

for i in range(1, len(S)):
    if S[i-1] == S[i]:
        cnt_2 += 1
        S[i] = "A"


#     print(cnt)
# else:
#     print(cnt + (cnt_2 - cnt) * (k-1))

# ---------------------------------------

for i in range(len(S)):
    S_3.append(S_3[i])

for i in range(1, len(S_3)):
    if S_3[i-1] == S_3[i]:
        cnt_3 += 1
        S_3[i] = "A"

# --------------------------------------

if k == 1:
    print(cnt)
elif k == 2:
    print(cnt_2)
elif k == 3:
    print(cnt_3)
elif cnt_2 - cnt == cnt_3 - cnt_2:
    print(cnt + (cnt_2 - cnt) * (k-1))
else:
    if k % 2 == 0:
        print(int(cnt + (cnt_2 - cnt) * (k / 2) + (cnt_3 - cnt_2) * (k / 2 - 1)))
    else:
        print(int(cnt + (cnt_2 - cnt) * ((k - 1) / 2) + (cnt_3 - cnt_2) * ((k - 1) / 2)))



Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted