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: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

#### input
S_orig = list(input())
K = int(input())

if len(S_orig) == 1:
  if K == 1:
    print(0)
    exit()
  else:
    S_orig = S_orig * K
    K = 1

if K != 1:
  S = S_orig * 2
else:
  S = S_orig

last_chk = 0
letters = string.ascii_lowercase
score = 0
for i in range(0, len(S) - 2):
  if S[i] == S[i + 1]:
    S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
    score = score + 1

if S[-2] == S[-1]:
  S[-1] = letters.strip(S[-2]).strip(S[0])[0]
  score = score + 1
else:
  if S[-1] == S[0]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
if em:print(S_orig)
if em:print(S)
if em:print("-----")
if K == 1:
  print(score)
  exit()
else:
  score_pre, score_fol = 0, 0
  for i in range(0, len(S_orig)):
    if S_orig[i] != S[i]:
      score_pre = score_pre + 1

  for i in range(0, len(S_orig)):
    if S_orig[i] != S[len(S_orig) + i]:
      score_fol = score_fol + 1

elif K % 2 == 0:
  total = (score_pre + score_fol) * K / 2
else:
  total = (score_pre + score_fol) * (K - 1) / 2 + score_fol
if em: embed()
if (S_orig[-1] != S[-1]) and (S_orig[-1] != S[-2]):
  total = total - 1
print(int(total))



# def calc(S, K):
#   if len(S) == 1:
#     if K == 1:
#       print(0)
#       exit()
#     else:
#       S = S * K
#       K = 1
#   # a, b = map(int, input().split())
#   # a = list(map(int, input().split()))
#   # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


#   # n = int(input())
#   # t = [int(input()) for i in range(n)]
#   if em: sys.stdin = open('/dev/tty')

#   #### function
#   # @lru_cache(maxsize=10000)

#   #### process
#   # if em: embed()
#   last_chk = 0
#   letters = string.ascii_lowercase
#   score = 0
#   for i in range(0, len(S) - 2):
#     # print(S)
#     if S[i] == S[i + 1]:
#       S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
#       score = score + 1

#   if S[-2] == S[-1]:
#     S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#     score = score + 1
#     # last_chk = 1
#   else:
#     if S[-1] == S[0]:
#       S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#       score = score + 1
#       last_chk = 1
#   return score, last_chk, S


# if K % 2 == 0:
#   S = S * 2
#   K = int(K / 2)
#   score, last_chk, S2 = calc(S,K)
#   if last_chk == 1: score = score - 1
#   total = score * K
#   if em: print("B")
#   # print("----")
#   # print(total)
#   print(total)
# else:
#   if K == 1:
#     score, last_chk, S2 = calc(S,K)
#     if last_chk == 1: score = score - 1
#     total = score * K
#     if em: print("A")
#     # print("----")
#     # print(total)
#     print(total)
#   else:
#     if em: print("C")
#     S_orig = S
#     S = S * 2
#     K = int((K - 1) / 2)
#     score, last_chk, S2 = calc(S,K)
#     # if last_chk == 1: score = score - 1
#     total = score * K
#     # if em: embed()
#     S3 = [S2[-1]] + S_orig
#     score3, last_chk3, S4 = calc(S3,1)
#     if last_chk3 == 1: score3 = score3 - 1
#     print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

#### input
S_orig = list(input())
K = int(input())

if len(S_orig) == 1:
  if K == 1:
    print(0)
    exit()
  else:
    S_orig = S_orig * K
    K = 1

if K != 1:
  S = S_orig * 2
else:
  S = S_orig

last_chk = 0
letters = string.ascii_lowercase
score = 0
for i in range(0, len(S) - 2):
  if S[i] == S[i + 1]:
    S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
    score = score + 1

if S[-2] == S[-1]:
  S[-1] = letters.strip(S[-2]).strip(S[0])[0]
  score = score + 1
else:
  if S[-1] == S[0]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
if em:print(S_orig)
if em:print(S)
if em:print("-----")
if K == 1:
  print(total)
  exit()
else:
  score_pre, score_fol = 0, 0
  for i in range(0, len(S_orig)):
    if S_orig[i] != S[i]:
      score_pre = score_pre + 1

  for i in range(0, len(S_orig)):
    if S_orig[i] != S[len(S_orig) + i]:
      score_fol = score_fol + 1

if K == 1:
  total = score
  if (S_orig[-1] != S[len(S_orig)]) and (S_orig[-1] != S[len(S_orig)-1]):
    total = total - 1
  print(int(total))
  exit()
elif K % 2 == 0:
  total = (score_pre + score_fol) * K / 2
else:
  total = (score_pre + score_fol) * (K - 1) / 2 + score_fol
if em: embed()
if (S_orig[-1] != S[-1]) and (S_orig[-1] != S[-2]):
  total = total - 1
print(int(total))



# def calc(S, K):
#   if len(S) == 1:
#     if K == 1:
#       print(0)
#       exit()
#     else:
#       S = S * K
#       K = 1
#   # a, b = map(int, input().split())
#   # a = list(map(int, input().split()))
#   # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


#   # n = int(input())
#   # t = [int(input()) for i in range(n)]
#   if em: sys.stdin = open('/dev/tty')

#   #### function
#   # @lru_cache(maxsize=10000)

#   #### process
#   # if em: embed()
#   last_chk = 0
#   letters = string.ascii_lowercase
#   score = 0
#   for i in range(0, len(S) - 2):
#     # print(S)
#     if S[i] == S[i + 1]:
#       S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
#       score = score + 1

#   if S[-2] == S[-1]:
#     S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#     score = score + 1
#     # last_chk = 1
#   else:
#     if S[-1] == S[0]:
#       S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#       score = score + 1
#       last_chk = 1
#   return score, last_chk, S


# if K % 2 == 0:
#   S = S * 2
#   K = int(K / 2)
#   score, last_chk, S2 = calc(S,K)
#   if last_chk == 1: score = score - 1
#   total = score * K
#   if em: print("B")
#   # print("----")
#   # print(total)
#   print(total)
# else:
#   if K == 1:
#     score, last_chk, S2 = calc(S,K)
#     if last_chk == 1: score = score - 1
#     total = score * K
#     if em: print("A")
#     # print("----")
#     # print(total)
#     print(total)
#   else:
#     if em: print("C")
#     S_orig = S
#     S = S * 2
#     K = int((K - 1) / 2)
#     score, last_chk, S2 = calc(S,K)
#     # if last_chk == 1: score = score - 1
#     total = score * K
#     # if em: embed()
#     S3 = [S2[-1]] + S_orig
#     score3, last_chk3, S4 = calc(S3,1)
#     if last_chk3 == 1: score3 = score3 - 1
#     print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

#### input
S_orig = list(input())
K = int(input())

if len(S_orig) == 1:
  if K == 1:
    print(0)
    exit()
  else:
    S_orig = S_orig * K
    K = 1

if K != 1:
  S = S_orig * 2
else:
  S = S_orig

last_chk = 0
letters = string.ascii_lowercase
score = 0
for i in range(0, len(S) - 2):
  if S[i] == S[i + 1]:
    S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
    score = score + 1

if S[-2] == S[-1]:
  S[-1] = letters.strip(S[-2]).strip(S[0])[0]
  score = score + 1
else:
  if S[-1] == S[0]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
if em:print(S_orig)
if em:print(S)
if em:print("-----")
if K == 1:
  print(total)
  exit()
else:
  score_pre, score_fol = 0, 0
  for i in range(0, len(S_orig)):
    if S_orig[i] != S[i]:
      score_pre = score_pre + 1

  for i in range(0, len(S_orig)):
    if S_orig[i] != S[len(S_orig) + i]:
      score_fol = score_fol + 1

if K == 1:
  total = score_pre
  if (S_orig[-1] != S[len(S_orig)]) and (S_orig[-1] != S[len(S_orig)-1]):
    total = total - 1
  print(int(total))
  exit()
elif K % 2 == 0:
  total = (score_pre + score_fol) * K / 2
else:
  total = (score_pre + score_fol) * (K - 1) / 2 + score_fol
if em: embed()
if (S_orig[-1] != S[-1]) and (S_orig[-1] != S[-2]):
  total = total - 1
print(int(total))



# def calc(S, K):
#   if len(S) == 1:
#     if K == 1:
#       print(0)
#       exit()
#     else:
#       S = S * K
#       K = 1
#   # a, b = map(int, input().split())
#   # a = list(map(int, input().split()))
#   # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


#   # n = int(input())
#   # t = [int(input()) for i in range(n)]
#   if em: sys.stdin = open('/dev/tty')

#   #### function
#   # @lru_cache(maxsize=10000)

#   #### process
#   # if em: embed()
#   last_chk = 0
#   letters = string.ascii_lowercase
#   score = 0
#   for i in range(0, len(S) - 2):
#     # print(S)
#     if S[i] == S[i + 1]:
#       S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
#       score = score + 1

#   if S[-2] == S[-1]:
#     S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#     score = score + 1
#     # last_chk = 1
#   else:
#     if S[-1] == S[0]:
#       S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#       score = score + 1
#       last_chk = 1
#   return score, last_chk, S


# if K % 2 == 0:
#   S = S * 2
#   K = int(K / 2)
#   score, last_chk, S2 = calc(S,K)
#   if last_chk == 1: score = score - 1
#   total = score * K
#   if em: print("B")
#   # print("----")
#   # print(total)
#   print(total)
# else:
#   if K == 1:
#     score, last_chk, S2 = calc(S,K)
#     if last_chk == 1: score = score - 1
#     total = score * K
#     if em: print("A")
#     # print("----")
#     # print(total)
#     print(total)
#   else:
#     if em: print("C")
#     S_orig = S
#     S = S * 2
#     K = int((K - 1) / 2)
#     score, last_chk, S2 = calc(S,K)
#     # if last_chk == 1: score = score - 1
#     total = score * K
#     # if em: embed()
#     S3 = [S2[-1]] + S_orig
#     score3, last_chk3, S4 = calc(S3,1)
#     if last_chk3 == 1: score3 = score3 - 1
#     print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

#### input
S_orig = list(input())
K = int(input())

if len(S_orig) == 1:
  if K == 1:
    print(0)
    exit()
  else:
    S_orig = S_orig * K
    K = 1

S = S_orig * 2

last_chk = 0
letters = string.ascii_lowercase
score = 0
for i in range(0, len(S) - 2):
  # print(S)
  if S[i] == S[i + 1]:
    S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
    score = score + 1

if S[-2] == S[-1]:
  S[-1] = letters.strip(S[-2]).strip(S[0])[0]
  score = score + 1
  # last_chk = 1
else:
  if S[-1] == S[0]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
    last_chk = 1
if em:print(S_orig)
if em:print(S)
if em:print("-----")
score_pre, score_fol = 0, 0
for i in range(0, len(S_orig)):
  if S_orig[i] != S[i]:
    score_pre = score_pre + 1

for i in range(0, len(S_orig)):
  if S_orig[i] != S[len(S_orig) + i]:
    score_fol = score_fol + 1

if K == 1:
  total = score_pre
  if (S_orig[-1] != S[len(S_orig)]) and (S_orig[-1] != S[len(S_orig-1)]):
    total = total - 1
  print(int(total))
  exit()
elif K % 2 == 0:
  total = (score_pre + score_fol) * K / 2
else:
  total = (score_pre + score_fol) * (K - 1) / 2 + score_fol
if em: embed()
if (S_orig[-1] != S[-1]) and (S_orig[-1] != S[-2]):
  total = total - 1
print(int(total))



# def calc(S, K):
#   if len(S) == 1:
#     if K == 1:
#       print(0)
#       exit()
#     else:
#       S = S * K
#       K = 1
#   # a, b = map(int, input().split())
#   # a = list(map(int, input().split()))
#   # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


#   # n = int(input())
#   # t = [int(input()) for i in range(n)]
#   if em: sys.stdin = open('/dev/tty')

#   #### function
#   # @lru_cache(maxsize=10000)

#   #### process
#   # if em: embed()
#   last_chk = 0
#   letters = string.ascii_lowercase
#   score = 0
#   for i in range(0, len(S) - 2):
#     # print(S)
#     if S[i] == S[i + 1]:
#       S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
#       score = score + 1

#   if S[-2] == S[-1]:
#     S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#     score = score + 1
#     # last_chk = 1
#   else:
#     if S[-1] == S[0]:
#       S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#       score = score + 1
#       last_chk = 1
#   return score, last_chk, S


# if K % 2 == 0:
#   S = S * 2
#   K = int(K / 2)
#   score, last_chk, S2 = calc(S,K)
#   if last_chk == 1: score = score - 1
#   total = score * K
#   if em: print("B")
#   # print("----")
#   # print(total)
#   print(total)
# else:
#   if K == 1:
#     score, last_chk, S2 = calc(S,K)
#     if last_chk == 1: score = score - 1
#     total = score * K
#     if em: print("A")
#     # print("----")
#     # print(total)
#     print(total)
#   else:
#     if em: print("C")
#     S_orig = S
#     S = S * 2
#     K = int((K - 1) / 2)
#     score, last_chk, S2 = calc(S,K)
#     # if last_chk == 1: score = score - 1
#     total = score * K
#     # if em: embed()
#     S3 = [S2[-1]] + S_orig
#     score3, last_chk3, S4 = calc(S3,1)
#     if last_chk3 == 1: score3 = score3 - 1
#     print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Runtime Error

Solution code:
from copy import deepcopy
s = list(input())
K = int(input())
dummy = "A"
S = deepcopy(s)
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    replace_cnt = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = dummy
            replace_cnt += 1
    if S[0] != S[-1]:
        print(replace_cnt*K)
    else:
        S = deepcopy(s)
        replace_cnt_two = 0
        l = len(S)
        for i in range(1, l*2):
            if S[i%l] == S[(i-1)%l]:
                S[i] = dummy
                replace_cnt_two += 1
        diff = replace_cnt_two - replace_cnt
        print(diff*(K-1)+replace_cnt)

Submission status: Runtime Error

Solution code:
print(a)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
#print(s,k)
all=""
for i in range(k):
  all=all+s
#print(all)
count=0
for i in range(len(all)-1):
  if(all[i]==all[i+1]):
    ran="ABCDEFHIGCEGEAKEABCDEFHIGCEGEAKEABCDEFHIGCEGEAKE"
    all=all[:i] + ran[i] + all[i+1:]
    count=count+1
    print(all)
print(count-1)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
s=[]
cnt=0
a=0
for i in range(len(S)):
  s.append(S[i])
if s[0]==s[-1]:
  cnt+=1
  if s[-1]!=s[-2]:
    a=1
  s[-1]='@'
  
  
for i in range(len(S)-1):
  if s[i]==s[i+1]:
    s[i+1]='@'
    cnt+=1


print(cnt*K-a)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=list(S*K)
s=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]='Q'
        s+=1
print(s)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=list(S*K)
s=0
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        T[i+1]='Q'
        s+=1
print(s)

Submission status: Runtime Error

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

for i in range(1, k + 1):
    t = t + s

t = list(t)
replace_count = 0
replace_word = '#'

for i in range(1, len(t) + 1):
    before_index = i - 1

    if t[before_index] != replace_word and t[before_index] == t[i]:
        replace_count += 1
        t[i] = replace_word


print(replace_count)

Submission status: Runtime Error

Solution code:
def opeCounter(S, K):
    T = S * K
    c = 0
    i = 1
    while i < len(T):
        if T[i-1] == T[i]:
            c += 1
            i += 1
        i += 1
    return c

def agc039a(S, K):
    s1 = opeCounter(S, 1)
    s2 = opeCounter(S, 2)
    s3 = opeCounter(S, 3)
    d1 = s2 - s1
    d2 = s3 - s2

    ans = 0
    if d1 == d2: ans = s1 + d1 * (K-1)
    else:
        if(k%2 != 0): ans = s1 + (d1+d2) * ((k+2-1)//2-1) ##(k/2):roundup
        else: ans = s2 + (d1+d2) * (k//2-1)
    return ans

S = input()
K = int(input())
print(agc039a(S, K))

Submission status: Runtime Error

Solution code:
def opeCounter(S, K):
    T = S * K
    c = 0
    i = 1
    while i < len(T):
        if T[i-1] == T[i]:
            c += 1
            i += 1
        i += 1
    return c

def agc039a(S, K):
    s1 = opeCounter(S, 1)
    s2 = opeCounter(S, 2)
    s3 = opeCounter(S, 3)
    d1 = s2 - s1
    d2 = s3 - s2

    ans = 0
    if d1 == d2: ans = s1 + d1 * (K-1)
    else:
        if(k%2 != 0): ans = s1 + (d1+d2) * ((k+2-1)/2-1) ##(k/2):roundup
        else: ans = s2 + (d1+d2) * (k/2-1)
    return ans

S = input()
K = int(input())
print(agc039a(S, K))

Submission status: Runtime Error

Solution code:
from itertools import groupby
from functools import reduce
from operator import xor

s = input()
k = int(input())
r = len(s) * k // 2
a = [reduce(xor, (1 for _ g)) for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Submission status: Runtime Error

Solution code:
a = input()
b = int(input())
c = 0
count = 0
for i in range(len(a)-1):
    if a[i] == a[i+1]:
        c += 1
    else:
        if c != 0:
            count += (c + 1) // 2
        c = 0
if c != 0:
    count += (c + 1) // 2
else:
  	if a[0] == a[-1]:
 		count += b
print(count * b)

Submission status: Runtime Error

Solution code:
import re

def main(s):
    #文字列sに同じ文字がn個以上連続している部分文字列を見つける
    n = 2
    # カンマを取ると n 個ちょうどになる
    reg = re.compile("(.)\\1{%d,}" %(n-1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]


if __name__ == "__main__":
    s = input()
    K = int(input())

    # 1文字の場合
    if len(s) == 1:
        print(K // 2)
        exit()

    t = list(main(s))

    # 全ての文字が同じ場合
    if len(t) == 1 and len(t[0]) == len(s):
        print((len(s) * K) // 2)
        exit()

    # 先頭と末尾が同じ場合
    ans1 = 0
    if len(s) >= 3 and s[0] == s[-1]:
        if len(t) >= 2 and t[0][0] == s[0] and t[-1][0] == s[-1]:
            ans1 = len(t[0]) // 2 + len(t[-1]) // 2 - ((len(t[0]) + len(t[-1])) // 2)
            ans *= K-1
        
        elif len(t) >= 1 and t[0][0] != s[0]:
            ans1 = len(t[-1]) // 2 - ((1 + len(t[-1])) // 2)
            ans1 *= K-1
        
        elif len(t) >= 1 and t[0][0] != s[0]:
            ans1 = len(t[0]) // 2 - ((len(t[0] + 1)) // 2)
            ans1 *= K-1
        
        else:
            ans1 = -1 * (K-1)

    # 先頭と末尾が異なる場合
    ans2 = 0
    for i in t:
        ans2 += len(i) // 2
    ans2 *= K

    ans = -ans1 + ans2
    print(ans)

Submission status: Runtime Error

Solution code:
def calc(S, T):
    U = [(S[0], 1)]
    un = 0
    for i in range(1, len(S)):
        if U[un][0] == S[i]:
            U[un] = (U[un][0], U[un][1] + 1)
        else:
            U += [(S[i], 1)]
            un += 1
    U = [u for u in U if u[1] > 1]
    un = len(U) - 1
    if un == 0:
        return (U[0][1] * T) // 2
    else:
        if U[0][0] != U[un][0]:
            return sum([u[1] // 2 for u in U]) * T
        else:
            return U[0][1] // 2 + U[un][1] // 2 \
                + (U[0][1] + U[un][1]) // 2 * (T - 1) \
                + sum([u[1] // 2 for u in U[1:un]]) * T


S = input()
T = int(input())
print(calc(S, T))

Submission status: Runtime Error

Solution code:
a = [input() for i in range(2)]

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

ans = 0
if text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Runtime Error

Solution code:
a = [input() for i in range(2)]

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

ans = 0
if text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
  else:
    for r in ren:
      ans += (r//2)
      ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()

string S;
ll K;

bool allSame() {
    REP (i, S.size()-1) if (S[i] != S[i+1]) return false;
    return true;
}

ll solve() {
    if (allSame()) {
        return K*S.size()/2;
    }
    ll res = 0;
    FOR (i, 1, S.size()) {
        if (S[i] == S[i-1]) {
            res++;
            if (i == S.size()-1) return res * K;
            i++;
        }
    }
    res *= K;
    if (S[0] == S[S.size()-1]) {
        res += K-1;
    }
    return res;
}

int main() {
    cin >> S >> K;
    cout << solve() << endl;
}

Submission status: Runtime Error

Solution code:
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count
 
def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())
 
if len(s)==1:
  print(k//2)
elif num_con(s)==len(s):
  print(d_c(s*k))
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  dis = (f//2 + m//2 - (f+m)//2)*(k-1)
  print(d_c(s)*k-dis)
else:
  print(d_c(s)*k)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,S):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,S2):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1
print(tmp+(tmp2-tmp)*(K-1))

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
S2 = S*2
tmp=0
tmp2=0
for i in range(1,S):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,S2):
    if S2[i-1]==S2[i]:
        S[i]="0"
        tmp2 += 1
print(tmp+tmp*(K-1))

Submission status: Runtime Error

Solution code:

# import sys
# input = sys.stdin.readline
# from collections import Counter
# import string
from collections import deque

INF = float('inf')


def main():
    N = int(input())
    # N = map(int, input().split())
    graph = [[] for i in range(N)]
    # ans = [0] * (N+1)
    # visit = [False]*(N+1)

    def get_distances(s):
        q = deque()
        q.append(s)
        d = [INF] * N
        d[s] = 0
        while len(q):
            u = q.popleft()
            for v, e in enumerate(graph[u]):
                if e:
                    if d[v] > d[u] + 1:
                        d[v] = d[u] + 1
                        q.append(v)
        return d

    def get_diameter():
        dmax = 0
        for i in range(N):
            d = get_distances(i)
            print(i, d)
            dmax = max(max(d), dmax)
        return dmax

    for i in range(N):
        edges = input()
        for s in range(len(edges)):
            e = int(edges[s])
            graph[i].append(e)

    q = deque()
    q.append(0)
    color = [0] * N
    vaild = True
    color[0] = 1
    d = [INF] * N
    d[0] = 0

    while vaild and len(q) != 0:
        u = q.popleft()
        # print(u+1, [i + 1 for i in q])
        for v, e in enumerate(graph[u]):
            # print(v, e)
            if e == 1:
                # print(v)
                if not color[v]:
                    color[v] = -color[u]
                    q.append(v)
                    # print(v+1, color)
                elif color[v] == color[u]:
                    vaild = False
                    # print('invalid')
                    break

                if d[v] > d[u] + 1:
                    d[v] = d[u] + 1
    if vaild:
        print(get_diameter() + 1)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    print(cnt)
    elif a[0] == a[-1]:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        # 接続部分
        start = 1
        end = 0
        i = 1
        # issii
        while (s[0] == s[i]):
            i +=1
            start +=1
        j = -1
        while (s[0] == s[j]):
            j -=1
            end +=1
        ans += replace_count(start+end) * (k-1)
        ans += replace_count(start) 
        ans += replace_count(end) 

        # 単語内部
        same = 1
        for idx in range(i, len(s)+j+1):
            if idx >= len(s):
                break
            if s[idx] == s[idx+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
    return ans


def replace_count(n):
    return n//2

main()

Submission status: Runtime Error

Solution code:

def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        # 接続部分
        start = 1
        end = 0
        i = 1
        # issii
        while (s[0] == s[i]):
            i +=1
            start +=1
        j = -1
        while (s[0] == s[j]):
            j -=1
            end +=1
        ans += replace_count(start+end) * (k-1)
        ans += replace_count(start) 
        ans += replace_count(end) 

        # 単語内部
        same = 1
        for idx in range(i, len(s)+j+1):
            if s[idx] == s[idx+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
    return ans


def replace_count(n):
    return n//2

main()

Submission status: Runtime Error

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

ss=""
for i in range(k):
	ss+=s

count=0

i=0
while i<len(ss):
	if ss[i]!=ss[i+1]:
		i+=1
	else:
		i+=2
		count+=1
print(count)