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

        print(cnt)

    
if __name__ == '__main__':
    main()

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

conti = False
for i in range(len(s)):
    if i == 0:
        if len(s) == 1:
            conti == True
        continue
    if s[i-1] != s[i]:
        conti = True

if len(s) % 2 == 0 and conti == False:
    print(cnt)
    exit()

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

if len(s) >=3:
    if len(s) % 2 == 1:
        cnt = cnt -((k + 1) // 2 - 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())
N = len(S)
M = [0 for i in range(N)]

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

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

count = 0
total = 0
c = 0
ans = 0

for i in range(N):
    if M[i] == 1:
        count+=1
    elif count==1:
        c=1
        total+=1
        count=0
    elif count!=0:
        if c==0 and count%2==0:
            c=count
        total+=int((1+count)/2)
        count=0
if count==1:
    total+=1
    print(total*K)
    exit()
elif count!=0:
    total+=int(count/2)
    if c!=0 and count%2==1:
        total += 1
        print(total*K-1)
        exit()

print(total*K)

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
 
s = input().rstrip()
k = int(input())

def check_duplicate(s):
    pre_s = None
    cnt_list = []
    cnt = 1
    for _s in s:
        if pre_s == _s:
            cnt += 1
        else:
            if cnt>1:
                cnt_list.append(cnt)
            cnt = 1
        # print(pre_s, _s, cnt, cnt_list)
        pre_s = _s
    if cnt>1:
        cnt_list.append(cnt)
    if len(cnt_list)==0:
        cnt_list.append(cnt)
    return cnt_list
 
def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c//2
    return(_ans * k)
 

if len(s)==1:
    ans = k//2
else:
    cnt_list = check_duplicate(s)
    if len(cnt_list)>1:
        ans = calc_ans(cnt_list, k)
    else:
        if cnt_list[0]%2==0:
            ans = cnt_list[0]//2 * k
            if s[0]==s[-1]:
                ans += (k - 1)
        elif cnt_list[0]%2==1 and cnt_list[0]>1 and cnt_list[0]==len(s):
            ans = cnt_list[0] * k //2
            if s[0]==s[-1]:
                ans += (k - 1)
        elif cnt_list[0]%2==1 and cnt_list[0]>1 and cnt_list[0]!=len(s):
            ans = cnt_list[0]//2 * k
        else:
            if s[0]==s[-1]:
                ans = k - 1
            else:
                ans = 0
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]
    if i == 0:
        prev_c = ''
    else:
        prev_c = t[i-1]
    if i == len(t)-1:
        if k == 1:
            next_c = ''
        else:
            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 > 2:
    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, prev_c, c, next_c, "->", another_c) 
    cnt = cnt + (cnt2 * (k-2))
    #print("".join(t))

if k > 1:
    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 = ''
        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
            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)
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)

elif K % 2 == 0:
    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:
    S_arranged = list(S*5)
    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)*4 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            elif i+1 > len(S)*2 -1 and i+1 <= len(S)*3 -1:

                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)*4 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1

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

Submission status: Wrong Answer

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

flg = 0
if len(S) % 2 != 0 and len(S) > 2:
    if S[0] == S[1] == S[-1]:
        flg = 1
    if len(S) > 4 and S[0] == S[1] == S[-1] == S[-2]:
        temp = S[0]
        for s in S:
            if temp != s:
                flg = 0
                break
            

for i in range(1, len(S)):
    if S[i] == S[i-1] and S[i] != None:
        ans += 1
        S[i] = None
ans *= K

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

if S[-1] == S[0] and S[1] != None:
    ans += (K-1)
elif flg:
    ans += K // 2


print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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


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

Submission status: Wrong Answer

Solution code:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-2):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        
        if a[0] == a[-1]:
            temp_3 = (temp + temp_2)//2
            cnt += temp_3 * (K-1)

        print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

memc = s[0]
resnum = 0
t = s + s[0]
a = []
for c in t:
    if c == memc:
        resnum +=1
    else:
        a += [int((resnum+1)/2)]
        memc = c
        resnum = 0
a += [int((resnum+1)/2)]
a[0] -=1
#print(a)
res = 0

# if len(s) == 1:
#     res = int(k/2)
# elif a[-1] == 1 and s[0] == s[-1]:
#     res = (sum(a[0:len(a)-1])) * k + (k-1)
# else:
#     res =  sum(a)*k
#print(res)

if len(a)>1 and a[-1]==1 and a[-1]!=a[-2]:
    res = (sum(a[0:len(a)-1])) * k + (k-1)
elif len(a)==1 and a[0]==0:
    res = int(k/2)
else:
    res = sum(a)*k
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

#### input
S_orig = list(input())
K = int(input())

if len(S_orig) == 1:
  if K == 1:
    print(0)
    exit()
  else:
    S_orig = S_orig * K
    K = 1

S = S_orig * 2

last_chk = 0
letters = string.ascii_lowercase
score = 0
for i in range(0, len(S) - 2):
  # print(S)
  if S[i] == S[i + 1]:
    S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
    score = score + 1

if S[-2] == S[-1]:
  S[-1] = letters.strip(S[-2]).strip(S[0])[0]
  score = score + 1
  # last_chk = 1
else:
  if S[-1] == S[0]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
    last_chk = 1

score_pre, score_fol = 0, 0
for i in range(0, len(S_orig)):
  if S_orig[i] != S[i]:
    score_pre = score_pre + 1

for i in range(0, len(S_orig)):
  if em:print(S_orig[i])
  if em:print(S[len(S_orig) + i - 1])
  if em:print("----")
  if S_orig[i] != S[len(S_orig) + i]:
    score_fol = score_fol + 1

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

if em:print(S)



# def calc(S, K):
#   if len(S) == 1:
#     if K == 1:
#       print(0)
#       exit()
#     else:
#       S = S * K
#       K = 1
#   # a, b = map(int, input().split())
#   # a = list(map(int, input().split()))
#   # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


#   # n = int(input())
#   # t = [int(input()) for i in range(n)]
#   if em: sys.stdin = open('/dev/tty')

#   #### function
#   # @lru_cache(maxsize=10000)

#   #### process
#   # if em: embed()
#   last_chk = 0
#   letters = string.ascii_lowercase
#   score = 0
#   for i in range(0, len(S) - 2):
#     # print(S)
#     if S[i] == S[i + 1]:
#       S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
#       score = score + 1

#   if S[-2] == S[-1]:
#     S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#     score = score + 1
#     # last_chk = 1
#   else:
#     if S[-1] == S[0]:
#       S[-1] = letters.strip(S[-2]).strip(S[0])[0]
#       score = score + 1
#       last_chk = 1
#   return score, last_chk, S


# if K % 2 == 0:
#   S = S * 2
#   K = int(K / 2)
#   score, last_chk, S2 = calc(S,K)
#   if last_chk == 1: score = score - 1
#   total = score * K
#   if em: print("B")
#   # print("----")
#   # print(total)
#   print(total)
# else:
#   if K == 1:
#     score, last_chk, S2 = calc(S,K)
#     if last_chk == 1: score = score - 1
#     total = score * K
#     if em: print("A")
#     # print("----")
#     # print(total)
#     print(total)
#   else:
#     if em: print("C")
#     S_orig = S
#     S = S * 2
#     K = int((K - 1) / 2)
#     score, last_chk, S2 = calc(S,K)
#     # if last_chk == 1: score = score - 1
#     total = score * K
#     # if em: embed()
#     S3 = [S2[-1]] + S_orig
#     score3, last_chk3, S4 = calc(S3,1)
#     if last_chk3 == 1: score3 = score3 - 1
#     print(total + score3)


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

# print(score*K)


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

Submission status: Wrong Answer

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

#print(link_S)
#S = S.append(S[len(S)-1])
i = 0
sum = 1
total_sum = 0
sum_ketu = 0
j = 0
add_sum = 1
if len(S) == 1:
    print( K -1)
    exit()
    
while(1):
    if i == len(S)-1:
        if S[i] == S[0]:
            while( j <= len(S)-2):
                if S[j] == S[j+1]:
                    add_sum += 1
                    j += 1
                else:
                    break
            if (sum % 2 == 1) and (add_sum % 2 == 1):
                sum += 1
                sum_ketu = -1
        break
    if S[i] == S[i+1]:
     #   print("i is", i)
     #   print("S[i] is", S[i], "S[i+1] is", S[i+1])
        sum += 1
     #   print(sum)
        i += 1
    else:
        total_sum += sum // 2
        sum = 1
        i += 1
        
total_sum += sum // 2
#print("test", 3 //2 )
#print(sum_ketu)
#print(total_sum)
print(total_sum * K + sum_ketu)

Submission status: Wrong Answer

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

T = S * 2
T2 = S
X = S

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

# 2bai
countB = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB += 1
        T[i + 1] = 'A'
if T[-1] == T2[0]:
    C = T2.copy()
    C.insert(0, T[-1])
    C.pop(-1)
    countC = 0
    for i in range(len(C) - 1):
        if C[i] == C[i + 1]:
            countC += 1
            C[i + 1] = 'A'

countX = 0
for i in range(len(X) - 2):
    if X[i] == X[i + 1]:
        countX += 1
        X[i + 1] = 'A'


if K > 1:
    if (T[-1] == T2[0]):
        print(countX+countC*(K-1))
    else:
        if K % 2 == 1:
            print(K//2*countB+countA)
        else:
            print(K//2*countB)
else:
    print(countA)

Submission status: Wrong Answer

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

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

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

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

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

t = k * su + f

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

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0
for i in range(l-2):
  if i==0 and d[0]==d[l-1] and d[i]==d[i+1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if l>=3:
  if d[l-1]!=d[l-2] and d[0]==d[l-1]:
    count*=n
    count+=n-1
  elif d[l-2]==d[l-1] and d[l-3]!=d[l-2]:
    count+=1
    count*=n
  else:
    count*=n
if l==2 and s[0]==s[1]:
  count=n
if l==1:
  if l%2:
    count+=n//2
  else:
    count+=(n/2)
print(count)

Submission status: Wrong Answer

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

s = ns()
k = ni()

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

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

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

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

Submission status: Wrong Answer

Solution code:
import copy

j = input()
k = int(input())



counter = 0
ans_L = []

s = [i for i in j]
r = copy.copy(s)
s.append("0")
renzoku = 1

for val in range(0,len(s)-1):
    tmp = s[val+1]
    if s[val] == tmp:
        renzoku += 1
    elif renzoku > 1 and s[val] != tmp:
        ans_L.append(renzoku//2)
        renzoku = 1
"""
r = list(reversed(r))
r.append("0")

rcount = 1
for val in range(0,len(r)-1):
    tmp = r[val+1]
    if s[val] == tmp:
        rcount += 1
    elif s[val] != tmp:
        break
"""
#print(rcount,"rcount")
# 繋がっている場合
# kbk



renzoku = 1
result = 0


if s[0] == s[-2]:
    for val in range(len(s)+1):
        if s[val+1] == s[0]:
            renzoku += 1
        else:
            break

    if renzoku ==0:
        result += 1
    elif renzoku == 2:
        result = 0
    elif renzoku >=3 and renzoku%2==1:
        result += 1


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


print(sum(ans_L)*k + result)

Submission status: Wrong Answer

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

memc = s[0]
resnum = 0
t = s + s[0]
a = []
for c in t:
    if c == memc:
        resnum +=1
    else:
        a += [int((resnum+1)/2)]
        memc = c
        resnum = 0
a += [int((resnum+1)/2)]
a[0] -=1
print(a)
res = 0

# if len(s) == 1:
#     res = int(k/2)
# elif a[-1] == 1 and s[0] == s[-1]:
#     res = (sum(a[0:len(a)-1])) * k + (k-1)
# else:
#     res =  sum(a)*k
#print(res)

if len(a)>1 and a[-1]==1 and a[-1]!=a[-2]:
    res = (sum(a[0:len(a)-1])) * k + (k-1)
elif len(a)==1 and a[0]==0:
    res = int(k/2)
else:
    res = sum(a)*k
print(res)

Submission status: Wrong Answer

Solution code:
import math

s = input()
k = int(input())
count = 0
i = 0
if k % 2 == 0:
    s *= 2
    k /= 2
else:
    for n in range(1,k+1):
        if k % n == 0:
            if n > 1:
                s *= n
                k /= n
                break

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

print(int(count * k))

Submission status: Wrong Answer

Solution code:
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)

elif K % 2 == 0:
    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:
    S_arranged = list(S*5)
    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)*4 -1:
                adder2 += 1
            elif i+1 > len(S) - 1 and i+1 <= len(S)*2 - 1:
                counts +=1
            elif i+1 > len(S)*2 and i+1 <= len(S)*3:

                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)*4 -1:
                adder2 += 1
            elif i > len(S)-1 and i+1 <= len(S)*2-1:
                counts +=1
            else:
                count2 +=1

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

Submission status: Wrong Answer

Solution code:
import string


def count_changes(s, k, pool, prev=None):
    if k == 0:
        return 0, None, None
    t = s * k
    count = 0
    i = 0
    for cur in t:
        if cur == prev:
            count += 1
            prev = pool[i]
            i = (i + 1) % length
        else:
            prev = cur
    return count, i, prev


if __name__ == '__main__':
    s = input()
    k = int(input())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    if k <= 100000:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, i, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
        	res += count_changes(s, rest, pool, prev)[0]
        elif s[0] == prev:
            res += (d - 1)
        print(res)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import math

def count(str):
  num = 0
  for k, g in groupby(str):
    num+=math.floor(len(list(g))/2)
    #print (math.floor(len(list(g))/2))
    h = (len(list(g))/2)%2
  return(num,h)
    
s=input()
k=int(input())

st=s*2
if st[0]==st[-1]:
  num = count(st)[0]+count(st)[1]*1
  print (num)
  if count(s)[0]==count(st)[0]/2:
    ans = (k//2)*num-count(st)[1]*1+(k%2)+(k%2)*count(s)[0]-1
  else:
    ans = (k//2)*num-count(st)[1]*1+(k%2)+(k%2)*count(s)[0]
else:
  ans = count(s)[0]*k
print (ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
flag = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    cnt += 1
    if i-1 >=0 and s[i-1] == s[i] and flag == 0:
      cnt -= 1
      flag = 1
    else:
      flag = 0
  else:
    flag = 0
tmp = 0
if len(s)%2 and s.count(s[0]) == len(s):
  tmp = k//2
print(cnt*k + tmp)

Submission status: Wrong Answer

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

def len_checker(S,r):
    count = 1
    if r == 0:
        icon = S[0]
        for j in range(1,len(S)):
            if S[j] == icon:
                count += 1
            else:
                break
    elif r == 1:
        icon = S[len(S)-1]
        for j in range(1,len(S)):
            jj = len(S) - j -1
            if S[jj] == icon:
                count += 1
            else:
                break
#     print(S,r,count)
    return(count)

def s_change(S):
    s_henkan = 0
    L = len(S) -1
#     print(S,S[0],S[L])
    if S[0] == S[L]:
        cf = len_checker(S,0)
        rf = len_checker(S,1)
#         print("cf:",cf,"rf:",rf)
        if cf % 2 == 1 and rf % 2 == 0:
            S[L] = "2"
            s_henkan += 1
#             pass
        else:
            S[0] = '2'
            s_henkan += 1
#     print("mit:",S)
    for i in range(1,len(S)):
#         print(S[i])
        if S[i] == S[i-1]:
#             print(S[i-1],S[i])
            S[i] = '1'
            s_henkan += 1
    return(S,s_henkan)

        
henkan = 0
    
if len(S) == 1:
    henkan = K // 2
elif len(S) == 2:
    if S[0] == S[1]:
        henkan = K
    else:
        henkan = 0
else:
    SSSS = s_change(S)
#     print("SSSS: ",SSSS)
    henS = SSSS[0]
    s_henkan = SSSS[1]
    
    if henS[0] == henS[len(henS)-1]:
        henkan = s_henkan * K + (K-1)
    else:
        henkan = s_henkan * K

print(henkan)