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:
def resolve():
    s = input()
    k = int(input())
    c = 1
    ls = len(s)
    if len(set(s)) == 1 and ls % 2 == 1:
        if k % 2 == 0:
            ans = ls * (k // 2)
        else:
            ans = ls * (k // 2) + ls // 2
        print(ans)
    else:
        ans = 0
        for i in range(len(s) - 1):
            if s[i] == s[i + 1]:
                c += 1
            else:
                ans += c // 2
                c = 1
        ans += c // 2
        ans *= k
        if s[-2] != s[-1] and s[0] == s[-1] and s[0] != s[1]:
            ans = k - 1
        print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

s_list = []
times = 0
flow = 0

s = s[len(s)-1] + s



for i in range(len(s)):
    s_list.append(s[i])


while True:
    if s_list[flow] == s_list[flow + 1]:
        times += 1
        flow += 2
    elif s_list[flow] != s_list[flow + 1]:
        flow += 1

    if flow >= len(s) - 2:
         break

print(times*k)

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 <= 10 ** 7:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

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)
    # print(l)
count *= int(K)
if len(L) == 1:
    if len(L[0]) == 1:
        count = int(int(K) / 2)
    elif len(L[0]) % 2 == 1:
        count += 1
    
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 + int(K) - 1
print(count)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
# 隣り合う文字列を置換した回数をカウント
cnt = 0
for i in range(len(s) - 1):
  # 末尾1文字前まで
  if s[i] == s[i + 1]:
    s[i + 1] = str(i)
    cnt += 1

cnt = cnt * k
    
# 末尾が置換されていなくて、先頭と同じならk-1を足す
try:
  float(s[-1])
except ValueError:
  cnt += k - 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
if N == 2:
    exit()
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if (S[0] == S[N-1]) and K != 1:
    if S[0] != S[1]:
        cnt += 1
        flg1 = True
if (S[N-2] == S[N-1]):
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
len_cnt = len(s)
#print(len_cnt)
submit = 0
tokusyu = 0
add = 0
if len_cnt == 1:
    print(k//2)
else:
    ato = 1
    while cnt < len_cnt-1:
        #print(len_cnt)
        if s[cnt] == s[cnt+1]:
            ato = cnt+1
            while s[cnt] == s[ato]:
                ato+=1
                #print("ここ",ato)
                if len_cnt == ato:
                    if len_cnt == ato-cnt:
                        tokusyu = 1
                        cnt = len_cnt
                        break

                    submit = submit + (ato-cnt)//2
                    cnt = len_cnt
                    break
            submit = submit + (ato-cnt)//2
            #print((ato-cnt)//2)
            cnt = ato
        else:
            cnt+=1
#print(submit)
#print(len_cnt)
    if tokusyu == 0:
        #print(len_cnt)
        if s[0] == s[-1]:
            add = k-1
            print(submit*k)
        else:
            #print(len_cnt)
            print((len_cnt*k)//2)

Submission status: Wrong Answer

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

tail = s[0]
plus = 0
flg = False
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      break
  for i in s[::-1]:
    if tail == i:
      tmp_cnt2 += 1
    else:
      break
  if (tmp_cnt1 % 2) == 1 and (tmp_cnt2%2)==1:
    plus = 1
  if tmp_cnt1 == len(s):
    flg = True

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
tmp = input()
S = list(tmp.strip())
K = int(input())
#S = S * K
lens = len(S)


#print(S)
count = 0
for i in range(1, lens):
    if S[i-1] == S[i]:
        count += 1
        S[i] ='X'
if S[0] == S[lens-1]:
    count = count * K + (K-1)
else:
    count = count * K

print(count)

Submission status: Wrong Answer

Solution code:
import copy

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

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    if st[-1] != st[-2]:
        li.append(1)
    return li

if len(s) == 1:
    lin = [1]
else:
    lin = cou(s)
if len(lin) == 1:
    print(lin[0]*k//2)
    exit()

lin2 = cou(s)

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]
else:
    if len(s) == 1:
        print(0)
        exit()

point = 0
point2 = 0
point3 = 0

for i in range(1,len(lin)):
    point3 += lin[i]//2

for w in range(1,len(lin2)):
    point2 += lin2[w]//2

point += (point3*(k-1))
point += point2
point += lin2[0]//2

print(point)

Submission status: Wrong Answer

Solution code:
import sys
import os

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

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

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 = (len(S)*K)//2
    # print(len(S))
    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:
def main():
    S = input()
    K = int(input())
    ans = 0

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

    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

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

    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())
n = len(s)
l = []

t = set(list(s))
if len(t) == 1:
    if k % 2:
        print(k // 2 * n + n // 2)
    else:
        print(n // 2 * k)
    exit()
    
x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i // 2
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)

Submission status: Wrong Answer

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

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

if K==1:
  cnt = 0
  for i in range(len(S)-1):
    if S[i]==S[i+1]:
      S[i+1]=0
      cnt+=1
  print(cnt)
  exit()
    
SW=S*2
cnt1=0
p,q=divmod(K,2)
for i in range(len(SW)-1):
  if SW[i]==SW[i+1]:
    SW[i+1]=0
    cnt1+=1

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

if SW[0] != SW[-1]:
  ans = cnt1*p + cnt2*q
else:
  ans = cnt1*p + cnt2*q + (p+q-1)
print(ans)

Submission status: Wrong Answer

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

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

another_c = ''
cnt = 0


t = list(s)
start = t[0]
#for i, c in enumerate(t):
for i in range(len(t)):
    c = t[i]
    prev_c = t[i-1]
    if i == len(t)-1:
        next_c = start
    else:
        next_c = t[i+1]
    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 > 1:
    cnt2 = 0
    t = list(s)
    #for i, c in enumerate(t):
    for i in range(len(t)):
        c = t[i]
        if i == 0:
            prev_c = end
        else:
            prev_c = t[i-1]
        if i == len(t)-1:
            next_c = start
        else:
            next_c = t[i+1]
        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-1))
        #print("".join(t))


print(cnt)

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 <= 10 ** 5:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

Solution code:
s=str(input())
k=int(input())
t=str()
cnt=0

for i in range(len(s)):
  if s[i-1] == s[i]:
    cnt+=1
if s[0] == s[-1]:
  a=k-1
else:
  a=0

print(cnt*k+a)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
alphbef = ""
alphbefbef = ""
sameamt = 0
for alph in s:
    if alph == alphbef:
        if alph != alphbefbef:
            sameamt += 1
    if alph != alphbefbef:
        alphbefbef = alphbef
    else:
        alphbefbef = ""
    alphbef = alph
ans = sameamt * k
if s[0] == s[len(s) - 1]:
    if s[0] != s[len(s) - 2]:
        ans += k - 1
print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
ans = 0
a = 0
if S[0] == S[-1]:
  i = 0
  j = 0
  for k in range(len(S)//2):
    if S[0] == S[k]:
      i += 1
    if S[0] == S[-k-1]:
      j += 1
    if S[0] != S[k] and S[0] != S[-k]:
      break
  if i%2 == 1 and j%2 == 1:
    a = 1
  else:
    a = 0
     
if len(S) == S.count(S[0]):
  print(len(S)*K//2);exit()
elif len(S) == 2:
  print(0);exit()
else:
  for i in range(0,len(S)-2,1):
    s = S[i:i+3]
    if s[0] == s[1] and s[1] == s[2]:
      ans += 1
      S[i+1] = "0"
    elif s[0] == s[1]:
      ans += 1
      S[i+1] = "0"
    elif s[1] == s[2]:
      ans += 1
      S[i+2] = "0"
if S[0] == S[-1]:
  print(ans*K+K-a)
else:
  print(ans*K)

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 cGroups[0][0] == cGroups[-1][0] and len(cGroups[0]) % 2 == 1 and len(cGroups[-1]) % 2 == 1:
    result += int(K) // 2

print(result)

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 <= 50:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

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 = 0
  last = 0
  firstchar = s[0]
  is_one_char = True
  while i < length-1:
    if firstchar != s[i] or firstchar != s[i+1]:
      is_one_char = False
    
    if s[i] == s[i+1]:
      sep += 1
      last = i
      i += 1
    i += 1
  
  if is_one_char:
    ans = (length // 2) * k
    if length % 2 != 0:
      ans += k // 2
  else:
    ans = sep * k
    if s[length-1] != s[length-2] or (s[length-1] == s[length-2] and s[length-1] == s[length-3]):
      if s[0] == s[length-1] and s[0] != s[1]:
        ans += k-1
 
print(ans)

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

Submission status: Wrong Answer

Solution code:
import sys
import fractions
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
 
s = input()
k = INT()
res = 0
suc = True
for i in range(len(s)-1):
    if s[i] != s[i+1]:
        suc = False


def cal(s):
    count = 0
    flag = False
    i = 0

    while i < (len(s)-1):
        if s[i] == s[i+1]: 
            if i == len(s)-2:
                flag = True
            count += 1
            i += 2
        else:
            i += 1
    return count


ss = s[-1] + s
res1 = cal(s)
res2 = cal(ss)

if suc:
    if len(s)%2 == 0:
        res = res1 * k
    else:
        if k % 2 == 0:
            res = (res1 + res2) * k //2
        else:
            res = (res1 + res2) * (k-1) // 2 + res1

elif s[0] == s[1] and s[0] == s[-1] and s[-1] != s[-2]:
    res = res1 + res2*(k-1)
elif s[0] != s[1] and s[0] == s[1] and s[-1] != s[-2]:
    res = res1 * k + k -1
else:
    res = res1*k

print(res)

Submission status: Wrong Answer

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

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
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
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):
    if len(s) % 2 == 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:
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 <= 15:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

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 S2[j] == S2[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:
import sys
input = sys.stdin.buffer.readline
S = list(input())[: -1]
ls = S * 2
K = int(input())
if len(S) == 1:
  print(K // 2)
  exit(0)
left = 0
mid = 0
right = 0
last = 0
i = 0
while i < len(S):
  if ls[i] == ls[i + 1]:
    left += 1
    if i == len(S) - 1:
      last = 1
    i += 1
  i += 1
i = 0 + last
while i < len(S):
  if ls[i] == ls[i + 1]:
    mid += 1
    i += 1
  i += 1
i = 0 + last
while i < len(S) - 1:
  if ls[i] == ls[i + 1]:
    right += 1
    i += 1
  i += 1
if K == 1:
  i = 0
  res = 0
  while i < len(S) - 1:
    if ls[i] == ls[i + 1]:
      res += 1
      i += 1
    i += 1
  print(res)
else:
  print(left + mid * max(K - 2, 0) + right)

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:
  print(int(cnt)*k+1)
if t[0] != t[-1]:
  print(int(cnt)*k)

Submission status: Wrong Answer

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

inputlist = list(S)
c_list = [0]*len(S)
#print(inputlist)
#print(c_list)

j = 0
conti = 0
ans = 0
add = 0
add2 = 0

#連続する文字の数カウント
for i in range(len(S)):
#    print(inputlist[i-1])
    if i > 0:
        if inputlist[i] == inputlist[i-1]:
            c_list[j] += 1
        if inputlist[i] != inputlist[i-1]:
            j += 1

#print(c_list)

#文字列の先頭と末尾が同じ場合、いくつ連続になるかカウント
if inputlist[0] == inputlist[-1]:
    for k in range(len(S)):
        if k > 0:
            if inputlist[k] == inputlist[k-1]:
                conti += 1
            if inputlist[k] != inputlist[k-1]:
                break
    for m in range(len(S)):
        if m > 0:
            if inputlist[-m] == inputlist[-m-1]:
                conti += 1
            if inputlist[-m] != inputlist[-m-1]:
                break
#print(conti, "conti")


#連続する文字数から、1区切り内の入れ替え必要箇所カウント
for i in range(len(c_list)):
    if c_list[i] > 0 :
        ans += (c_list[i] // 2) + (c_list[i] % 2)

#文字列全部が同じ文字だった場合
if conti //2 +1  == len(S):
    #print("same")
    if len(S)%2 == 1:
        print("odd")
        add2 += 1
    else:
        conti = 0 #偶数ですべて同じ文字だったら、先頭と末尾のカウントを気にしない。

#文字列の文字が1種類でない場合で　連続数が偶数の場合
elif conti%2 == 0 and conti != 0 and conti >= 4:
    add += 1

#print(ans)
print(ans * K + add *(K-1) + K//2*add2)

Submission status: Wrong Answer

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

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

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



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

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

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


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

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])

if n == 1: 
    print(k // 2 + k % 2 )
    sys.exit(0)

flag=0
for i in range(1,n):
    #print("loop",i)
   if s[i] != s[i-1]:
       flag =1
       break

if flag == 0: 
    print((k*n-1) // 2 + (k*n-1) % 2 )
    sys.exit(0)



p=0
q=0
if s[0] == s[n-1]:
    l=1 
    # Start 
    for i in range(1,n):
        #print("loop",i)
        if s[i] == s[i-1]:
            p +=1
        else:
            break
    if l < n:
        for i in range(n-2,0,-1):
            if s[i] == s[i+1]:
                q +=1
            else:
                break
    l = 1 + p + q
    # print("Ring ", l, p,q)
    op = ((l // 2) + (l % 2)) * (k-1)
    op += ((p // 2) + (p % 2)) 
    op += ((q // 2) + (q % 2))
else: 
    op = 0     

# print("OP",op)

s2 = s[p+1:n-1-q] 
# print("s2",s2)
n=len(s2)
            

l = 0
for i in range(1,n):
    # print(s2[i],i,l,op)
    if s2[i-1] != s2[i]:
        op += (l // 2 + l % 2) * k
        l = 0 
    else:
        l += 1 
if l != 0:
    op += (l // 2 + l % 2) * k

        
print(op)

Submission status: Wrong Answer

Solution code:
S = input()
K = input()
# for i in range(int(K)):
#     T = T + S

list_str = list(S)

a = len(S)
a = a - 1
n = 0
p = 0
o = 0
for u in range(a):
  if list_str[n] == list_str[n+1]:
    p = p + 1
    list_str[n+1] = "1"
  if list_str[0] == list_str[a]:
    o = o + 1
  n = n + 1
p = p*int(K)
o = (o*int(K))
p = int(p) + int(float(o)) -1

print(p)

Submission status: Wrong Answer

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

res = 0
cn = 0
for i in range(1, len(S)):
    if S[i-1] == S[i] :
        res += 1          
        if i != len(S)-1 and S[i] == S[i+1]:
            cn += 1

cnt = cn // 2
if cn % 2 == 1:
    cnt += 1
n = res - cnt

#if len(S)%2 == 1 and S[0] == S[-1] and S[0] != S[1]  and S[-2] != S[-1]:
#   ans = n*K + K - 1
#else:
ans = n*K

print(ans)

Submission status: Wrong Answer

Solution code:
import string

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


ds = S + S + S[0]
ss = S
dlength = len(ds)
slength = len(ss)
chkd, chks = K // 2, K % 2

def chkpart(p):
    if p[0] == p[1] or p[1] == p[2]:
        return 1
    else:
        return 0

def chkerss(idx):
    i = 1
    while idx + i < slength and ss[idx] == ss[idx + i]:
        i += 1

    return i // 2, idx + i

def chkerds(idx):
    i = 1
    while idx + i < dlength and ds[idx] == ds[idx + i]:
        i += 1

    return i // 2, idx + i

c1, idx1 = 0, 0
while idx1 < dlength:
    a, idx1 = chkerds(idx1)
    c1 += a

c2, idx2 = 0, 0
if chks != 0:
    while idx2 < slength:
        a, idx2 = chkerss(idx2)
        c2 += a

# print(c1, c2, chkd, chks)
print(c1 * chkd + c2)