Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
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 N==1:
    print(K//2)
elif len(arr)==1 and N%2==1:
    print((N//2+1)*K)

else:
    if S[0]==S[-1] and arr[0]%2==1 and arr[-1]%2==1:
        print((ans+1)*K-1)
    else:
        print((ans)*K)

Submission status: Wrong Answer

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

a_li = []
b = len(S)
i = 0
j = 0
S.append(0)
x = 0

while i != b:
    a = 1
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    a_li.append(a)

if S[0] == S[-2]:
    if a_li[0] >= 2 and (a_li[0]%2 == 1):
        if a_li[0] >= 2 and (a_li[0]%2 == 1):
            x = K-1
scr = 0
for n in range(len(a_li)):
    scr += a_li[n] // 2

if b == 1:
    scr = b//2
    
print(scr*K+x)

Submission status: Wrong Answer

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

sl = list(s)
cnt = 1
i = 0
su = 0
tmp = "0"
f = 0
ff =0
fst =0
lst = 0

for i in range(len(s)):
  if sl[i] == tmp:
    cnt += 1
  else:
    su += int(cnt / 2)
    if ff == 1:
      fst = cnt
    ff += 1
    cnt = 1
    tmp = sl[i]

if cnt != 1:
  su += int(cnt / 2)

lst = cnt

if sl[0] == sl[-1]:
  if (fst == 0) or (fst % 2 != 0):
    if (lst == 0) or (lst % 2 != 0):
      if (fst == 1) or (lst == 1):
        f = k - 1
      else:
        f = k // 2

t = k * su + f

if len(s) == 1:
  ath = k / 2
  print(int(ath))
else:  
  print(int(t))

Submission status: Wrong Answer

Solution code:
S = list(input())
l = int(len(S))      ##Sの長さ
K = int(input())
counter_mae = 0            ##最初からカウント             
counter_ato = 0
count = 0
Frag = False         ##偶奇フラグ(Trueは影響なし)
Frag_z = False       ##全部同じかどうか
if S[0] == S[l - 1]:        ##先頭と末尾が同じとき
    for i in range(l):        ##先頭からの何文字目まで同じか
        if S[i] == S[0]:
            counter_mae += 1
        else:
            break
    if counter_mae == l:        ##全文字同じ場合 
        Frag_z = True
    else:                       ##全文字同じではないとき
        for i in range(l - 1):
            if S[0] == S[l - i - 1]:        ##後ろから何文字まで同じか
                counter_ato += 1

            else:
                break

        if counter_ato % 2 == 0:
            Frag = True
        else:
            if counter_mae % 2 == 0:
                Frag = True

for i in range(l - 1):     ##Sの中の書き換え操作回数
    if S[i] == S[i + 1]:
        count += 1
        S[i + 1] = 0


if Frag_z == True:
    if l % 2 == 0:
        print(K * count)
        
    else:
        if K % 2 == 0:
            print((K // 2) * (2 * count + 1))
            
        else:
            print((K // 2) * (2 * count + 1) + count)
else:
    if Frag == True:
        print(K * count)

    else:
        print(count + (K - 1) * (count + 1))

Submission status: Wrong Answer

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

beforeStr = ''
count_mae = 0
count_ato = 0

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

    for i in range(len(s) * 2):

        if beforeStr == list_s[i]:
            if 0 <= i and i <= len(s) - 1:
                count_mae += 1
            else:
                count_ato += 1
            list_s[i] = 'Z'
        beforeStr = list_s[i]

print((int)(count_mae * k / 2 + count_ato * k / 2))

Submission status: Wrong Answer

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

T = list(S)
T.insert(0, T[len(T)-1])
count = 0
for i in range(len(T)-1):
    if T[i] == T[i+1]:
        T[i+1] == "1"
        count += 1

if T[len(T)-1] != "1":
    t = list(S)
    cnt = 0
    for i in range(len(t)-1):
        if t[i] == t[i+1]:
            t[i+1] == "1"
            cnt += 1
    print(cnt + count * (K-1))

else:
    t = list(S * 2)
    cnt = 0
    for i in range(len(t)-1):
        if t[i] == t[i+1]:
            t[i+1] == "1"
            cnt += 1
    if K % 2 == 0:
        print(cnt * K / 2)
    else:
        tt = list(S)
        ccnt = 0
        for i in range(len(tt)-1):
            if tt[i] == tt[i+1]:
                tt[i+1] == "1"
                ccnt += 1
        print(ccnt + cnt * K // 2)

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 N==1:
    print(K//2)
elif len(arr)==1 and N%2==1:
    print((N//2+1)*K-1)

else:
    if S[0]==S[-1] and arr[0]%2==1 and arr[-1]%2==1:
        print((ans+1)*K-1)
    else:
        print((ans)*K)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

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

A = [len(tuple(v)) for _, v in groupby(S)]
if len(A) == 1:
    print((A[0] * K) // 2)
    quit()

if S[0] == S[-1]:
    m = sum(a // 2 for a in A) - A[0] // 2 - A[-1] // 2
    print(K * m + (K - 1) * (A[0] + A[-1]) // 2 + A[0] // 2 + A[-1] // 2)
else:
    print(sum(a // 2 for a in A) * K)

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 N==1:
    print(K//2)
elif len(arr)==1 and N%2==1:
    print((N//2+1)*K-1)

else:
    if S[0]==S[1] and arr[0]%2==1 and arr[-1]%2==1:
        print((ans+1)*K-1)
    else:
        print((ans)*K)

Submission status: Wrong Answer

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

a_li = []
b = len(S)
i = 0
j = 0
S.append(0)
x = 0

while i != b:
    a = 1
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    a_li.append(a)

if S[0] == S[-2] and b != 1:
    if a_li[0] >= 2 and (a_li[0]%2 == 1):
        if a_li[0] >= 2 and (a_li[0]%2 == 1):
            x = K-1
scr = 0
for n in range(len(a_li)):
    scr += a_li[n] // 2

if b == 1:
    scr = b//2
    
print(scr*K+x)

Submission status: Wrong Answer

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

def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]
    if l == 1:
        res = K//2
    else:
        s1, s2, s3 = '-', S[0], S[1]
        for i,s in enumerate(SK[2:]+'--', start=1):
            s1, s2, s3 = s2, s3, s
            if s3 and s1==s2:
                s2 = '='
                swap_count[i//l] += 1

        res = swap_count[0] + max(0, K-1)*swap_count[1]
    return str(res)
    

if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
s = input() * 2
k = int(input()) / 2
count = 0
i = 0

while i <= len(s):
    tmp = 1
    for j in range(i + 1, len(s)):
        if s[i] == s[j]:
            tmp += 1
        else:
            break
    i += tmp
    tmp //= 2
    count += tmp

print(int(count * k))

Submission status: Wrong Answer

Solution code:
import math


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

if K == 1:
    cnt = 0
    S2 = list(S)
    for i in range(1, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt += 1

    cnt = cnt * K

    # if S2[0] == S2[-1]:
    #    cnt += K-1

    print(cnt)

else:
    S2 = list(S)
    S2 = S2 + S2
    cnt1 = 0
    for i in range(1, int(len(S2)/2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt1 += 1

    cnt2 = 0
    for i in range(int(len(S2)/2), len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt2 += 1

    S3 = S2 + S2
    if S3[len(S2)] == S3[len(S2)+1]:
        flg = 1
    else:
        flg = 0

    cnt = cnt1 * math.ceil(K/2)
    cnt += cnt2 * (K//2)

    if S2[0] == S2[-1]:
        if flg!= 1:
            cnt += math.ceil(K/2) - 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
s = input()
length = len(s)
k = int(input())
if(len(s)==1):
    print(int(k/2))
    exit()
start = 0
end = 0
t = 0
last = 0
count = 0
ans = 0
b4 = s[0]
ansloop = 0
for c in s:
    if(b4!=c):
        if(last==0):
            start=t
            t=1
            last = 1
        else:
            ansloop+=1
            ans+=int(t/2)
            t=1
    else:
        t+=1
    count+=1
    b4=c
end = t
if(s[0]==s[-1] and start==end and ansloop!=1):print(int(start/2) + int(end/2) + (ans)*k + int(start/end)*(k-1))
else:print((int(start/2) + int(end/2) + ans)*k)

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

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 em:print(S_orig[i])
  if em:print(S[len(S_orig) + i - 1])
  if em:print("----")
  if S_orig[i] != S[len(S_orig) + i]:
    score_fol = score_fol + 1

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

if em:print(S)



# 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:
import sys
import numpy as np
def main():
    input = sys.stdin.readline
    S = input().strip()
    K = input()
    ans = np.zeros(1, dtype="int64")
    len_S = len(S)
    if len_S != 1:
        char_S = []
        cnts_S = []
        #char_S.append(S[0])
        cnt = 0
        for i in range(len_S - 1):
            #print(i+1)
            #print(len_S-1)
            #print(char_S)
            #print(cnts_S)
            if cnt == 0:
                if S[i] == S[i+1]:
                    cnt += 1
                    if i + 1 == len_S - 1:
                        char_S.append(S[i])
                        cnts_S.append(cnt)
                else:
                    char_S.append(S[i])
                    cnts_S.append(cnt)
                    cnt = 0
                    if i + 1 == len_S - 1:
                        char_S.append(S[i+1])
                        cnts_S.append(cnt)
            elif cnt == 1:
                char_S.append(S[i])
                cnts_S.append(cnt)
                cnt = 0
                if i + 1 == len_S - 1:
                    char_S.append(S[i+1])
                    cnts_S.append(cnt)

        cnts_repeat = np.count_nonzero(cnts_S)

        ans[0] = np.int64(cnts_repeat)
        ans[0] *= np.int64(K)
        #print(cnts_repeat)
        if S[-1] == S[0]:
            if cnts_S[0] + cnts_S[-1] == 0:
                ans[0] += np.int64(K) - np.int64(1)
            elif cnts_S[0] + cnts_S[-1] == 1:
                if S.count(S[0]) == len_S:
                    ans[0] += np.int64(K) - np.int64(1)
                elif S[0] == S[1] == S[2] == S[-1] or S[0] == S[-1] == S[-2] == S[-3]:
                    if S[0] != S[3] or S[-1] != S[-4]:
                        ans[0] += np.int64(K) - np.int64(1)
            else:
                pass
    else:
        if np.mod(np.int64(K),2) == 0:
            ans[0] = np.int64(K) // 2
        else:
            ans[0] = (np.int64(K) - 1) // 2
    print(ans[0])
    #print(cnts_S)
    #print(char_S)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
S = list(input())
N =int(input())
print(S,N)
ct=0
ctm=0
m=[]
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    if m==[]:
      m =S[i-1]
      ctm=ctm+2
      
    else:
      if S[i]==m:
        ctm=ctm+1
      
  else:
    m=S[i]
    ct=ct + int(ctm/2)
    #print(ct,int(ctm/2))
    ctm=0
    ctm=ctm+1
if ctm >0:
  ct=ct + int(ctm/2)
  
    
print(ct)
if N>1:
  if S[0]==S[-1]:
    ctm=ctm+1
    if ctm%2==0:
      print(N*ct+(N-1))
    else:
      print(N*ct)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
SS=list(S)
n=0 #文字列S内での隣接重複文字
nn=0 #最後の文字と次の最初の文字の重複を処理
nnn=0 #1-2文字目が重複した時の処理
#モノマー内の重複を修正。文字数が偶数なら同じことを繰り返すだけ
if K==1:
    for i in range(len(S)-1):
        if SS[i]==SS[i+1]:
            SS[i+1]="?"
            n+=1
    print(n)
elif len(S)%2==0:
    for i in range(len(S)-1):
        if SS[i]==SS[i+1]:
            SS[i+1]="?"
            n+=1
#つなぎの処理
    if SS[0]==SS[-1]:
        n+=1
        nn-=1
#1,2文字目と最終文字が重複した場合、2ループ目以降は1文字目を置き換えた方が効率的
        if SS[0]==SS[1]:
            SS[0]="?"
            for i in range(len(S)-1):
                if SS[i]==SS[i+1]:
                    SS[i+1]="?"
                    nnn+=1
            print(n+nnn*(K-1)+nn)
    else:
        print(n*K+nn)

else: #文字数が奇数の場合、偶数回目と奇数回目で伏せる文字を変えた方が効率的なことがあるので2重の新たな文字列で考える
    SSS=list(S+S)
    for i in range(len(SSS)-1):
        if SSS[i]==SSS[i+1]:
            SSS[i+1]="?"
            n+=1
#つなぎの処理
    if K%2==0: #Kが偶数なら末端の処理は不要
        if SSS[0]==SSS[-1]:
            n+=1
            nn-=1
            if SSS[0]==SSS[1]:
                SSS[0]="?"
                for i in range(len(S)-1):
                    if SSS[i]==SSS[i+1]:
                        SSS[i+1]="?"
                        nnn+=1
            print(n+nnn*((K//2)-1)+nn)
        else:
            print(n*K//2+nn)
    
    else: #Kが奇数なら最後の1ループは例外的に計算
        for i in range(len(S)-1):
            if SS[i]==SS[i+1]:
                SS[i+1]="?"
                n+=1
                nn-=1
        if SSS[0]==SSS[1]:
            SS[0]="?"
            for i in range(len(S)-1):
                if SS[i]==SS[i+1]:
                    SS[i+1]="?"
                    nnn+=1
            print(n+nnn*(K-1)+nn)
        else:
            print(n*(K//2)+nn)

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
SSSS=SS+SS
K=int(input())
x=K%2
c=0
cc=0
ccc=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
  if S[1]=="!":
    print(c+(K-1)*cc+K-1)
  else:
    for i in range(1,len(S)):
      if SSSS[i]==SSSS[i-1]:
        S[i]="!"
        ccc+=1
    print(ccc*(K//2)+c*x+1)

Submission status: Wrong Answer

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

def counter_func(string,x):
  try:
    if string[x] == string[x+1]:
      return 1
    else:
      return 0
  except:
    return 0
  
counter = []
count = 0
j = 0
add = 0
while j < length:
  if counter_func(s,j) == 1:
    count += 1
  else:
    counter.append(count)
    count = 0
  j += 1
if s[-1] == s[0] and k > 1:
  add = 1
sum = 0
if len(counter) != 1:
  for i in range(1,len(counter)-1):
    if counter[i] != 0:
      sum += int((counter[i]+1)/2)*k
  if add == 1 and counter[0]%2 == 1:
    sum += int((counter[-1]+1)/2)*(k-1)
    sum += int((counter[-1])/2)
    if counter[0] != 0:
      sum += int((counter[0]+1)/2)*k
  if add == 1 and counter[0]%2 == 0:
    sum += int((counter[0]+2)/2)*(k-1)
    sum += int((counter[0]+1)/2)
    if counter[-1] != 0:
      sum += int((counter[-1]+1)/2)*k
  elif add == 0:
    if counter[-1] != 0:
      sum += int((counter[-1]+1)/2)*k
    if counter[0] != 0:
      sum += int((counter[0]+1)/2)*k
else:
  sum = int((length*k)/2)
print(sum)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    ans = 0

    if len(set(S)) is 1:
        print(len(S) * K // 2)
        return

    i = 0
    while i < len(S) - 1:
        if S[i] is S[i + 1]:
            ans += 1
            i += 1
        i += 1

    if S[0] is S[-1]:
        tmp_str = S[::-1]
        cnt = 0
        i = 0
        while tmp_str[i] is tmp_str[0]:
            cnt += 1
            i += 1
        if cnt % 2 is not 0:
            ans += 1
        print(ans * K - 1)
        return

    if ans is 0 and S[0] is S[-1]:
        print(K - 1)
    else:
        print(ans * K)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
 
ans = 0
length = len(s)
 
if length == 1:
  ans = k // 2
elif length == 2:
  if s[0] == s[1]:
    ans = k
else:
  sep = 0
  i = 1
  last = 0
  firstchar = s[0]
  is_one_char = True
  while i < length:
    if firstchar != s[i]:
      is_one_char = False
    
    if s[i] == s[i-1]:
      sep += 1
      last = i
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    if last != length-1:
      lastchar = s[length-1]
      lastchar_count = 0
      j = 0
      while j < length:
        if lastchar == s[j]:
          lastchar_count += 1
          j += 1
        break
      if lastchar_count % 2 == 1:
          ans += k-1
 
print(ans)

Submission status: Wrong Answer

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


cnt = 0
i = 0

while i < len(S)-1:
    if S[i] == S[i+1]:
        cnt += 1                  
        i += 2

    elif S[0] == S[-1]:
        cnt += 1
    else:
        i += 1

print(cnt*K)

Submission status: Wrong Answer

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


def count(s):
    s_length = len(s)
    s_types = len(set(s))
    if s_length == s_types:
        return 0
    if s_length == 2:
        if s_types == 1:
            return 1
        else:
            return 0
    else:
        i = 0
        replace = 0
        while i < s_length:
            chunk = s[i:i + 3]
            chunk_types = len(set(chunk))
            if len(chunk) == 1:
                break
            elif len(chunk) == 2:
                if chunk_types == 1:
                    replace += 1
                    break
                else:
                    break
            else:
                if chunk_types == 1:
                    replace += 1
                    i += 2
                elif chunk_types == 2:
                    if chunk[0] == chunk[1]:
                        replace += 1
                        i += 2
                    elif chunk[1] == chunk[2]:
                        replace += 1
                        i += 3
                    else:
                        i += 2
                else:
                    i += 2

    return replace


# assert (count('aa') == 1)
# assert (count('ab') == 0)
#
# assert (count('aaa') == 1)
# assert (count('aab') == 1)
# assert (count('abb') == 1)
# assert (count('aba') == 0)
#
# assert (count('aaaa') == 2)
# assert (count('aaab') == 1)
# assert (count('baaa') == 1)
# assert (count('abaa') == 1)
# assert (count('abab') == 0)


def check(s, k):
    s_length = len(s)
    s_types = len(set(s))
    if (s_types == 1) or (s_length == 1):
        return s_length * k // 2
    elif s_types == s_length:
        return 0
    elif s_length == 2:
        if s[0] == s[1]:
            return k
        else:
            return 0
    elif s[0] == s[-1] and s[-1] != s[-2] and s[0] != s[1]:
        return count(s) * k + (k - 1)
    elif s_length >= 7 and s[0:3] == s[-3:]:
        return count(s) * k + (k - 1)
    else:
        return count(s) * k


print(check(S, K))

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 K <= 1:
    print(count)
elif N == 2 and S[0] == S[N-1]:
    print(count*K)
elif N == 2 and S[0] != S[N-1]:
    print(0)
elif N == 3 and S[0] == S[N-1] == S[N-2]:
    print(count + (K - 1) * (count + 1))
elif N < 4 and S[0] == S[N-1]:
    print((K-1)*(count+1))
elif N < 4 and S[0] != S[N-1] :
    print(count + (K-1)*(count+1) )
elif N >= 4 and S[0] == S[N-1] == S[N-2]:
    print(count+(K-1)*(count))
elif N >= 4 and S[0] == S[N-1]:
    print(count + (K - 1) * (count + 1))
else:
    print(count+(K-1)*(count))

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

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 em:print(S_orig[i])
  if em:print(S[len(S_orig) + i - 1])
  if em:print("----")
  if S_orig[i] != S[len(S_orig) + i]:
    score_fol = score_fol + 1

if K == 1:
  total = score_pre
  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 (S_orig[-1] != S[-1]) and (S_orig[-1] != S_orig[-2]) and (S_orig[0] != S_orig[-1]):
  total = total - 1
if em: embed()
print(int(total))

if em:print(S)



# 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:
import copy
S=list(input())
SS=copy.deepcopy(S)
SSSS=SS+SS
K=int(input())
x=K%2
c=0
cc=0
ccc=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
  if S[1]=="!":
    print(c+(K-1)*cc+K-1)
  else:
    for i in range(1,len(S)):
      if SSSS[i]==SSSS[i-1]:
        S[i]="!"
        ccc+=1
    print(ccc*(K//2)+c*x)

Submission status: Wrong Answer

Solution code:
import math
S = list(input())
K = int(input())
i = 0

for x in range(len(S)):
    if x>0:
        if S[x-1] == S[x]:
            i += 1
          
if S[0] == S[len(S)-1]:
  i += 1
                    
i = math.ceil(i/2)
if len(S)>1:
	i *= K
  
print(i)

Submission status: Wrong Answer

Solution code:
from math import *
def solve2():
    s = input()
    len_s = len(s)
    k = int(input())
    s += s
    cnt = 0
    flg = False
    for i in range(0, len_s * 2 - 1):
        if flg:
            flg = False
        elif s[i] == s[i + 1]:
            flg = True
            cnt += 1
    return ceil(cnt * k // 2)
print(solve2())

Submission status: Wrong Answer

Solution code:
import math


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

if K == 1:
    cnt = 0
    S2 = list(S)
    for i in range(1, len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt += 1

    cnt = cnt * K

    # if S2[0] == S2[-1]:
    #    cnt += K-1

    print(cnt)

else:
    S2 = list(S)
    S2 = S2 + S2
    cnt1 = 0
    for i in range(1, int(len(S2)/2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt1 += 1

    cnt2 = 0
    for i in range(int(len(S2)/2), len(S2)):
        if S2[i-1] == S2[i]:
            S2[i] = "A"
            cnt2 += 1

    S3 = S2 + S2
    if S3[len(S2)] == S3[len(S2)+1]:
        flg = 1
    else:
        flg = 0

    cnt = cnt1 * math.ceil(K/2)
    cnt += cnt2 * (K//2)

    if S2[0] == S2[-1]:
        if flg == 1:
            cnt += math.ceil(K/2) - 1

    print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

s = ns()
k = ni()

if len(s) == 1:
	print(k//2)
	exit()

for i in range(len(s)-1):
	if s[i] != s[i+1]:
		break
else:
	print((len(s)*k)//2)
	exit()

n = 0
m = 0
h = 0
p = False
for i in range(len(s)):
	if i == 0:
		if s[i] == s[-1] and s[-1] != s[-2]:
			n += 1
			if s[0] == s[1]:
				m -= 1
				h += 1
		if s[0] == s[1] and s[1] == s[2]:
			n +=1
	elif not p and s[i] == s[i-1]:
		m += 1
		p = True
	else:
		p = False

print(m*k+n*(k-1)+h)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import math

s = input()
k = int(input())

cnt = 0

ttt = [[k, len(list(g))] for k, g in groupby(s)]
tttt = [tt[1] for i,tt in enumerate(ttt) if tt[1] > 1]

t = list(s)
for tt in tttt:
    if tt % 2 == 0:
      cnt += tt / 2
    if tt %2 !=0:
      cnt += tt//2
      
if t[0] == t[-1] and len(t)%2 ==0:
  print(int(cnt)*k)
if t[0] == t[-1] and len(t)%2 != 0 and t.count(t[0]) == len(t):
  print(int(cnt)*k+1)
if t[0] == t[-1] and len(t)%2 != 0 and t.count(t[0]) != len(t):
  print(int(cnt)*k)
if t[0] != t[-1]:
  print(int(cnt)*k)