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())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]

    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]

    next = S[1]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    for i in range(0, len(S)-2):
        cnt += 1
        if flg == 1:
            flg = 0
            next = S[i+2]
            continue
        if S[i] == next:
            cnt_list2.append(cnt)
            flg = 1
        next = S[i+2]

    if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
        print(min(len(cnt_list), len(cnt_list2))*K)
    elif S[0] == S[-1]:
        print(min(len(cnt_list), len(cnt_list2))*K+K)
    else :
        print(min(len(cnt_list), len(cnt_list2))*K)

S = list(input())
K = int(input())
 
if len(S) == 1:
    print(K // 2)
else :
    chkd = 0
    flg = 1
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            chkd = 0
            continue
        if S[i] == S[i-1]:
            cnt_list.append(cnt)
            chkd = 1
        else:
            flg = 0
    
    if flg:
        print(len(S)*K // 2)
    elif S[0] == S[-1] and cnt_list[0] % 2 and cnt_list[-1] % 2:
        print(len(cnt_list)*K + K - 1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and ((S[-1] == S[-2] and len(S-2) not in cnt_list) or (S[0] == S[1] and len(2) not in cnt_list)):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())
 
if len(S) == 1:
    print(K // 2)
else :
    chkd = 0
    flg = 1
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            chkd = 0
            continue
        if S[i] == S[i-1]:
            cnt_list.append(cnt)
            chkd = 1
        else:
            flg = 0
    
    if flg:
        print(len(S)*K // 2)
    elif S[0] == S[-1] and cnt_list[0] % 2 and cnt_list[-1] % 2:
        print(len(cnt_list2)*K + K - 1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
        
    S = [::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(len(S)-1-cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K-1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())
 
if len(S) == 1:
    print(K // 2)
else :
    chkd = 0
    flg = 1
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            chkd = 0
            continue
        if S[i] == S[i-1]:
            cnt_list.append(cnt+1)
            chkd = 1
        else:
            flg = 0
    
    if flg:
        print(len(S)*K // 2)
    elif S[0] == S[-1] and cnt_list[0] % 2 and cnt_list[-1] % 2:
        print(len(cnt_list)*K + K - 1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K-1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    S = list(input())
    K = int(input())
    
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]

    print(cnt_list, S*K)
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K-1)
    else :
        print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]

    S = reversed(S)
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
        print(min(len(cnt_list), len(cnt_list2))*K)
    elif S[0] == S[-1]:
        print(min(len(cnt_list), len(cnt_list2))*K+K)
    else :
        print(min(len(cnt_list), len(cnt_list2))*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    S = S[::-1]
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list2 =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list2.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] and len(S)-1 in cnt_list):
        print(len(cnt_list)*K)
    elif S[0] == S[-1] and (S[0] == S[1] and 0 in cnt_list2):
        print(len(cnt_list2)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

S = input()
K = int(input())
 
if len(S) == 1:
    print(K // 2)
    exit()

flg = 1
cnt = 0
cnt_list =[]

for i in range(1, len(S)):
    cnt += 1
    if S[i] == S[i-1]:
        print("ok")
        cnt_list.append(cnt+1)
    else:
        flg = 0

if flg:
    print(len(S)*K // 2)
elif S[0] == S[-1] and cnt_list[0] % 2 and cnt_list[-1] % 2:
    print(len(cnt_list)*K + K - 1)
else :
    print(len(cnt_list)*K)

S = input()
K = int(input())
 
if len(S) == 1:
    print(K // 2)
    exit()

flg = 1
cnt = 0
cnt_list =[]

for i in range(1, len(S)):
    cnt += 1
    if S[i] == S[i-1]:
        cnt_list.append(cnt+1)
    else:
        flg = 0

if flg:
    print(len(S)*K // 2)
elif S[0] == S[-1] and cnt_list[0] % 2 and cnt_list[-1] % 2:
    print(len(cnt_list)*K + K - 1)
else :
    print(len(cnt_list)*K)

S = list(input())
K = int(input())

if len(S) == 1:
    print(K // 2)
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and ((S[-1] == S[-2] and len(S)-2 in cnt_list) or (S[0] == S[1] and 1 in cnt_list)):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

S = input()
K = int(input())
 
if len(S) == 1:
    print(K // 2)
    exit()

chkd, flg = 0, 1
cnt = 0
cnt_list =[]
 
for i in range(1, len(S)):
    cnt += 1
    if S[i] != S[i-1]:
        flg = 0
    if chkd == 1:
        chkd = 0
        continue
    if S[i] == S[i-1]:
        cnt_list.append(cnt)
        chkd = 1
 

S2 = S*2
chkd = 0
cnt = 0
cnt_list2 =[]
 
for i in range(1, len(S2)):
    cnt += 1
    if chkd == 1:
        chkd = 0
        continue
    if S2[i] == S2[i-1]:
        cnt_list2.append(cnt)
        chkd = 1

if flg: 
    print(len(S)*K // 2)
elif S[0] == S[-1]:
    print(len(cnt_list) + (len(cnt_list2)-len(cnt_list)) * (K - 1))
else :
    print(len(cnt_list)*K)

Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted