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:
#!/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

if 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: Wrong Answer

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

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

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left >> 1) + (mid >> 1) * (k - 1) + (right >> 1)

    prev = 1
    sz = 0
    for i in xrange(lidx, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += (sz // 2)
            sz = 1

        prev = i
else:
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    if sz != 0:
        ans += (sz // 2)
        sz = 0

    ans *= k

print ans

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
s=[]
cnt=0
a=0

if len(S)==1:
  print(K//2)
else:
  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: Wrong Answer

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: Wrong Answer

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
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: Wrong Answer

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
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_orig[-2]) and (S[-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: Wrong Answer

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
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_orig[-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: Wrong Answer

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%l] = dummy
                replace_cnt_two += 1
        diff = replace_cnt_two - replace_cnt
        print(diff*(K-1)+replace_cnt)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
s=[]
cnt=0
a=0

if len(S)==1:
  print(K//2)
else:
  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: Wrong Answer

Solution code:
S=input()
K=int(input())
s=[]
cnt=0
a=0

if len(S)==1:
  print(K//2)
else:
  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: Wrong Answer

Solution code:
S = input() + 'Z'
K = int(input())

n = 0
x = S[0]
T = []

for s in S:
    if s == x:
        n += 1
    else:
        x = s
        if n != 0:
            T.append(n)
            n = 1

if len(T) == 1:
    ans = (T[0]*K) // 2
elif  K==1 or S[0]!=S[-2]:
    ans = K * sum([t//2 for t in T])
else:
    ans = K * sum([t//2 for t in T[1:-1]]) + T[0]//2 + T[-1]//2 + (T[0]+T[-1])//2
    
print(ans)

Submission status: Wrong Answer

Solution code:
S = input() + 'Z'
K = int(input())

n = 0
x = S[0]
T = []

for s in S:
    if s == x:
        n += 1
    else:
        x = s
        if n != 0:
            T.append(n)
            n = 1

if len(T) == 1:
    ans = (T[0]*K) // 2 
elif S[0] != S[-2]:
    ans = K * sum([t//2 for t in T])
else:
    ans = K * sum([t//2 for t in T[1:-1]]) + T[0]//2 + T[-1]//2 + (T[0]+T[-1])//2
    
print(ans)

Submission status: Wrong Answer

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


print(cnt*K)

Submission status: Wrong Answer

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

alphabets = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]


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]:
            replace_cnt += 1
            cnt = 0
            while S[i] == S[i-1]:
                S[i] = alphabets[cnt]
                cnt += 1
    if S[0] != S[-1]:
        print(replace_cnt*K)
    else:
        print(replace_cnt*K+(K-1))

Submission status: Wrong Answer

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

alphabets = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]


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]:
            replace_cnt += 1
            cnt = 0
            while S[i] == S[i-1]:
                S[i] = alphabets[cnt]
                cnt += 1
    if S[0] != S[-1]:
        print(replace_cnt*K)
    else:
        print(replace_cnt*K+(K-2))

Submission status: Wrong Answer

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


lis = []
i = 0
cnt = 1

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

res = 0

if len(lis) == 1:
    res += (lis[0]*k)//2
else:
    if s[0] == s[len(s)-1]:
        for a in lis[1:-1]:
            res += ((a)//2)*k
        res += ((lis[0])//2)*(k-1)
        res += ((lis[-1])//2)*(k-1)
        res += ((lis[0]+lis[-1])//2)*(k-1)
    else:
        for a in lis:
            res += ((a)//2)*k

print(res)

Submission status: Wrong Answer

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


lis = []
i = 0
cnt = 1

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

res = 0
if s[0] == s[len(s)-1]:
    for a in lis[1:-1]:
        res += ((a)//2)*k
    res += ((lis[0])//2)*(k-1)
    res += ((lis[-1])//2)*(k-1)
    res += ((lis[0]+lis[-1])//2)*(k-1)
else:
    for a in lis:
        res += ((a)//2)*k

print(res)

Submission status: Wrong Answer

Solution code:
word = list(input())
num = int(input())
count = 1
box = []
for i in range(len(word)-1):
  if word[i] == word[i + 1]:
    count += 1
  else:
    box.append(count)
    count = 1
box.append(count)
total = 0
for i in box:
  total += i//2
  


print(total*num)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def solve():
    ans=0;f=0
    s=input();n=len(s);k=int(input())
    if len(s)==1:
        return k//2
    for i in range(n-1):
        if s[i]!=s[i+1]:
            break
    else:return (n*k)//2
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2
        ans+=count//2
    else:
        ans+=count//2*(k-1)
        
        
    i=j
    while i<n-1:
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        ans+=count//2*(k)
    return ans

print(solve())

Submission status: Wrong Answer

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

ans=0
x=s
x+='Q'
a=s+s
a+='A'
cnt=0
tmp=1
ad=0

bng=1

on=1
oe=0
if k==1:
    for i in range(0 ,len(x)-1):
        if x[i]==x[i+1]:
            on+=1
        if x[i]!=x[i+1]:
            oe+=int(on/2)
            on=1

    print(int(oe))

else :
    for i in range(0, len(a)-2):
        if a[i]==a[i+1]:
            bng+=1

    if bng==len(a)-1:
        print(int(len(s)*k/2))

    else:
        for i in range(0, len(a)-1):
            if a[i]==a[i+1]:
                tmp+=1
            if a[i]!=a[i+1]:
                cnt+=int(tmp/2)
                tmp=1

        v=0
        tmpv=0
        for i in range(1,len(s)-2):
            if s[i]==s[i+1]:
                tmpv+=1
            if s[i]!=s[i+1]:
                v+=int(tmpv/2)
                tmpv=1


        if s[0]==s[len(s)-1]:
            v+=1

        if a[0]==a[len(a)-2]:
            ad=1

        if k%2==0:
            ans=ad*(int(k/2)-1)+cnt*(int(k/2))

        else :
            ans=ad*(int(k/2)-1)+cnt*(int(k/2))+ad+v

        print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def solve():
    ans=0;f=0
    s=input();n=len(s);k=int(input())
    if len(s)==1:
        return k//2
    for i in range(n-1):
        if s[i]!=s[i+1]:
            break
    else:return (n*k)//2
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2
        ans+=count//2
    else:
        ans+=count//2*(k-1)
        
        
    i=1
    while i<n-1:
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        ans+=count//2*(k)
    return ans

print(solve())

Submission status: Wrong Answer

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

ans=0

a=s+s
a+='A'
cnt=0
tmp=1
ad=0

bng=1

on=1
if k==1:
    for i in range(0 ,len(s)-1):
        if s[i]==s[i+1]:
            on+=1

    print(int(on/2))
else :

    for i in range(0, len(a)-2):
        if a[i]==a[i+1]:
            bng+=1

    if bng==len(a)-1:
        print(int(len(s)*k/2))

    else:
        for i in range(0, len(a)-1):
            if a[i]==a[i+1]:
                tmp+=1
            if a[i]!=a[i+1]:
                cnt+=int(tmp/2)
                tmp=1

        v=0
        tmpv=0
        for i in range(1,len(s)-2):
            if s[i]==s[i+1]:
                tmpv+=1
            if s[i]!=s[i+1]:
                v+=int(tmpv/2)
                tmpv=1


        if s[0]==s[len(s)-1]:
            v+=1

        if a[0]==a[len(a)-2]:
            ad=1

        if k%2==0:
            ans=ad*(int(k/2)-1)+cnt*(int(k/2))

        else :
            ans=ad*(int(k/2)-1)+cnt*(int(k/2))+ad+v

        print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
def solve():
    ans=0;f=0
    s=input();n=len(s);k=int(input())
    if len(s)==1:
        return k//2
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2*(k-1)
        ans+=count//2
    else:
        ans+=count//2*(k-1)
        
        
    i=1
    while i<n-1:
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        ans+=count//2*(k)
    return ans

print(solve())

Submission status: Wrong Answer

Solution code:
print("1")