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 = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i-1]:
            count_b += 1
        else:
            break

    count = count_a + count_b
    change = count // 2
    print(change)
    change_a = count_a // 2
    print(change_a)
    change_b = count_b // 2
    print(change_b)
    if change == change_a + change_b:
        print(result * K)
    else:
        print(result * K + K - 1)





S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:
    m = None
    for i in range(len(S)):

        if i == 0:
            m = S[i]
            continue

        if m == S[i]:
            result += 1
            m = None
            continue

        m = S[i]
    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i]:
            count_b += 1
        else:
            break

    count = count_a + count_b
    change = count // 2
    change_a = count_a // 2
    change_b = count_b // 2
    if change == change_a + change_b:
        print(result * K)
    else:
        print(result * K - 1 )


S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:

    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i-1]:
            count_b += 1
        else:
            break

    T = S[count_a:-count_b]

    m = None
    for i in range(len(T)):

        if i == 0:
            m = T[i]
            continue

        if m == T[i]:
            result += 1
            m = None
            continue

        m = T[i]

    count = count_a + count_b
    change = count // 2
    change_a = count_a // 2
    change_b = count_b // 2

    print(change_a + change_b + (K - 1) * change + K * result)




Verdicts: Wrong Answer

Wrong Answer

Accepted