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())
L = len(S)
a = S[0]
i = 0
C = []
if L == 1:
    C = [1]
while i < L-1:
    count = 1
    b,c = S[i],S[i+1]
    i+= 1
    while b == c:
        count += 1
        if i == L-1:
            break
        b,c = S[i],S[i+1]
        i += 1
    C.append(count)
if a != S[L-1]:
    num = 0
    for co in C:
        num += co//2
    ans = int(num*K)
else:
    if len(C) == 1:
        ans  = L*K//2
    else:
        num1 = 0
        for co in C:
            num1 += co//2
        num2 = (C[len(C)-1]+1)//2
        for co in C[:len(C)-1]:
            num2 += co//2
            ans = int(num1+num2*(K-1))
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
N = int(input())
st = []
c = 0
s = S+S
j = [True]*len(s)
for i in range(0,len(s)-1):
  if s[i]==s[i+1] and j[i]==True:
    #print(i,s[i],s[i+1])
    #print(s)
    j[i+1]=False
    c+=1
a = N*c//2
k=0
if s[0]==s[-1] and j[-1]==True:
  k += N//4
print(a+k)

Submission status: Wrong Answer

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

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

else:
    S_arranged = list(S)
    for i in range(N):
        if (S_arranged[i] == S_arranged[i-1]) and (S_arranged[i-1] == S_arranged[i-2]):
            S_arranged[i-1] = "0"
            if (i >= 2):
                counts +=1
            else:
                counts += 1

    for i in range(N):
        if S_arranged[i] == S_arranged[i-1]:
            S_arranged[i-1] = "1"
            if i >= 2:
                counts += 1
            else:
                counts += 1
    if (S[0] == S[-1]) and (S[0] != S[1]) and (S[-1] != S[-2]):
        print(counts*K + adder*(K-1) -1)
    else:
        print(counts*K + adder*(K-1))

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
S2 = S[:] * 5

if len(S) == 1:
    print(K//2)
    exit()
cons = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cons += 1
        S[i+1] = '_'
cons2 = int(S[0] == S[-1])
if K == 1:
    print(cons)
    exit()
cons3 = 0
for i in range(len(S2) - 1):
    if S2[i] == S2[i+1]:
        cons3 += 1
        S2[i+1] = '_'
if cons3 != cons * 2 + cons2:
    minus = 1
else:
    minus = 0
print(cons3 * (K // 5) + cons * (K%5))

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
S += T
ans = 0
cnt = 0

if N == 1:
    print(K//2)
    exit()

for i in range(1, N):
    if S[i - 1] == S[i]:
        cnt += 1
        S[i] = "-1"
ans += cnt
if T[0] == T[-1] and S[N - 1] != "-1":
    cnt2 = 0
    for i in range(N):
        if T[i] == T[i - 1]:
            cnt2 += 1
            T[i] = "-1"
    ans += cnt2 * (K - 1)
else:
    ans += cnt * (K - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import string

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

end = len(S) -1
j = 0
k = False

if S[end] == S[0]:
    chars = list(string.ascii_lowercase)
    pre = S[end -1]
    back = S[0]
    if pre != back:
        chars.pop(chars.index(pre))
        chars.pop(chars.index(back))
    else:
        chars.pop(chars.index(pre))
    S[end] = chars[0]
    j += 1
    k = True

for i in range(1, len(S)-1):
    if S[i-1] == S[i]:
        chars = list(string.ascii_lowercase)
        pre = S[i-1]
        back = S[i+1]
        if pre != back:
            chars.pop(chars.index(pre))
            chars.pop(chars.index(back))
        else:
            chars.pop(chars.index(pre))
        S[i] = chars[0]
        j += 1
    else:
        continue

if k:
   print(j*K-1)
else:
   print(j*K)

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K // 2)
    exit()
else :
    pre = S[0]
    flg = 0
    cnt = 0
    cnt_list =[]
    
    for i in range(1, len(S)):
        cnt += 1
        if flg == 1:
            flg = 0
            pre = S[i]
            continue
        if S[i] == pre:
            cnt_list.append(cnt)
            flg = 1
        pre = S[i]
    
    if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
        print(len(cnt_list)*K)
    elif S[0] == S[-1]:
        print(len(cnt_list)*K+K)
    else :
        print(len(cnt_list)*K)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


s = S()
k = I()
L = []
cnt = 0
ret = 1
if len(s) == 1:
    print(k // 2)
else:
    for j in range(len(s) - 1):
        if s[j] != s[0]:
            break

    for l in range(len(s) - 1):
        if s[-1] != s[-1 - l]:
            break


    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            ret += 1
        else:
            if ret > 1:
                cnt += ret // 2
            ret = 1


    cnt += ret // 2


    ans = cnt * k
    if s[0] == s[-1] and j % 2 and l % 2:
        ans += k - 1


    print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
def unit(s):
    """sの両端を外した状態でいくつ変えるか
    """
    count = 0
    prev = s[0]
    for c in s[1:]:
        if c==prev:
            count += 1
            prev = None
        else:
            prev = c
    return count
if len(s)==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
elif len(s)==1:
    print(k//2)
elif not s:
    print(0)
elif s[0]!=s[-1]:
    result = k * unit(s[1:-1])
    print(result)
else:
    s0 = unit(s[1:-1])
    s1 = unit(s[1:])
    s2 = unit(s[:-1])
    s3 = unit(s)
    result = min(s1*(k-1)+s3, s2*(k-1)+s3)
    if k%2==0:
        result2 = min(s3*(k/2)+s0*(k/2-1)+s1, s2+s3*(k/2))
    else:
        result2 = min(s3*(k//2+1)+s0, s2+(s3*(k//2)+s0*(k//2-1))+s1)
    print(min(result2, result) + (k-1))

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
S=''
#for i in range(k):
#	S+=s
#print(s)
S = list(s)
#last_word=s[-1]
count=0
l_s=len(S)
code_list = 'abcdefghijklmnopqrstuvwxyz'
#print('step1')
for i in range(1,l_s):
  #print(S,count)
  if S[i]==S[i-1] and i<l_s-1:
    for j in code_list:
      if j != S[i+1] and j !=S[i-1]:
        S[i]=j
        count+=1
        break
  elif S[i]==S[i-1]:
    for j in code_list:
      if j != S[i-1] and j !=S[0]:
        S[i]=j
        count+=1
        break
if S[-1]==S[0]:
  plu=k-1
else:
  plu= 0
print(count*k+plu)

Submission status: Wrong Answer

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

s = input()
k = int(input())
flag = 1
lenth = 1
ans = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       ans += lenth//2
       lenth = 1
       flag = 1
ans += lenth//2
lenth = 1
flag = 1

ji = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag ==1:
    ji += 1
lenth = 1
flag = 1
for i in range(len(s)-1):

    if s[len(s)-i-1] == s[len(s)-1-(i+1)]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag == 1:
    ji += 1
ans = ans*k
# print(ans, ji)
if ji == 2 and s[0] == s[len(s)-1]:
    ans = ans+k-1
if len(s) == 1:
    if k == 1:
        print(0)
    elif k == 2:
        print(1)
    else:
        print(k-2)
else:
    print(ans)

Submission status: Wrong Answer

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

count = 0

if len(s) == 2 and s[0] == s[1]:
    count = k
elif len(s) == 3 and s[0] == s[1] and s[2] == s[1]:
 	count = k + 1
elif len(s) == 4 and s[0] == s[1] and s[2] == s[1]:
  	count = k *2
else:
    for i in range(int(len(s)/2)):
        tmp = s[i*2:i*2+3]
        if len(tmp) == 2:
        	if tmp[0] == tmp[1]:
	            count+=1
        elif tmp[0] == tmp[1] or tmp[2] == tmp[1]:
            count+=1

    if len(s)%2==1:
        count = count*k
    elif len(s)%2==0:
        count = (count-1)*k

print(count)

Submission status: Wrong Answer

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

n = len(S)
s = S[0]
c = 0

if S.count(s) == n:
  print((n*K)//2)
  exit()
else:
  head = 1
  for i in range(1, n):
    if S[i] == s:
      head += 1
    else:
      c += head//2
      S = S[i:]
      break
  if S[-1] == s:
    tail = 1
    for i in range(-2, -n, -1):
      if S[i] == s:
        tail += 1
      else:
        c += tail//2
        S = S[:i+1]
        break
    c += (K-1)*((head + tail)//2)
  start = 0
  s = S[start]
  S += 'X'
  for i in range(1, len(S)):
    if S[i] != s:
      c += K*((i - start)//2)
      start = i
      s = S[start]
print(c)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
strings = str(input())
k = int(input())
s = strings + strings
count = 0

for i in range(len(s)-1):
	# print('strj'+str(j))
	if s[i] == s[i+1]:
		# print(str('s[i]=') + s[i] + str(' s[i+1]=') + s[i+1])
		count += 1
		# s[i] = str(i)
		s = s[:i+1] + str('A') + s[i+2:]
		# print(s)

# print(count)
ans = count*k//2

# if k % 2 == 0:
# 	count = count * k // 2
# else:
# 	print(count*k//2)
# 	print(count//2)
# 	count = count * k // 2 - (count // 2)
if strings[0] == strings[len(strings)-1] and k % 2 == 1:
	ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy

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

A = np.zeros(1)
j = 0 

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        A[j] += 1
    else:
        j += 1
        A = np.append(A, 0)

ans = 0

if np.size(A) == 1:
    ans = (len(S) * K) // 2
    
else:   
    A = A + 1
    B = copy.deepcopy(A)
    if S[0] == S[len(S)-1]:
        B[0] += A[-1]
        A = np.delete(A, np.size(A)-1)
        ans = np.sum(A // 2) + np.sum(B // 2) * (K-1)
    else:
        ans = np.sum(A // 2) * K

print(int(ans))

Submission status: Wrong Answer

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


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1,len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

if S[0] == S[-1]:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2,len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Submission status: Wrong Answer

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

if len(S)==1:
  print(K//2)
  exit()

zcount=0
ju=0
for i in range(len(S)):
  if S[i]==S[0]:
    zcount+=1
    if i==len(S)-1:
      ju=1
  else:
    break

if ju==0:
  for i in reversed(range(len(S))):
    if S[i]==S[0]:
      zcount+=1
    else:
      break
  
if S[0]==S[1]:
  S[0]="#"
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    S[i]="#"

ans=S.count("#")
if ju==1 and len(S)%2==1:
  print((len(S)*K)//2)
  exit()
  
if S[0]=="#" and zcount%2==1 and ju==0:
  ans-=1
  if S[-2]!=S[0]:
    ans+=1

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

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
lis = []
n = 1
for i in range(len(s)-1):
    if s[i] == s[i + 1]:
        n += 1
    else:
        lis.append(n)
        n = 1
lis.append(n)
if s[0] == s[len(s) - 1]:
    if len(lis) == 1:
        m = lis[0] //2 * k
    if len(lis) >= 2:
        m = lis[0] // 2
        m = m + lis[len(lis)-1]//2
        m = m + (lis[0] + lis[len(lis)-1]) // 2 * (k - 1)
    if len(lis) >= 3:
        for i in range(len(lis) - 2):
            m = m + lis[i + 1] // 2 * k
    print(m)
else:
    m = 0
    for i in range(len(lis)):
        m = m + lis[i] // 2 * k
    print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
s = input()
k = int(input())

tmp_count = 0
continue_str = s[0]
count_in_loop = 0
half_count = 0
result = 0
for i, si in enumerate(s):
    if continue_str == si:
        tmp_count += 1
    else:
        count_in_loop += tmp_count // 2
        tmp_count = 1
        continue_str = si
    if i == len(s) - 1:
        # 全て同じ文字だった場合の処理
        if tmp_count == len(s):
            result = len(s) * k // 2
        # そうでないケース
        else:
            count_in_loop += tmp_count // 2
            if (tmp_count % 2 == 1) & (si == s[0]):
                half_count = 1
            result = count_in_loop * k + half_count * (k-1)
print(result)

Submission status: Wrong Answer

Solution code:

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


before = ""
count = 0
loop_num = 0
length_S = len(S)
for s in S:
    loop_num += 1
    if before == s:
        count += 1
        before = ""
    else:
        before = s

### If before is empty at the end of the loop, the last character should be replaced ->
### Can aboid to be same as the first character of the string
if before == S[0]:
    first_last_replace = K - 1
else:
    first_last_replace = 0
    
out = count * K + first_last_replace
print(out)

Submission status: Wrong Answer

Solution code:
import re
import sys
import copy

s = input()
k = int(input())
count01=0
count02=0

#case01
if len(s)==1:
  print(k//2)
  sys.exit(0)
  
#case02
tmp = s[0]
flag=True
for i in range(len(s)):
  if s[i]!=tmp:
    flag=False
if flag==True:
  print((len(s)*k)//2)
  sys.exit(0)

s2 = copy.deepcopy(s)
if s[0]==s[-1]:
  tmp = s[0]

  offset01=0
  while s[0]==tmp:
    offset01+=1
    s = s[1:] + s[0]
    #print(s)
  offset02=0
  while s2[-1]==tmp:
    offset02+=1
    s2 = s2[-1] + s2[:-1]
    #print(s2)
  
  count01 += (offset01//2 + offset02//2)
  k -= 1

pattern = "a+|b+|c+|d+|e+|f+|g+|h+|i+|j+|k+|l+|m+|n+|o+|p+|q+|r+|s+|t+|u+|v+|w+|x+"
result = re.finditer(pattern, s)
for res in result:
  l = res.end()-res.start()
  if l>1:
    #切り捨て
    count02 += l//2
    #print(count)

print(count01 + count02*k)

Submission status: Wrong Answer

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

cnt,ans=0,0
if len(S)==1:
  print(k//2)
  exit()
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    cnt+=1
  else:
    cnt+=1
    ans+=cnt//2
    cnt=0
ans+=(cnt+1)//2
flag=0
for i in range(len(S)-1,0,-1):
  if S[i]==S[i-1]:
    flag+=1
if len(S)!=2 and S[0]==S[-1]:
  if flag%2==0:
    print(ans*k)
  else:
    print(ans*k+k-1)
else:
  print(ans*k)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(map(int, input().split()))

import numpy as np
s = input()
k = ii()

s = list(s)
cnt = 0
if len(s) > 1:
    for i in range(len(s)):
        if i < len(s)-1 and s[i] == s[i+1]:
            s[i+1] = '?'
            cnt += 1

    if s[0] == s[-1]:
        print(cnt*k + k-1)
    else:
        print(cnt*k)
else:
    if k%2==0:
        print(int(k/2))
    else:
        print(int((k-1)/2))

Submission status: Wrong Answer

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        
        # A
        ans = 0
        if S[0] == S[-1]:
            S[-1] = 'X'
            ans += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = str(i)
                ans += 1
        ans = ans*(K-1)
        S2 = S[:] + ORG_S
        for i in range(len(S2)-1):
            if S2[i] == S2[i+1]:
                S2[i+1] = 'A'+str(i)
                ans += 1
         
        print(ans)
        return

f()

Submission status: Wrong Answer

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

count = 0

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

print(count*K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if S[0] == S[-1]:
    if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
        ans += ((K+1)//2)
if len(set(S))==1:
    ans = (len(S)*K)//2
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
S = input()
K = int(input())
res = 0
if len(S) == 1:
    res = int(K/2)
if len(S) == 2:
    if S[0] == S[-1]:
        res += 1
if len(S) > 2:
    n = 0
    while n <= len(S)-2:
        if S[n] == S[n+1]:
            res += K
            n += 1
        n += 1
    if S[0] == S[-1]:
        res += K-1
print(res)

Submission status: Wrong Answer

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

S+="*"

count=1
result=0
for x in range(len(S)-1):
    if S[x]==S[x+1]:# and S[x+1]!="*":
        count+=1
    else:
        if count>1:
             result+=int(count/2)
        count=1
    #print(S[x],":",S[x+1])
    #print(count)
    #print(result)
result*=K
print(result)

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline
S = [ord(s)-97 for s in readline().strip()]
K = int(readline())

if len(S) == 1:
    Ans = K//2
else:
    dp = [1]*26
    dp[S[0]] = 0
    inf = 10**9
    
    for s in S[1:]:
        dp2 = [inf]*26
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                dp2[i] = min(dp2[i], 1+dp[j])
        
        for j in range(26):
            if s == j:
                continue
            dp2[s] = min(dp2[s], dp[j])
        
        dp = dp2[:]
    
    ans = inf
    for i in range(26):
        if i == S[0]:
            continue
        ans = min(ans, dp[i])
    
    ma = min(dp)
    Ans = ans * (K-1) + ma
print(Ans)

Submission status: Wrong Answer

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)
if S[0]==S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Wrong Answer

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


count=0
n=len(s)
x=s[0]
y=s[-1]

if s.count(s[0])==n:
    print((n*k)//2)
    exit()

if s[0]!=s[-1]:
    for i in range(1,n):
        if s[i]==s[i-1]:
            count+=1
            s[i]='1'
    print(count*k)


else:
    countA=1
    countB=1
    v=0
    w=0
    for i in range(1,n):
        if s[i]==s[i-1]:
            countA+=1
            
        else:
            v=i
            break
    for i in range(1,n):
        if s[n-1-i]==s[n-1]:
            countB+=1
        else:
            w=n-1-i
            break
    
    for i in range(v,w+1):
        if s[i+1]==s[v]:
            count+=1
            s[i+1]='1'
    print(count*k+(countA+countB)*(k-1)//2+countB//2)

Submission status: Wrong Answer

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


result = 0

# S内の重複
dup_c = 0
c_list = ['XXXXX']
for s in S:
    if c_list[0] == s:
        dup_c += 1
        c_list = ['XXXXX']
    else:
        c_list[0] = s
result = dup_c * K

# 間の重複
if len(S) > 1:
#    if S[0] == S[len(S)-1]:
#        if S[1] != S[0] and S[len(S)-2] != S[0]:
#            result += K - 1
    #後ろから同じ文字をカウント
    Sr = sorted(S, reverse=True)
    same_s = 0
    for i in range(len(Sr)):
        if Sr[0] == Sr[i]:
            same_s += 1
        else:
            break
    
    if same_s % 2 ==1:
        result += K - 1

else:
    result += K//2

print(result)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
brob = 0
temp = 1
first = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        temp += 1
    else:
        if first == 0:
            first = temp
        brob += temp // 2
        temp = 1
        
brob += temp // 2
last = temp

if K == 1:
    print(brob)

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

    elif last % 2 and first % 2:
        print(brob * K + K - 1)

    else:
        print(brob * K)

Submission status: Wrong Answer

Solution code:

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

str_in=S

count=0



str_in=list(str_in)
for i in range(1,len(str_in)):
    if(str_in[i-1]==str_in[i]):
        str_in[i]="a"
        count+=1

print(count*K)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
#ts=time.time()
#sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
show_flg=True

def naive(b,k):
    a=b*k
    n=len(a)
    
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
            
    #print()
    return t


b=StoI()
a=b+[b[0]]
k=I()
n=len(a)
p=a[0]
t=0

if len(b)*k<10**5:
    print(naive(b,k))
    exit()


for i in range(n-1):
    if p==a[i+1]:
        t+=1
        p=-1
    else:
        p=a[i+1]

ans=t*k
if n==2:
    ans=k//2
if len(Counter(b))==1:
    ans=(k*len(b))//2
if b[0]==b[-1]:
    ans=naive(b[1:-1],1)*k+k-1
print(ans)
#show(a,k)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def get_abc(s1,s2):
    if s1 == 'a':
        if s2 == 'b':
            return 'c'
        else:
            return 'b'
    elif s1 == 'b':
        if s2 == 'a':
            return 'c'
        else:
            return 'a'
    else:
        if s2 == 'a':
            return 'b'
        else:
            return 'a'

count = 0
for i in range(s_len):
    if pre_character == S_list[i]:
        count += 1
        if (s_len - 1) != i:
            S_list[i] = get_abc(pre_character,S_list[i+1])
        else:
            S_list[i] = get_abc(pre_character,S[0])
    pre_character = S_list[i]
if S_list[0] == S_list[-1]:
    pre_character = S_list[-1]
    S_list=list(S)
    second_count = 0
    for i in range(s_len):
        if pre_character == S_list[i]:
            second_count += 1
            if (s_len - 1) != i:
                S_list[i] = get_abc(pre_character,S_list[i+1])
            else:
                S_list[i] = get_abc(pre_character,S[0])
        pre_character = S_list[i]
    print(count + (second_count*(K-1)))
else:
    print(count*K)

Submission status: Wrong Answer

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

    previous = S[0]
    i = 0
    continue_dict = {
        0: 0
    }
    for s in S:
        if s == previous:
            continue_dict[i] += 1
        else:
            i += 1
            continue_dict[i] = 1
        previous = s

    # head + tail
    count_ht = 0
    for l in continue_dict.values():
        count_ht += l // 2

    if K > 1:
        # between
        if S[0] == S[-1] and len(continue_dict) > 1:
            continue_dict[0] += continue_dict[i]
            continue_dict.pop(i)

        count_btw = 0
        for l in continue_dict.values():
            count_btw += l // 2

        ans = count_ht + count_btw * (K-1)
    else:
        ans = count_ht

    print(ans)


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from decimal import *
S = input()
K = Decimal(input())
fletter = 0 # 最初の文字の連続数を数える
lletter = 0 # 最後の文字の連続数を数える
cletter = 0 # 現在連続している文字の連続数を数える
cng = 0 # 文字変更の必要がある文字をカウント
for i in range(len(S)): # 最初の文字の連続数を数える
    if S[0] == S[i]:
        fletter += 1
    else:
        break
for i in range(len(S)):
    if S[-1] == S[-(i + 1)]:
        lletter += 1
    else:
        break
for i in range(len(S) - 1): # 1文字ずつ連続しているかを判定
    if S[i] == S[i + 1]:
        cletter += 1
    else:
        cng -= - cletter // 2
        cletter = 0
cng -= - cletter // 2 # 最終文字まで到達したら文字変更数の処理
if S[0] == S[-1] and fletter % 2 == 1 and lletter % 2 == 1:
    ans = Decimal((cng + 1) * K - 1)
else:
    ans = Decimal(cng * K)
print(ans) #解答の出力

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
S2 = S[:] * 2

if len(S) == 1:
    print(K//2)
    exit()
cons = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cons += 1
        S[i+1] = '_'
cons2 = int(S[0] == S[-1])
if K == 1:
    print(cons)
    exit()
cons3 = 0
for i in range(len(S2) - 1):
    if S2[i] == S2[i+1]:
        cons3 += 1
        S2[i+1] = '_'
if cons3 != cons * 2 + cons2:
    minus = 1
else:
    minus = 0
print(cons3 * (K // 2) + cons * (K%2))

Submission status: Wrong Answer

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

if not (1 <= len(S) and len(S) <= 100):
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()

pre = ""
count = 0
first = S[0]
last = S[len(S)-1]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if pre != S[i] and first == S[i]:
            flag = True
    if pre != S[i]:
        pre = S[i]

    elif pre == S[i]:
        count += 1
        pre = ""


count *= int(K)
if flag:
    count = count + 1*int(K) -1 
print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0

import sys
if len(s) == 1:
    if k == 1:
        print(0)
    else:
        print(k // 2)
    sys.exit(0)

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

print(count * k)

Submission status: Wrong Answer

Solution code:
def count(S):
    ans = 0
    i = 1
    while i < len(S):
        if S[i-1] == S[i]:
            i += 1
            ans += 1
        i += 1
    return ans

def main():
    S = input()
    K = int(input())

    if len(S) == 1:
        if K == 1:
            print(0)
            exit()
        else:
            print(K//2)
            exit()

    if K == 1:
        print(count(S))
        exit()

    ans = count(S*2)
    ans *= K // 2
    if K % 2 == 1:
        ans += count(S)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s = input()
k = int(input())
flag = 1
lenth = 1
ans = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       ans += lenth//2
       lenth = 1
       flag = 1
ans += lenth//2
lenth = 1
flag = 1

ji = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag ==1:
    ji += 1
lenth = 1
flag = 1
for i in range(len(s)-1):

    if s[len(s)-i-1] == s[len(s)-1-(i+1)]:
        lenth += 1
    else :
        flag = 0
    if flag == 0:
       # print(lenth)
       if lenth%2 == 1:
            ji += 1
       break
if lenth%2 == 1 and flag == 1:
    ji += 1
ans = ans*k
# print(ans, ji)
if ji == 2 and s[0] == s[len(s)-1]:
    ans = ans+k-1
if len(s) == 1:
    if k < 2:
        print(1)
    else:
        print(k-2)
else:
    print(ans)

Submission status: Wrong Answer

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

import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """

    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
if len(s)==1:
	print(k//2)
elif len(s)==2:
	if len(set(s))==1:
		print(k)
	else:
		print(0)
elif len(s)==3:
	if len(set(s))==1:
		print(3*k//2)
	elif len(set(s))==2:
		print(k)
else:
    if s[0]!=s[-1]:
        a=list(nchars(s, 2))
        b=0
        for i in a:
            b+=len(i)//2
        print(b*k)
    else:
        if s[0]!=s[1] and s[-1]!=s[-2]:
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            print(b*k+k-1)
        else:
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            print(b*k)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if len(S) == 1:
    print(int(K/2))
else:    
    count = 0
    bef = ""
    isbefchange = False
    for i in range(0, len(S)):
        if(isbefchange == False and S[i] == bef):
            count += 1
            isbefchange = True
            bef = S[i]
            continue
        bef = S[i]
        isbefchange = False
    count *= K
    if(isbefchange == False and S[len(S)-1] == S[0]):
        count += (K-1)
    print(count)