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:
# -*- coding: utf-8 -*-

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

another_c = ''
cnt = 0


t = list(s)
for i, c in enumerate(t):
    prev_c = t[i-1]
    next_c = t[(i+1)%len(t)]
    if i > 0 and c == prev_c:
        another_c = chr(ord(c) + 1)
        if ord(another_c) > ord('z'):
            another_c = chr(ord(another_c)-26)
        if next_c == another_c:
            another_c = chr(ord(another_c) + 1)
        t[i] = another_c
        cnt = cnt + 1
    else:
        another_c = ''
    #print(i, prev_c, c, next_c, "->", another_c) 

#print("".join(t))
end = t[-1]

if k > 2:
    cnt2 = 0
    t = list(s)
    for i, c in enumerate(t):
        if i == 0:
            prev_c = end
        else:
            prev_c = t[i-1]
        next_c = t[(i+1)%len(t)]
        if c == prev_c:
            another_c = chr(ord(c) + 1)
            if ord(another_c) > ord('z'):
                another_c = chr(ord(another_c)-26)
            if next_c == another_c:
                another_c = chr(ord(another_c) + 1)
            t[i] = another_c
            cnt2 = cnt2 + 1
        else:
            another_c = ''
        #print(i, c, another_c)
    cnt = cnt + (cnt2 * (k-2))
        #print("".join(t))

if k > 1:
    t = list(s)
    for i, c in enumerate(t):
        if i == 0:
            prev_c = end
        else:
            prev_c = t[i-1]
        next_c = t[(i+1)%len(t)]
        if c == prev_c:
            another_c = chr(ord(c) + 1)
            if ord(another_c) > ord('z'):
                another_c = chr(ord(another_c)-26)
            if next_c == another_c:
                another_c = chr(ord(another_c) + 1)
            t[i] = another_c
            cnt = cnt + 1
        else:
            another_c = ''
        #print(i, prev_c, c, next_c, "->", another_c) 

    #print("".join(t))

print(cnt)

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 (arr[0]%2==1 or arr[-1]%2==1):
    if S[0]==S[-1]:
        print((ans+1)*K)
        print('A')
    else:
        print((ans+1)*K-1)
        print('B')
elif len(arr)==1 and N%2==1:
    if N==1:
        print(K//2)
        print('C')
    else:    
        print((ans+1)*K-1)
        print('D')
else:
    print((ans)*K)

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
K = int(input())
count = 0
last_count = 0
if len(S_list) % 2 == 1 and len(set(S_list)) == 1:
    print((len(S_list) * K) // 2)
elif K > 1:
    S_list.append(S_list[0])
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            if idx == len(S_list) - 2:
                last_count = 1
            else:
                count += 1
                S_list[idx + 1] = "*"
    print(count * K + last_count * (K-1))
else:
    for idx, s in enumerate(S_list):
        if idx == len(S_list) - 1:
            break
        if S_list[idx + 1] == s:
            count += 1
            S_list[idx + 1] = "*"
    print(count * K)

Submission status: Wrong Answer

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

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

cnt=1
ans1=0
c=[]
for i in range(1,LS-1):
    if S[i-1]==S[i]:
        cnt+=1
    elif cnt!=1:
        ans1+=cnt//2
        c.append(cnt)
        cnt=1

if K==1:
    if S[-2]==S[-1]:
        cnt+=1
        ans1+=cnt//2
else:
    if S[-2]==S[-1]:
        cnt+=1
        c.append(cnt)
        if cnt%2==0:
            if ans1!=0:
                ans1+=cnt//2
                ans1=ans1*K
            else:
                ans1=(cnt//2)*K
        elif cnt%2==1 and S[0]==S[-1]:
            if ans1!=0:
                if c[0]%2==0:
                    ans1+=cnt//2
                    ans1=ans1*K
                else:
                    ans1+=cnt//2+1
                    ans1=ans1*K-1
            else:
                ans1=(cnt//2)*K+K-1
        elif cnt%2==1 and S[0]!=S[-1]:
            ans1+=cnt//2
            ans1=ans1*K
    else:
        ans1+=cnt//2
        c.append(cnt)
        c.append(1)
        if S[0]==S[-1]:
            if ans1!=0:
                ans1=ans1*K+K-1
            else:
                ans1=K-1
        else:
            ans1=ans1*K
print(ans1)

Submission status: Wrong Answer

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

i = 1

ops2 = 0

ss = s + s

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

i = 1

ops3 = 0

sss = ss + s

while i < len(sss):
    if sss[i - 1] == sss[i]:
        ops3 += 1
        i += 1
    i += 1

ops = (k // 2) * ops2 + (k % 2) * (ops3 - ops2)

print(ops)

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
scr = 0

while i != b:
    a = 1
    aa = 0
    while S[j] == S[j+1]:
        i += 1
        j += 1
        a += 1
    i += 1
    j += 1
    scr += a//2
    if aa == 0:
        a_s = a
    aa = 1
if S[0] == S[-2]:
    if a_s >= 2 and a_s%2 == 1:
        if a >= 2 and a == 1:
            x = K-1

print(scr*K+x)

Submission status: Wrong Answer

Solution code:
import copy
S=list(input())
SS=copy.deepcopy(S)
K=int(input())
c=0
cc=0
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 = list(input())
k = int(input())
ss = s + s

def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

kk = k//2

if k%2 == 0: 
    answer, string = counter(ss)
    if string[0] == string[-1]:
        print(answer*kk+kk-1)
    else:
        print(answer*kk)
else:
    answer, string = counter(s)
    answer2, string2 = counter(ss)

    if string2[0] == string2[-1]:
        if string[0] == string[-1]: 
            print(answer2*kk + answer + kk)
        else:
            print(answer2*kk + answer + kk-1)

    else:
        if string[0] == string[-1]: 
            print(answer2*kk + answer + 1)
        else:
            print(answer2*kk + answer)

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 += k -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())

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 = []
ans = 0
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
            ans += (e_idx - s_idx + 1) // 2
            idx_list.append((s_idx, e_idx))
else:
    if is_start:
        e_idx = len(s) - 1
        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)

Submission status: Wrong Answer

Solution code:
import math

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

changes = 0
c = 0
subgroups = []
subgroups.append(1)
for i in range(1, len(S)):
    if S[i - 1] != S[i]:
        c += 1
        subgroups.append(1)
    else:
        subgroups[c] += 1

total = 0
for g in subgroups:
    total += math.floor(g/2) 

if S[0] != S[-1] or len(subgroups) == 1:
    print(K*total)
else:
    total_corners = math.floor((subgroups[0] + subgroups[-1])/2)
    total_lite = total - total_corners
    print(total + total_lite * (K - 1) + total_corners * (K - 1))

Submission status: Wrong Answer

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


def c(ss, k):
    s = list(ss)
    s *= k
    n = len(s)
    ret = 0
    for i in range(n - 2):
        if s[i] == s[i + 1] == s[i + 2]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    return ret


x1 = c(S, 1)
x2 = c(S, 2)
print(x1 + (x2 - x1) * (K - 1))

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)
elif K*len(S) %2 == 1:
    print(cnt_2+(cnt_3-cnt_2)*(K-2))
else:
    print(cnt_2*(K//2))

Submission status: Wrong Answer

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

count = 0
count2 = 0

if len(S)%2 == 1:
    for i in range(len(S)//2-1):
        if S[2*i+1] == S[2*i] or S[2*i+1] == S[2*i+2]:
            count +=1
else:
    for i in range(len(S)//2-1):
        if S[2*i] == S[2*i+1]:
            count +=1

if S[len(S)-2] == S[len(S)-1]:
    count+=1
else:
    if S[0] == S[len(S)-1]:
        count2 = 1

print(count*K+count2*(K-1))

Submission status: Wrong Answer

Solution code:
S, K = open(0).read().split()

cGroups = []
tempList = []
for i, c in zip(range(len(S)), S):
    tempList.append(c)
    if (i == len(S) - 1) or c != S[i + 1]:
        cGroups.append(tempList)
        tempList = []

singleResult = 0
for g in cGroups:
    if len(g) > 1:
        singleResult += len(g) // 2

result = singleResult * int(K)

if len(cGroups[0]) % 2 == 1 and len(cGroups[-1]) % 2 == 1:
    result += int(K) - 1

print(result)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
A = []
b = 1
if s[0] != s[-1]:
  for i in range(len(s)-1):
    if s[i] == s[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  print(k*sum(a//2 for a in A))
elif all(a  == s[0] for a in s):
  print((k*len(s))//2)
else:
  t = s[0]
  ahead = 0
  back = 0
  ans = 0
  for i in range(len(s)):
    if s[i] == t:
      ahead += 1
    else:
      break
  for i in reversed(range(len(s))):
    if s[i] == t:
      back += 1
    else:
      break
  ans += ahead//2 + back//2 + (k-1)*(ahead+back)//2
  l = s[ahead:-back]
  for i in range(len(l)-1):
    if l[i] == l[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  ans += (k*sum(a//2 for a in A))
  print(ans)

Submission status: Wrong Answer

Solution code:
import collections
S =[i for i in input()]
K = int(input())
a = 1
b = []
c = []
SS = len(S)
result = 0
if S[0] != S[-1]:
  try:
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  print(result*K)
elif len(collections.Counter(S)) == 1:
  print(len(S)*K//2)
else:
  result2 = 0
  for j in range(len(S)):
    if S[j] == S[j + 1]:
      a += 1
    else:
      c.append(a)
      a = 1
      break
  S2 = S[c[0]:]
  try:
    for j in range(len(S2)):
      if S[j] == S[j + 1]:
        a += 1
      else:
        c.append(a)
        a = 1
  except:
    c.append(a)
  S = S+S
  S = S[c[0]:-(SS-c[0])]
  try:
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  for l in c:
    result2 += l//2
  print(result*(K-1)+result2)

Submission status: Wrong Answer

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

count = 0
S = S + "0"

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

S = S[:-1] #最後の0を削除

if len(S) == 1:
    result = K
elif len(S) == 2:
    result = count * K
else:
    if S[0] == S[-1]:
        if S[0] != S[1] and S[-1] != S[-2]:
            result = count * K + (K-1)
            #print("if1")
        elif S[0] == S[1] == S[2] and S[-1] != S[-2]: 
            result = count * K + (K-1)
            #print("if2")
        elif S[-1] == S[-2] == S[-3] and S[0] != S[1]: 
            result = count * K + (K-1)
            #print("if3")    
        else:
            result = count * K
            #print("else1")
    else:
        result = count * K
        #print("else2")


print(result)

Submission status: Wrong Answer

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

import re

def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

if len(S)==1:
  print(K//2)
  
elif S[0]!=S[-1]:
  S_l = list(nchars(S,2))
  answer = 0
  for s in S_l:
    answer += len(s)//2
  print(answer*K)

else:
  if (S[0]!=S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2

    answer = S_lr + (1+S_lr)*(K-1)
    print(answer)
  
  elif (S[0]!=S[1])&(S[-1]==S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[-1])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
  
  elif (S[0]==S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[0])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
    
  else:
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[0])
    b=len(S_l[-1])
    answer = S_lr + ((a+b)//2 + (S_lr-a//2-b//2))*(K-1)
    print(answer)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = 0
TTL_num = 0
first = 0

for i in range(len(S)-1):
	if S[i] == S[i+1]:
		if i <= (len(S)-3):
			for j in range(len(A)):
				if S[i] != A[j] and S[i+2] != A[j]:
					S[i+1] = A[j]
					num += 1
					break
		if i == (len(S)-2):
			for k in range(len(A)):
				if S[i] != A[k] and S[0] != A[k]:
					S[i+1] = A[k]
					num += 1
					break
	if i == (len(S)-1) and S[i+1] == S[0]:
		for l in range(len(A)):
			if S[0] != A[l]:
				S[i+1] = A[l]
				num += 1
				first += 1
				break

if first == 1:
	TTL_num = num*K -1
else:
	TTL_num = num*K

print(TTL_num)

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


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


print(check(S, K))

Submission status: Wrong Answer

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

if len(S) == 1:
    print(K//2)
    exit()
L = []
LS = []
ans = 0
streak = 1
s = ''

for i in range(len(S)-1):
    if i == 0:
        s = S[0]
        streak = 1
    
    if s == '':
        if S[i+1:i+2] == S[i:i+1]:
            s = S[i:i+1]
            streak += 1
    else:
        if S[i+1:i+2] == s:
            streak += 1
        else:
            L.append(streak)
            LS.append(s)
            s = ''
            streak = 1
    
    if i == len(S)-2:
        L.append(streak)
        LS.append(S[-1])
        # if streak == len(S):
        #     ans = K*(len(S)//2)
        #     print(ans)
        #     exit()

# print(LS)
# print(L)
if streak == len(S):
    ans = K*(len(S)//2)
    print(ans)
    exit()

if LS[0] != LS[-1]:
    for l in L:
        if l >=2:
            ans += l//2
    ans *= K
elif LS[0] == LS[-1]:
    for i in range(1,len(L)-1):
        ans += L[i]//2
    ans = K*ans + L[0]//2 + (K-1)*(L[0]+L[-1])//2 + L[-1]//2
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
ans=0
if len(set(list(s)))==1:
  ans=(k*len(s))//2
  print(ans)
  exit()
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    ans+=1
    s=s[:i+1]+'0'+s[i+2:]
ans*=k
if s[0]==s[-1]:
  ans+=k-1

print(ans)

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]
        if len(s) >= 3 and s[0] == prev:
            res += (d - 1)
            if rest:
                res += 1
        print(res)

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):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
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:
S=input()
K=int(input())
T0=0
count=1
ans=0
l=0
c0=0
 
for i in range(len(S)):
	if S[i]==T0:
		count+=1
	else:
		ans+=count//2
		l+=1
		if l==2:
			c0=count
		count=1
	T0=S[i]
 
if S[0]==S[-1]:
	ans-=c0//2
	a=count+c0
	b=c0//2
else:
	a=count
	b=0

print(b+ans*K+(a//2)*(K-1)+count//2)

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] or S[0] in box[0]:
        result = (all_temp * K)
    else:
        result = (all_temp * K) + (K - 1)
    
print(int(result))

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] = ""
    
if S[0] == S[len(S)-1]:
  print(count*K + K)
else:
  print(count*K)

Submission status: Wrong Answer

Solution code:
#l=list(map(int, input().split()))
#n,m=map(int, input().split())
#l=list(map(int, input().split()))
#n = int(input())
def map_all(s):
    temp = s[0]
    
    for i in range(1,len(s)-1):
        if (temp != s[i]):
            return False

    return True

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

if map_all(s):
    temp = k * len(s)
    print(temp//2)
    exit()

le = len(s) -1
ans = []
temp = 0
de = True
for i in range(le):
    if s[i] == s[i+1]:
        temp+=1
        if (de == True):
            de = False
    else:
        if (de == False):
            ans.append(temp)
            temp = 0
            de= True
if (temp != 0):
    ans.append(temp)

#print(ans)
ansVal = 0
for i in ans:
    temp = (i+1)//2
    ansVal+=temp*k

print(ansVal)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
#S = list("cbccc")
#K = 2
# S = list("aabca")
# K = 2
if S.count(S[0]) == len(S):
    print(len(S) * K // 2)
    exit()

cnt = 0
continuous = 0
chr = S[0]
for i in range(1, len(S)):
    if chr == S[i]:
        cnt += 1
    if chr != S[i] or i == len(S) - 1:
        continuous += (cnt + 1) // 2
        cnt = 0
    chr = S[i]
chr = S[0]
left = 0
idx = 0
while idx < len(S) and S[idx] == chr:
    left += 1
    idx += 1
idx = -1
right = 0
while idx < len(S) and S[idx] == chr:
    right += 1
    idx -= 1
same = right + left
if continuous == 0 and S[0] == S[-1]:
    print(K - 1)
else:
    if S[-1] == S[0] and S[0] == S[1] or S[-1] == S[0] and S[0] == S[-2] and left % 2 == 0 and right % 2 == 0:
        print(K * continuous)
    #elif S[0] == S[-1] and S[-2] != S[0] and S[2] != S[0]:
    elif S[0] == S[-1] and same % 3 != 0:
        print(K * continuous + K - 1)
    else:
        print(K * continuous)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt


if K == 1:
	print(connect_cnt(S))
	exit(0)

if len(S) == 1:
	print(connect_cnt(S*K))
	exit(0)

if S[0] != S[-1]:

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:
	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K + K - 1
	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)
    if last_chk == 1: score = score - 1
    total = score * K
    # if em: embed()
    S3 = [S2[-1]] + S_orig
    score3, last_chk3, S4 = calc(S3,1)
    if last_chk3 == 1: score3 = score3 - 1
    print(total + score3)


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

# print(score*K)


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

Submission status: Wrong Answer

Solution code:
s = 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(a[0]*k//2))
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:
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:
    pass
elif 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 sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		# 文字列の入力  abcdefg...
k = int(input())						# 5


ans=0

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

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)



# グループ作成
#「groupby」は、「キーとグループ」を返すようなイテレータを作成します
from itertools import groupby
#s='AAABBCABBB'

#「groupby」は、キーとグループを返すようなイテレータを作成します
m=[]
for key, g in groupby(s):
	#print( key, list(g) )
	m.append([key,len(list(g))])

p(m)

for a,i in m:
	#p(a,i)
	ans+=(i//2)*k

if m[0][0]==m[-1][0]:
	if m[0][1]%2==1:
		if m[-1][1]%2==1:
			ans+=(k-1)

print(ans)

Submission status: Wrong Answer

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

count = 0
count2 = 0

if len(S)%2 == 1:
    for i in range(len(S)//2-1):
        if S[2*i+1] == S[2*i] or S[2*i+1] == S[2*i+2]:
            count +=1
else:
    for i in range(len(S)//2-1):
        if S[2*i] == S[2*i+1]:
            count +=1

if S[len(S)-2] == S[len(S)-1]:
    count+=1
else:
    if S[0] == S[len(S)-1]:
        count2 = 1

print(count*K+count2)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
if q[-1]%2==0:
	cnt=True
else:
	cnt=False
ans=0
if cnt==True:
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)
	exit()
if len(q)==1:
	print(len(s)*k//2)
	exit()
for i in range(len(q)-1):
	ans+=q[i+1]//2
ans*=k
ans+=q[0]//2
if s[0]==s[-1]:
	ans+=(q[0]+q[-1])//2*(k-1)
	ans+=q[-1]//2
	print(ans)
else:
	ans=0
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)