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:
X=input()
a=int(input())
count=0
ans=0
z=list(X)
b=0
if len(z)==1:
    ans=int(a/2)
elif z[0]!=z[len(z)-1]:
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
        ans=count*a
else:
     for k in range(len(z)-1):
        if z[k]==z[len(z)-1]:
            b+=1
     for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
     ans=int((a-1)*int((b+1)/2)+count)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import os

S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1
if cntL == len(S):
    if (K*cntL)%2==0:
        print(K*cntL//2)
    else:
        print((K*cntL-1)//2)
    sys.exit()
print(ans)

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 = list(input())
K = int(input())

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
  # 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
    # print("----")
    # print(total)
    print(total)
  else:
    S_orig = S
    S = S * 2
    K = int((K - 1) / 2)
    score, last_chk, S2 = calc(S,K)
    total = score * K
    # if last_chk == 1: total = total - 1
    # 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 =  input()
num = int(input())
out = 0

cha = s[0]
countf = 1
countl = 0
for i in range((len(s)-1)//2):
  if s[i+1] == cha:
    countf += 1
  else:
    break
for i in range((len(s)+1)//2):
  if s[-i-1] == cha:
    countl += 1
  else:
    break

fs = s[:-countl]
ms = s[countf:-countl]
ls = s[countf:]

if countf == 1 and countl == 0:
  fs = s
  ms = s
  ls = s

count = 1
for i in range(len(fs)-1):
  if fs[i] == fs[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2
  
count = 1
for i in range(len(ls)-1):
  if ls[i] == ls[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2    

count = 1
tout = 0
for i in range(len(ms)-1):
  if ms[i] == ms[i+1]:
    count += 1
  else:
    tout += count//2
    count = 1
tout += count//2
out += tout*(num-2)

out += ((countf+countl)//2) * (num-1)

if s.count(s[0]) == len(s):
  out = -(-(len(s)*num)//2)

print(out)

Submission status: Wrong Answer

Solution code:
import math

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

pre = ""
count = 0

if K >= 2:
    S += S
    K /= 2.0
for s in S:
    if s == pre:
        pre = ""
        count += 1
    else:
        pre = s

result = count * K

if S[0] == pre:
    result += (K-1)

print(math.floor(result))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

S = str(input())
K = int(input())

if len(S) == 1:
    S = S * 2
    K = K // 2

check = [0 for _ in range(len(S))]

if K >= 2 and len(S) >= 2:
    if S[0] == S[-1]:
        if S[-2] == S[-1]:
            pass
        else:
            check[0] = 1


for i in range(1, len(S)):
    if S[i - 1] == S[i]:
        if check[i - 1] == 0:
            check[i] = 1

print(sum(check) * K)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l = [c for c in s]
i=0
count=0
n=len(l)
l.append(l[0])
while i < n:
  if l[i] == l[i+1]:
    i += 2
    count += 1
    if i == 0:
      l[n] == 0 #絶対被らない文字列
  
  else:
    i += 1

if l[n-1] == l[n]:
  ans = count*k - 1
else:
  ans = count*k
  
print(ans)

Submission status: Wrong Answer

Solution code:
T = input()
# T = "xaaxxxx"
K = int(input())
# S = T*K
S_ = T*2
N = len(T)
last = T[-1]
mod = 10**9+7
"""
後ろを前につなげて、一番長くなるのはいくつ？
cooooooooonteeeeeeeeeest
3
"""
def check(S):
    S += "1"
    cnt = 1
    pre = "-1"
    flg = False
    ret = 0
    for i, s in enumerate(S):
        if pre == s:
            cnt += 1
            flg = True
        else:
            if flg:
                ret += cnt // 2
                flg = False
            cnt = 1
        pre = s
    return ret

s = 0
for i in range(N):
    if last != T[i]:
        s = i
        break
e = 0 
for i in range(N):
    if last != T[-i]:
        e = -i
        break
# 一回
if K == 1:
    print(check(T))
    exit()
# 同じ文字の連続
if len(set(T)) == 1:
    ans = check(T*2)*(K//2)+check(T)*(K%2)
    print(ans)
    exit()
# 最初と最後が違う
if s == e:
    print(check(T)*K)
    exit()


# その他
pre = T[:s]
mid = S_[s:e+2*N]
end = T[e-1:]
a = check(pre)    
b = check(mid)    
c = check(end)
# print(pre,mid,end)
print(a+b*(K-1)+c)




"""
xxxaaxyxx
2
"""

Submission status: Wrong Answer

Solution code:
S=list(str(input()))
s=set(S)
K=int(input())
num=0
i=0
lst=[]
def dp(i,lst):
  if i==len(S)-1:
    return lst
  if S[i]!=S[i+1]:
    return dp(i+1,lst)
  j=i+1
  while j<=len(S)-1  and S[i]==S[j]:
    j+=1
  lst.append(list(S[i:j]))
  return dp(j-1,lst)

if 2<=len(s):
  L=dp(i,lst)
  for i in range(len(L)):
    num+=(len(L[i])//2)
  if num==0:
    if S[0]!=S[-1]:
      print(0)
    else:
      print(K-1)
  elif L[-1][-1]==S[0] and len(L[-1])%2==1:
    print(num+(K-1)*(num+1))
  else:
    print(num*K)
else:
  print((len(S)*K)//2)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
S_1=S+"A"
S_2=S*2+"A"
S_3=S*3+"A"
cnt_1=0
cnt_2=0
cnt_3=0
import queue
q=queue.Queue()
for i in S_1:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_1+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)
q=queue.Queue()
for i in S_2:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_2+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)
q=queue.Queue()
for i in S_3:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt_3+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)         
    else:
        q.put(i)

if K==1:
    print(cnt_1)
else:
    print(cnt_2+(cnt_3-cnt_2)*(K-2))

Submission status: Wrong Answer

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

str_list = list(S)
str_list.append('0')
replace_times = 0
 
for i in range(0, len(S)-1):
  if i == len(S)-1:
    break
  elif str_list[i] == str_list[i+1] == str_list[i+2]:
    str_list[i+1] = str(i)
    replace_times += 1
    continue
  elif str_list[i] == str_list[i+1]:
    str_list[i+1] = str(i)
    replace_times += 1
    
print(replace_times * K)

Submission status: Wrong Answer

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

tail = s[0]
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      for i in s[::-1]:
        if tail == i:
          tmp_cnt2 += 1
        else:
          break
      break
  if (tmp_cnt1 + tmp_cnt2) % 2 == 1:
    tail = ''

cnt=0
if len(s)==1:
  ret = k
else:
  tmp=s[0]
  tmp_cnt=0
  for i in s[1:]+tail:
    if tmp==i:
      tmp_cnt+=1
    else:
      cnt+=(tmp_cnt+1)//2
      tmp_cnt=0
    tmp=i
  cnt+=(tmp_cnt+1)//2
  ret=cnt*k
print(ret)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
S = str(input())
K = int(input())
count =  0
tmp = 0
N = len(S)

if N == 1:
    if K >= 2:
        count = 1 * (K//2)
    else:
        count = 0
elif S[0] == S[N-1] == S[N-2]:
    count += 1
else:
    pass

for i in range(1,N-1):
    if S[i-1] == S[i] == S[i+1] and tmp == 0:
        tmp = 1
    else:
        tmp = 0
    if S[i-1] == S[i] and tmp == 0:
        count += 1
    else:
        pass
if S[N-2] == S[N-1] and tmp == 1:
    count += 1
else:
    pass


if N == 1 and tmp == 0:
    print(count)
elif N == 3 and tmp == 0:
    print(count*(K-1))
elif N == 3 and tmp == 1:
    print(count + K)
elif N >= 4 and S[0] == S[N-1] != S[N-2]:
     print(count+(K-1)*(count+1))
else:
    ans = count * K
    print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
counts = 0
count2 = 0
adder = 0
adder2 = 0

if len(S) == 1:
    print(K // 2)
elif len(S) == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print(0)

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



elif K <= 3:
    S_arranged = list(S*K)
    for i in range(len(S_arranged)-2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            counts += 1
        
    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            counts += 1
            
    print(counts)

else:
    S_arranged = list(S*4)
    for i in range(len(S_arranged) - 2):
        if (S_arranged[i] == S_arranged[i+1]) and (S_arranged[i+1] == S_arranged[i+2]):
            S_arranged[i+1] = "0"
            if i+1 <= len(S)-1:
                adder += 1
            elif i+1 > len(S)*3 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            else:
                count2 +=1

    for i in range(len(S_arranged)-1):
        if S_arranged[i] == S_arranged[i+1]:
            S_arranged[i] = "1"
            if i <= len(S)-1:
                adder += 1
            elif i > len(S)*3 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1


    if K % 2 == 0:
        print(int(adder + (counts + count2) * ((K-2)/2) + adder2))

    else:
        print(int((adder + counts * ((K-1)/2) + count2 * ((K-1)/2 -1) +adder2)))

Submission status: Wrong Answer

Solution code:
s = list(map(str,input()))
k = int(input())
cnt =0
bool =True
x=-1
ck=0
while bool:
    if s[x]==s[x+1]:
        #print('+A')
        cnt+=1
        x+=2
        #print(x)
        if x==1:
            ck+=1
        if x ==len(s):
            ck +=1
        if x>=len(s)-1:
            #print('fiA')
            bool =False
    elif s[x]!=s[x+1]:
        #print('+B')
        x+=1
        #print(x)
        if x>=len(s)-1:
            #print('fiB')
            bool =False
    if x>=len(s)-1:
        #print('fiC')
        bool =False
if ck==2:
    cnt-=1
#print(ck)
print(cnt*k)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = s + s
ans = 0
i = 1
while True:
    c = 0
    while True:
        if i>=len(t):
            break
        elif t[i-1] == t[i]:
            i +=2
            c+= 1
        else:
            break
    ans += c
    if i>=len(t):
        break
    i+=1
if len(set(s))==1:
    c = int(len(s)%2==0)
elif s[0]==s[-1]:
    c = 0
    while True:
        s0 = s[-1]
        if len(s)==1:
            break
        if s0==s[0]:
            c +=1
            s = s[:-1]
        else:
            break
if c%2 ==1:
    print((((ans+1)* k)-1)//2)
else:
    print(((ans* k))//2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

S = input()
K = int(input().split()[0])
c = 0
is_change_tail = False

unit_c = 0

# 連続する文字が奇数かどうか
# 先頭と末尾が異なる場合はシンプル
# 先頭と末尾が同じ場合は、
is_seq = False
count_seq_start = 1  # 先頭からの連続文字数
count_seq_end = 1  # 末尾の連続文字数agca

# 先頭からの連続文字数を数える
for i in range(1, len(S)):
    if S[i] == S[0]:
        count_seq_start += 1
    else:
        break

# 末尾からの連続文字数を数える
count_seq_end = 1  # 末尾の連続文字数

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

count_seq = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        count_seq += 1
    else:
        # count_seqの偶奇判定
        if count_seq % 2 == 0:
            unit_c += count_seq // 2
        else:
            unit_c += (count_seq - 1) // 2
        count_seq = 1

# 末尾まで連続していた時の処理
# 末尾が1文字だけの場合は、unit_cには0が足されるのでOK
# 末尾が2文字以上の場合、まだunit_cにはカウントされていないのでここで足してOK
if count_seq % 2 == 0:
    unit_c += count_seq // 2
else:
    unit_c += (count_seq - 1) // 2

c += unit_c * K

if S[0] == S[-1] and count_seq_start % 2 == 1 and count_seq_end % 2 == 1:
    c += K - 1

ans = int(c)
print(ans)

Submission status: Wrong Answer

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


cnt = 0
ss = s

for i in range(len(ss)):
    if i == 0:
        continue
    
    if ss[i-1] == ss[i]:
        ss = ss[:i] + '\\' + ss[i+1:]
        cnt += 1

        
cnt = cnt * k

if len(s) >= 3 and k >= 2 and s[0] == s[1] and s[0] == ss[2]:
    cnt += 1

if len(s) >=3:
    if s[0] == s[len(s)-1] and s[0] != s[1] and  s[len(s)-1] != s[len(s)-2]:
        cnt = cnt + (k - 1)
print(cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1
 
 
print(ans)

Submission status: Wrong Answer

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

if len(set(S)) == 1 or len(S) == 1:
    ans = K * len(S) // 2
    print(0)
    exit()

S1 = list(S)
S2 = list(S) + list(S)
S3 = list(S) + list(S) + list(S)

def calc(T):
    L = T[::]
    ret = 0
    for i, t in enumerate(L[1:], start=1):
        if t == L[i - 1]:
            ret += 1
            L[i] = '.'
    return ret

change = [0, 0, 0]
for i, T in enumerate([S1, S2, S3]):
    change[i] = min(calc(T), calc(T[:: -1]))

diff1 = change[1] - change[0]
diff2 = change[2] - change[0] * 2

ans = diff1 * K + (diff2 - diff1) * (K - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(c+(K-1)*cc+K-1)

Submission status: Wrong Answer

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

s_cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        s_cnt = i + 1
        break
else:
    s_cnt = len(s)
for i in range(len(s) - 1, 1, -1):
    if s[i] != s[i - 1]:
        e_cnt = len(s) - i
        break
else:
    e_cnt = len(s)
is_start = False
idx_list = []
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        if not is_start:
            is_start = True
            s_idx = i
    else:
        if is_start:
            is_start = False
            e_idx = i
            idx_list.append((s_idx, e_idx))
else:
    if is_start:
        idx_list.append((s_idx, len(s) - 1))
ans = 0
for s_idx, e_idx in idx_list:
    ans += (e_idx - s_idx + 1) // 2
ans *= k
if len(s) == 1:
    ans = k // 2
elif s_cnt == len(s) and e_cnt == len(s) and len(s) % 2 == 1 and len(s) != 1:
    ans += 1
elif s_cnt % 2 == 1 and e_cnt % 2 == 1 and s[0] == s[-1]:
    ans += k - 1
# print(ans)
print(0)

Submission status: Wrong Answer

Solution code:
import sys
s = input()
k = int(input())
ret = 0
t = 0
ss = []
ss.append(s[0])
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        ss[-1]+=s[i]
    else:
        ss.append(s[i])

for ss_ in ss[1:-1]:
    ret += len(ss_) // 2 * k

if len(ss) >= 3:
    if ss[0][0] != ss[-1][0]:
        ret += len(ss[0]) // 2 * k
        ret += len(ss[-1]) // 2 * k
    else:
        ret += (len(ss[-1]) + len(ss[0]))  // 2 * (k-1)
        ret += len(ss[0]) // 2
        ret += len(ss[-1]) // 2

if len(ss) == 1:
    ret += len(ss[0]) * k // 2

print(ret)

Submission status: Wrong Answer

Solution code:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    if k <= 3:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif len(s) >= 3 and s[0] == prev:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

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

N=len(S)
t=S[0]
cnt=0
arr=[]
for s in S[1:N]:
    if t==s:
        cnt+=1
    else:
        arr.append(cnt+1)
        cnt=0
    t=s
arr.append(cnt+1)
ans=sum(map(lambda x: x//2, arr))
if len(arr)>1 and S[0]==S[-1] and arr[0]%2==1:
    print((ans+1)*K)
#     print('A')
elif len(arr)==1 and N%2==1:
    if N==1:
        print(K//2)
    else:    
        print((ans+1)*K)
else:
    print((ans)*K)

Submission status: Wrong Answer

Solution code:
import copy

S=input()
K=int(input())
S=list(S)
n=len(S)
ans=0
j=1

def allsame(str):
    flag = True
    for i in range(len(str)-1):
        if str[i+1]!=str[i]:
            flag=False
            break;
    return flag

if S[0]!=S[n-1]:
    for i in range(n):
        if i==n-1:
            ans += j//2
            break
        if S[i+1]==S[i]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = ans*K
elif allsame(S):
    ans = n*K//2
else:
    i=0
    l=1
    for i in range(n):
        if S[i+1]==S[i]:
            l += 1
        else:
            tempans =l//2
            break;
    T=copy.copy(S)
    T.reverse
    k=1
    for i in range(n):
        if T[i+1]==T[i]:
            k += 1
        else:
            tempans +=k//2
            break;
    
    for i in range(n-l-k):
        if i==n-l-k:
            ans += j//2
            break;
        if S[i+l+1]==S[i+l]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = (ans+l+k//2)*K
    ans = ans + tempans
    
print(ans)

Submission status: Wrong Answer

Solution code:
S = str(input())
K = int(input())
# 全部異なる文字だった場合
if len(set(S)) == len(S):
    result = 0
# １文字めと最後の文字が異なる場合
elif S[0] != S[-1]:
    box = []
    mini_count = 0
    mini_str = ''
    for index, s in enumerate(S[:-1]):
        #print(s, S[index+1])
        if s == S[index+1]: # 次の文字と同じだった場合
            mini_count += 1
            mini_str = s
        else:
            if mini_count != 0:
                #print('#################')
                box.append(mini_str * (mini_count+1))
                mini_str = ''
                mini_count = 0
               
    all_temp = 0
    for each in box:
        if len(each) % 2 == 0: # 偶数の場合
            temp = len(each) / 2
        else: # 奇数の場合
            temp = len(each) // 2
        all_temp += temp
        
    result = all_temp * K

            
# １文字めと最後の文字が同じ場合
else:
    box = []
    mini_count = 0
    mini_str = ''
    for index, s in enumerate(S):
        if index+1 != len(S):# 最後の文字でない場合
            if s == S[index+1]: # 次の文字と同じだった場合
                mini_count += 1
                mini_str = s
            else:
                if mini_count != 0:
                    box.append(mini_str * (mini_count+1))
                    mini_str = ''
                    mini_count = 0
        else: # 最後の文字の場合
            if mini_count != 0:
                box.append(mini_str * (mini_count+1))
                mini_str = ''
                mini_count = 0
            
               
    all_temp = 0
    for each in box:
        if len(each) % 2 == 0: # 偶数の場合
            temp = len(each) / 2
        else: # 奇数の場合
            temp = len(each) // 2
        all_temp += temp
        
    if S[0] in box[-1]:
        result = (all_temp * K)
    else:
        result = (all_temp * K) + (K - 1)
    
print(int(result))

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[-2]== s[-3]== s[-4]== s[-5]== s[len(s)//2] \
                and s[0] == s[1] == s[2]== s[3]== s[4]== s[5]:
            print(s[10000000000000000000000000])
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n)

Submission status: Wrong Answer

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

ops = 0
i = 1

while i < len(s):
    last = 1
    if s[i-1] == s[i]:
        ops += 1
        i += 1
        last = 2
    i += 1

i = 1

ops2 = 0

ss = s + s

while i < len(ss):
    if ss[i-1] == ss[i]:
        ops2 += 1
        i += 1
    i += 1
last = 0

if i == len(ss) and ss[0] == ss[-1]:
    last = 1

ops = (k // 2) * ops2 + (k%2) * ops + last

print(ops)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
    exit()
changed = [False] * len(S)
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and changed[i-1]:
        continue
    elif S[i] == S[i-1]:
        changed[i] = True
ans = sum(changed)
res = ans * K
cnt, s, l, r = 1, S[0], 0, len(S)-1
while l + 1 <= r and S[l+1] == s:
    cnt += 1
    l += 1
while l <= r - 1 and S[r] == s:
    cnt += 1
    r -= 1
if changed[-1] or S[0] != S[-1] or cnt & 1:
    print(res)
    exit()
res += K - 1
print(res)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]//2
    print(int(c*k))
elif d == 1:
    print(int(d*k))
else:
    for i in range(1,d-1):
        c += a[i]//2
    c += (k-1)*((a[0]+a[d-1])/2) + a[0]/2 + a[d-1]/2
    print(int(c))

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
s = input()
k = (int)(input())

beforeStr = ''
count = 0
amari = 0

if len(s) == 1:
    print((int)(k / 2))
else:
    list_s = list(s)

    for i in range(len(s) + 1):

        if i == len(s):
            if beforeStr == list_s[0]:
                amari = (int)(0.5 * k)
            break
        if beforeStr == list_s[i]:
            count += 1
            list_s[i] = 'Z'
        beforeStr = list_s[i]

    print(count * k + amari)

Submission status: Wrong Answer

Solution code:
S = list(input())
#取得文字列
K = int(input())
#繰り返し回数

count = 0
befStr = "AAAAAA"

for i in range(1,len(S)):
  befStr = S[i - 1]
  if befStr == S[i]:
    count += 1
    S[i] = ""
    

print(count*K)

Submission status: Wrong Answer

Solution code:
a=input()
b=int(input())
s=""
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]
abb=0
abc=0

for x in a:
    if aa==[]:
        n+=1
    elif x==s:
        n+=1
    elif x!=s:
        ab.append(n)
        n=1
    s=x
    i+=1
    aa.append(x)
    if i==len(a):
        ab.append(n)
if aa[0]==aa[-1] and len(ab)>2:
    if ab[-1]!=1:
        abb=ab[-1]
        del ab[-1]
    if ab[0]!=1:
        abc=ab[0]
        del ab[0]
    s=aa[0]
    i=0
    for x in aa:
        if aa[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if aa[-i]==s:
            nn+=1
        else:
            break
        i+=1    
for x in ab:
    zn+=(x//2)
if len(a)==1:
    print(b//2)
elif len(ab)==1 and abb==0 and abc==0:
    print((b*ab[0])//2)
else:
    print(zn*b+(nn//2)*(b-1)+(abb+abc)//2)

Submission status: Wrong Answer

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

# if not(1 <= len(S) and len(S) <= 100):
#     exit()
# if not (S.isalpha() and S.islower()):
#     exit()
# if not float(K).is_integer():
#     exit()
# if not (1 <= int(K) and int(K) <= 10 ** 9):
#     exit()



L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)

count *= int(K)
if len(L) == 1:
    print(count)
    exit()
if L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + 1 * int(K) - 1
print(count)

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  SS[0]="!"
  for i in range(1,len(S)):
    if SS[i]==SS[i-1]:
      SS[i]="!"
      cc+=1
  print(min(c*K+K-1,c+(K-1)*cc+K-1))

Submission status: Wrong Answer

Solution code:
S = input().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
str3 = S * 3
n1 = 0
n2 = 0
n3 = 0
ln1 = []
ln2 = []
ln3 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1
    ln2[i] = "A"
    
for c in str3:
  ln3.append(c)
for i in range(len(ln3)):
  if i == 0:
    pass
  elif ln3[i-1] == ln3[i]:
    n3 += 1
    ln3[i] = "A"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 0
        temp_2 = 0
        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 += (i+1)//2
        cnt += (len(a)-j)//2
        
        if a[0] == a[-1]:
            temp_3 = ((i+1) + len(a)-j)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

count=0
i=1
while i<len(S):
    if S[i]==S[i-1]:
        count+=1
        S[i]="."
    i+=1

a=(S[0]==S[-1])

if len(S)==1:
    print((K*len(S))//2)
else:
    print(count*K+a*(K-1))

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = ans2 = 0
N = len(S)
if N == 1:
    print(K - 1)
    exit()
for i in range(N - 2):
    if S[i] == S[i + 1] == S[i + 2]:
        S[i + 1] = 'AB' [(i + 1) % 2]
        ans += 1
if S[-2] == S[-1] == S[0]:
    S[-1] = 'AB' [N % 2 - 1]
    ans += 1
if S[1] == S[-1] == S[0]:
    S[0] = 'A'
    ans += 1
for i in range(N - 1):
    if S[i] == S[i + 1]:
        S[i + 1] = 'AB' [i % 2 - 1]
        ans += 1
if S[-1] == S[0]:
    S[0] = 'A'
    ans2 += 1
print(ans * K + ans2 * (K - 1))

Submission status: Wrong Answer

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

T = S * 2
T2 = S

countA = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA += 1
        S[i + 1] = 'A'

# 2bai
countB = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB += 1
        T[i + 1] = 'A'
if T[-1] == T2[0]:
    C = T2.copy()
    C.insert(0, T[-1])
    countC = 0
    for i in range(len(C) - 1):
        if C[i] == C[i + 1]:
            countC += 1
            C[i + 1] = 'A'
if K > 1:
    if (T[-1] == T2[0]):
        print(countA*K+K-1)
    else:
        if K % 2 == 1:
            print(K//2*countB+countA)
        else:
            print(K//2*countB)
else:
    print(countA)