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:
s = input()
k = int(input())
li = list(s)
lee = len(s)

if lee == 1:
    print(k-1)
    exit()

if len(list(set(li))) == 1:
    print((lee)*k//2)
    exit()


def motome(n,list):
    count = 0
    kaihi_flag = False
    li = list*n
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    #print(count*k)
    return count

if k >= 5 and k%2 == 1:
    a = motome(3,li)
    b = motome(5,li)
    diff = b - a
    ans = b + (k-5)//2*diff
    print(ans)
elif k >=5 and k%2 == 0:
    a = motome(2,li)
    b = motome(4,li)
    diff = b - a
    ans = b + (k-4)//2*diff
    print(ans)
else:
    count = 0
    kaihi_flag = False
    li = li*k
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    #print(count*k)
    print(count)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
li = list(s)
lee = len(s)

if lee == 1:
    print(k-1)
    exit()

if len(list(set(li))) == 1:
    print((lee)*k//2)
    exit()

if li[0] != li[-1]:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    print(count*k)
elif li[0] == li[-1] and lee%2 == 1 and k%2 == 1:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+(k-1)//2)
else:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+k-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
li = list(s)
lee = len(s)

if lee == 1:
    print(k-1)
    exit()

if len(list(set(li))) == 1:
    print((lee-1)*k)
    exit()

if li[0] != li[-1]:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    print(count*k)
elif li[0] == li[-1] and lee%2 == 1 and k%2 == 1:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+(k-1)//2)
else:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+k-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
li = list(s)
lee = len(s)

if lee == 1:
    print(k-1)
    exit()

if li[0] != li[-1]:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    print(count*k)
elif li[0] == li[-1] and lee%2 == 1 and k%2 == 1:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+(k-1)//2)
else:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print('kaihi_flag',kaihi_flag)
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+k-1)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
li = list(s)
lee = len(s)

if lee == 1:
    print(k-1)
    exit()

if li[0] != li[-1]:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    #print(count)
    print(count*k)
else:
    count = 0
    kaihi_flag = False
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    if kaihi_flag:
        print(count*k)
    else:
        print(count*k+k-1)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
if len(s)==1:
  print(k//2)
  exit()
c=1
ans=0
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    c+=1
  else:
    ans+=c//2
    c=1
ans+=c//2
ans*=k
cl=0
i=0
while  i<len(s) and s[-1]==s[i]:
  cl+=1
  i+=1
if cl%2==1 and c%2==1:
  ans+=k-1
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
c=1
ans=0
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    c+=1
  else:
    ans+=c//2
    c=1
ans+=c//2
ans*=k
cl=0
i=0
while  i<len(s) and s[-1]==s[i]:
  cl+=1
  i+=1
if cl%2==1 and c%2==1:
  ans+=k-1
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if S[0] == S[length-1]:
        # ans = num_correct * K + (K - 1)
        # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
        # 最短は, correct_S = _aba_a で ans = 4
        # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
        # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
        head_length = 1
        head_char = S[0]
        for i in range(1, length):
            now_char = S[i]
            if now_char != head_char:
                break
            head_length += 1
        tail_length = 1
        tail_char = S[length - 1]
        for i in range(1, length):
            now_char = S[length - 1 - i]
            if now_char != tail_char:
                break
            tail_length += 1
        # print("head_length: {}, tail_length: {}".format(head_length, tail_length))
        if length == head_length == tail_length:
            # print("length == head_length == tail_length")
            ans = length * num_correct * K
        else:
            # print("not length == head_length == tail_length")
            ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)

num_fixed = 0
pre_char = ''
count = 0
for i in range(length):
    now_char = S[i]
    if pre_char != now_char:
        num_fixed += count//2
        count = 0
    count += 1
    pre_char = now_char
    # print("pre_char:{}, now_char:{}, count:{}".format(pre_char, now_char, count))
num_fixed += count//2

# print(num_fixed)

if S[0] == S[length - 1]:
    head_length = 1
    head_char = S[0]
    for i in range(1, length):
        now_char = S[i]
        if now_char != head_char:
            break
        head_length += 1
    tail_length = 1
    tail_char = S[length - 1]
    for i in range(1, length):
        now_char = S[length - 1 - i]
        if now_char != tail_char:
            break
        tail_length += 1
    if length == head_length == tail_length:
        ans = num_fixed * K
    else:
        ans = K * num_fixed - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
else:
    ans = num_fixed * K

print(ans)

Submission status: Wrong Answer

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

length = len(S)

num_correct = 0
pre_char = ''
count = 0
for i in range(length):
    now_char = S[i]
    if pre_char != now_char:
        num_correct += count//2
        count = 0
    else:
        count += 1
    pre_char = now_char
num_correct += count//2

# print(num_correct)

if S[0] == S[length - 1]:
    head_length = 1
    head_char = S[0]
    for i in range(1, length):
        now_char = S[i]
        if now_char != head_char:
            break
        head_length += 1
    tail_length = 1
    tail_char = S[length - 1]
    for i in range(1, length):
        now_char = S[length - 1 - i]
        if now_char != tail_char:
            break
        tail_length += 1
    if length == head_length == tail_length:
        ans = num_correct * K
    else:
        ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
else:
    ans = num_correct * K

print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))


if S[0] == S[length-1]:
    # ans = num_correct * K + (K - 1)
    # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
    # 最短は, correct_S = _aba_a で ans = 4
    # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
    # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
    head_length = 1
    head_char = S[0]
    for i in range(1, length):
        now_char = S[i]
        if now_char != head_char:
            break
        head_length += 1
    tail_length = 1
    tail_char = S[length - 1]
    for i in range(1, length):
        now_char = S[length - 1 - i]
        if now_char != tail_char:
            break
        tail_length += 1
    # print("head_length: {}, tail_length: {}".format(head_length, tail_length))
    if length == head_length == tail_length:
        # print("length == head_length == tail_length")
        ans = num_correct * K
    else:
        # print("not length == head_length == tail_length")
        ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
else:
    ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))


if S[0] == S[length-1]:
    # ans = num_correct * K + (K - 1)
    # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
    # 最短は, correct_S = _aba_a で ans = 4
    # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
    # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
    head_length = 1
    head_char = S[0]
    for i in range(1, length):
        now_char = S[i]
        if now_char != head_char:
            break
        head_length += 1
    tail_length = 1
    tail_char = S[length - 1]
    for i in range(1, length):
        now_char = S[length - 1 - i]
        if now_char != tail_char:
            break
        tail_length += 1
    # print("head_length: {}, tail_length: {}".format(head_length, tail_length))
    if length == head_length == tail_length:
        # print("length == head_length == tail_length")
        ans = num_correct * K
    else:
        # print("not length == head_length == tail_length")
        ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
else:
    ans = num_correct + K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if S[0] == S[length-1]:
        # ans = num_correct * K + (K - 1)
        # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
        # 最短は, correct_S = _aba_a で ans = 4
        # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
        # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
        head_length = 1
        head_char = S[0]
        for i in range(1, length):
            now_char = S[i]
            if now_char != head_char:
                break
            head_length += 1
        tail_length = 1
        tail_char = S[length - 1]
        for i in range(1, length):
            now_char = S[length - 1 - i]
            if now_char != tail_char:
                break
            tail_length += 1
        # print("head_length: {}, tail_length: {}".format(head_length, tail_length))
        if length == head_length == tail_length:
            # print("length == head_length == tail_length")
            ans = num_correct * K
        else:
            # print("not length == head_length == tail_length")
            ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if correct_S[0] == correct_S[length-1]:
        # ans = num_correct * K + (K - 1)
        # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
        # 最短は, correct_S = _aba_a で ans = 4
        # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
        # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
        head_length = 1
        head_char = S[0]
        for i in range(1, length):
            now_char = S[i]
            if now_char != head_char:
                break
            head_length += 1
        tail_length = 1
        tail_char = S[length - 1]
        for i in range(1, length):
            now_char = S[length - 1 - i]
            if now_char != tail_char:
                break
            tail_length += 1
        # print("head_length: {}, tail_length: {}".format(head_length, tail_length))
        if length == head_length == tail_length:
            ans = num_correct * K + (K - 1)
        else:
            ans = K * num_correct - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if correct_S[0] == correct_S[length-1]:
        # ans = num_correct * K + (K - 1)
        # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
        # 最短は, correct_S = _aba_a で ans = 4
        # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
        # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
        head_length = 1
        head_char = S[0]
        for i in range(1, length):
            now_char = S[i]
            if now_char != head_char:
                break
            head_length += 1
        tail_length = 1
        tail_char = S[length - 1]
        for i in range(1, length):
            now_char = S[length - 1 - i]
            if now_char != tail_char:
                break
            tail_length += 1
        if length == head_length == tail_length:
            ans = num_correct * K + (K - 1)
        else:
            ans = K * num_correct + (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if correct_S[0] == correct_S[length-1]:
        # ans = num_correct * K + (K - 1)
        # S = aabaaa, K = 2 の場合, ans = 5, correct_S = a_ba_a となってしまう
        # 最短は, correct_S = _aba_a で ans = 4
        # S = aaabaa, K = 2 の場合, ans = 4, correct_S = a_aba_ となり最短っぽい
        # => S[0] == S[len(S)-1] なら, 先頭と最後尾で連続するそれぞれの文字数で計算が必要
        head_length = 1
        head_char = S[0]
        for i in range(1, length):
            now_char = S[i]
            if now_char != head_char:
                break
            head_length += 1
        tail_length = 1
        tail_char = S[length - 1]
        for i in range(1, length):
            now_char = S[length - 1 - i]
            if now_char != tail_char:
                break
            tail_length += 1
        if length == head_length == tail_length:
            ans = num_correct + K + (K - 1)
        else:
            ans = K * num_correct + (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = K // 2
else:
    if correct_S[0] == correct_S[length-1]:
        ans = num_correct * K + (K - 1)
    else:
        ans = num_correct * K
# print(correct_S*K)
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
correct_S = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
    correct_S += pre_char
# print("num_correct:{}".format(num_correct))
# print("correct_S:{}".format(correct_S))

if length == 1:
    ans = length // 2
else:
    if correct_S[0] == correct_S[length-1]:
        ans = num_correct * K + (K - 1)
    else:
        ans = num_correct * K
print(ans)

Submission status: Wrong Answer

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

length = len(S)
num_correct = 0
pre_char = S[0]
for i in range(1, length):
    now_char = S[i]
    if pre_char == now_char:
        num_correct += 1
        pre_char = '-'
    else:
        pre_char = now_char
# print("num_correct:{}".format(num_correct))
if length == 1:
    ans = length // 2
else:
    if S[0] == S[length-1] and length >= 3 and S[length-1] != S[length-2]:
        num_correct += 1
    ans = num_correct * K
print(ans)

Submission status: Wrong Answer

Solution code:
def f(s,n):
    res = 0
    flg = False
    for i in range(1,n):
        if flg:
            flg = False
            continue
        if S[i] == S[i-1]:
            flg = True
            res += 1
    return res
S = input()
K = int(input())
ans = f(S,len(S))
S = S * 2
print(ans + (f(S,len(S)) - ans) * (K-1))

Submission status: Wrong Answer

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

curr = S[0]
tmp = ""
T = []
for s in S:
    if s == curr:
        tmp += s
    else:
        T.append(tmp)
        curr = s
        tmp = s
T.append(tmp)

if len(T) == 1:
    res = (len(S) * K) // 2
    exit(0)

res = 0
t_first = T[0]
t_last = T[-1]

if t_first[0] != t_last[0]:
    res += (len(t_first) // 2) * K
    res += (len(t_last) // 2) * K
else:
    res += len(t_first) // 2
    res += len(t_last) // 2
    res += (len(t_first + t_last) // 2) * (K - 1)

for t in T[1:-1]:
    res += (len(t) // 2) * K

print(res)

Submission status: Wrong Answer

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

curr = S[0]
tmp = ""
T = []
for s in S:
    if s == curr:
        tmp += s
    else:
        T.append(tmp)
        curr = s
        tmp = s
T.append(tmp)

if len(T) == 1:
    t = T[0]
    res = 0
    if len(t) == 1:
        res = K // 2
    elif len(t) == 2 and t[0] != t[1]:
        res = 0
    elif len(t) == 2 and t[0] == t[1]:
        res = K
    elif len(t) % 2 == 0:
        res = (len(t) // 2) * K
    else:
        res = (len(t) // 2) * K
        res += K - 1
    print(res)
    exit(0)

res = 0

t_first = T[0]
t_last = T[-1]

if t_first[0] != t_last[0]:
    res += (len(t_first) // 2) * K
    res += (len(t_last) // 2) * K
else:
    res += len(t_first) // 2
    res += len(t_last) // 2
    res += (len(t_first + t_last) // 2) * (K - 1)

for t in T[1:-1]:
    res += (len(t) // 2) * K

print(res)

Submission status: Wrong Answer

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

curr = S[0]
tmp = ""
T = []
for s in S:
    if s == curr:
        tmp += s
    else:
        T.append(tmp)
        curr = s
        tmp = s
T.append(tmp)

if len(T) == 1:
    t = T[0]
    res = 0
    if len(t) == 1:
        res = K // 2
    elif len(t) == 2 and t[0] != t[1]:
        res = 0
    elif len(t) == 2 and t[0] == t[1]:
        res = K
    elif len(t) % 2 == 0:
        res = (len(t) // 2) * K
    else:
        res = (len(t) // 2) * K
        res += K - 1
    print(res)
    exit(0)

res = 0

t_first = T[0]
t_last = T[-1]

if t_first[0] != t_last[0]:
    res += (len(t_first) // 2) * K
    res += (len(t_last) // 2) * K
else:
    if len(t_first) == 1:
        res += K - 1
        res += (len(t_last) // 2) * K
    elif len(t_last) == 1:
        res += K - 1
        res += (len(t_first) // 2) * K
    elif len(t_first) % 2 != 0 and len(t_last) % 2 != 0:
        res += (len(t_first) // 2) * K
        res += (len(t_last) // 2) * K
        res += K - 1
    else:
        res += (len(t_first) // 2) * K
        res += (len(t_last) // 2) * K

for t in T[1:-1]:
    res += (len(t) // 2) * K

print(res)

Submission status: Wrong Answer

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

curr = S[0]
tmp = ""
T = []
for s in S:
    if s == curr:
        tmp += s
    else:
        T.append(tmp)
        curr = s
        tmp = s
T.append(tmp)

if len(T) == 1:
    t = T[0]
    res = 0
    if len(t) == 1:
        res = K // 2
    elif len(t) == 2 and t[0] != t[1]:
        res = 0
    elif len(t) == 2 and t[0] == t[1]:
        res = K
    elif len(t) % 2 == 0:
        res = (len(t) // 2) * K
    else:
        res = (len(t) // 2) * K
        res += K - 1
    print(res)
    exit(0)

res = 0

t_first = T[0]
t_last = T[-1]

if t_first[0] != t_last[0]:
    res += len(t_first) // 2
    res += len(t_last) // 2
else:
    if len(t_first) == 1:
        res += K - 1
        res += (len(t_last) // 2) * K
    elif len(t_last) == 1:
        res += K - 1
        res += (len(t_first) // 2) * K
    elif len(t_first) % 2 != 0 and len(t_last) % 2 != 0:
        res += (len(t_first) // 2) * K
        res += (len(t_last) // 2) * K
        res += K - 1
    else:
        res += (len(t_first) // 2) * K
        res += (len(t_last) // 2) * K

for t in T[1:-1]:
    res += (len(t) // 2) * K

print(res)

Submission status: Wrong Answer

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

ans = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        ans += 1
        S[i+1] = "*"

if S[0] == S[-1]:
    ans += 1
print(ans*K)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
#%%
from copy import deepcopy
def count_replace_pos(s):
    cnt = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = '0'
            cnt += 1

    if s[0] == s[-1]:
        cnt += 1

    return cnt

def count_replace_pre(s):
    cnt = 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            s[i] = '0'
            cnt += 1

    if s[0] == s[-1]:
        cnt += 1

    return cnt

s1 = deepcopy(s)
c1 = count_replace_pos(s1)
s2 = deepcopy(s)
c2 = count_replace_pre(s2)

ans = min(c1, c2) * k
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
#%%
cnt = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        s[i+1] = '0'
        cnt += 1

if s[0] == s[-1]:
    cnt += 1

ans = cnt * k
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
n = int(input())
s += s[0]

ct = 0
prev = ''
for i in s:
    if i == prev:
        ct += 1
        prev = ''
    else:
        prev = i
        
print(ct*n)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while i<=L-1 and S[i]==S[0]:
      head += 1
      i +=1
    while j<=L-1 and S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail<=0:
      ans=K*L//2
    #elif L-head-tail==0:
    #  print("")
    #  ans= K*( (head//2)+(tail//2) )
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while i<=L-1 and S[i]==S[0]:
      head += 1
      i +=1
    while j<=L-1 and S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail<0:
      ans=K*L//2
    elif L-head-tail==0:
      print("")
      ans= K*( (head//2)+(tail//2) )
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
L=len(S)
cnt=0
ren=1

if L==1:
  ans= K//2
elif L==2:
  if S[0]==S[1]:
    ans = K
  else:
    ans = 0
else:  #|S|>=3が保証
  if S[0]!=S[-1]:
    for i in range(1,L):  
      if S[i]==S[i-1]:
        ren +=1
        if i==L-1:
          cnt += ren//2  
      else:
        cnt += ren//2
        ren=1
    ans=cnt*K
  else:
    head=0
    tail=0
    i=0
    j=0
    while i<=L-1 and S[i]==S[0]:
      head += 1
      i +=1
    while j<=L-1 and S[-1-j]==S[-1]:
      tail += 1
      j += 1
    #print(head,tail)
    if L-head-tail<0:
      ans=K*L//2
    elif L-head-tail==0:
      ans= K*( (head//2)+(tail//2) )
    else:  
      for k in range(head,L-tail):  
        #print("k=",k,"S[k]=",S[k])
        if S[k]==S[k-1]:
          ren +=1
          if k==L-tail-1:
            cnt += ren//2  
        else:
          cnt += ren//2
          ren=1
      #print("cnt",cnt)   
      ans=K*cnt + (K-1)*(head+tail)//2 + head//2 + tail//2
    
print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())

ans = 0

if len(s) == 1:
    print(k//2)
elif len(s) == 2:
    if s[0] == s[1]:
        print(k)
    else:
        print(0)
else:
    c = False
    for i in range(1,len(s)):
        if s[i-1] == s[i] and not c:
            ans += k
            c = True
        else:
            c = False
    if s[0] == s[-1] != s[-2]:
        ans += k-1
    print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
ans = 0
flag = False
if len(s) == 1:
    print(k//2)
    exit()
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ans += 1
        s[i+1] = '_'
if s[0] == s[-1]:
    flag = True
    ans += 1
ans *= k
if flag:
    ans -= 1
print(ans)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
ans = 0
flag = False
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ans += 1
        s[i+1] = '_'
if s[0] == s[-1]:
    flag = True
    ans += 1
ans *= k
if flag:
    ans -= 1
print(ans)