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: Runtime Error

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()
i = 0
count = 0
flag1  = False
flag2 = False

if s[0] == s[-1]:
    if s[0] == s[1]:
        flag1 = True
    if s[-2] == s[-1]:
        flag2 = True

if flag1 and not flag2:
    i = 1

while i < (len(s)-1):
    if s[i] == s[i+1]:
        count += 1
        i += 2
    else:
        i += 1
count *= k

if s[0] == s[-1]:
    if not flag1 and not flag2:
        count += (k-1)
    elif flag1 and not flag2:
        count += k



print(count)

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
count = 0
import copy
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    else (count + 2) % 5 == 0:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Submission status: Runtime Error

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

T = list(S * K)
T.append("0")
count = 0

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

print(count)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
cnt=0
leng=len(s)
next_eq=0
if leng==1:
  if k%2==0:
    cnt=k//2
    print(cnt)
  else:
    cnt=(k-1)//2
    print(cnt)
elif leng==2:
  if s[0]==s[1]:
    cnt=k
    print(cnt)
  else:
    print(cnt)
else:
  for i in range(k):
    if s[i]==s[i+1]:
        next_eq+=1
        cnt=cnt+next_eq%2
  if s[0]==s[-1]:
    if s[0]==s[1] or s[-1]==s[-2]:
        print(cnt)
    else:
        cnt+=k-1
        print(cnt)
  else:
    print(cnt)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
S_=S*K+"A"
cnt=0
import queue
q=queue.Queue()
for i in S_:
    if not q.empty():
        if not i == q.get():
            n=1
            while not q.empty():
                q.get()
                n+=1
            cnt+=n//2
            q.put(i)
        else:
            q.put(i)
            q.put(i)#出した分戻さないと           
    else:
        q.put(i)
        #最後、入れっぱなしで終わってしまっていた
print(cnt)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
t = s + s
ans = 0
i = 1
while True:
    c = 0
    while True:
        if i>=len(t):
            break
        elif t[i-1] == t[i]:
            i +=2
            c+= 1
        else:
            break
    ans += c
    if i>=len(t):
        break
    i+=1
if s[0]==s[-1]:
    c = 0
    while True:
        s0 = s[-1]
        print(s)
        if len(s)==1:
            break
        if s0==s[0]:
            c +=1
            s = s[:-1]
if c%2 ==0:
    print(((ans* k)+1)//2)
else:
    print(((ans* k))//2)

Submission status: Runtime Error

Solution code:
def zen(s):
    ans=0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            ans+=1
        if len(s)-1==ans:
            return True
        else:
            return False

s=input()
k=int(input())
a=s*2
cnt=[0]*len(s)
cnt2=[0]*len(a)
l=0
m=0
ans=0
j=True
if zen(s):
    print(len(s)*k//2)
else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            if i==0:
                j=False
            cnt[l]+=1
        elif cnt[l]!=0:
            l+=1
    for i in range(len(a)-1):
        if a[i]==a[i+1]:
            cnt2[m]+=1
        elif cnt2[m]!=0:
            m+=1
    ans=0
    p=1
    while cnt[p]!=0:
        ans+=(cnt2[p]+1)//2
        p+=1
    if j:
        ans+=(cnt[0]+1)//2
    ans*=k-1
    for i in range(p):
        ans+=(cnt[i]+1)//2
    print(ans)

Submission status: Runtime Error

Solution code:
#code
s=input()
k=int(input())
l=list()
curr=0
for i in range(1,len(s)):
    
    if(s[i]!=s[i-1]and curr!=0):
        l.append(curr+1)
        curr=0
    if(s[i]==s[i-1]):
        curr+=1
if(curr!=0):
    l.append(curr+1)
#for start
start=0
if(s[0]==s[len(s)-1]):
    for i in range(1,len(s)):
        if(s[i]==s[i-1]):
            start+=1
        else:
            break
    start+=l[len(l)-1]+1
ans=0
if(start==0):
    for i in range(len(l)):
        ans+=(l[i]//2)
    
    ans=ans*k    
else:
    if(len(l)==1):
        ans+=((l[0])//2)*k
    else:
        ans+=(l[0]+l[len(l)-1])//2
        ans=ans*(k-1)
        ans+=l[0]//2
        ans+=l[len(l)-1]//2
        if(len(l)>=3):
            for i in range(1,len(l)-1):
                ans+=((l[i]//2)*k)
print(int(ans))

Submission status: Runtime Error

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

cnt = 0
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        cnt = i + 1
        break
if cnt % 2 == 1:
    s = s + s[0]
    is_start = False
    idx_list = []
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            # print(i, i + 1)
            if not is_start:
                is_start = True
                s_idx = i
        else:
            if is_start:
                is_start = False
                e_idx = i
                idx_list.append((s_idx, e_idx))
    else:
        if is_start:
            idx_list.append((s_idx, len(s) - 1))
    # print(idx_list)
    ans = 0
    for s_idx, e_idx in idx_list:
        ans += (e_idx - s_idx + 1) // 2
    # print(ans)
    ans *= k
    if idx_list[-1][0] == len(s) - 2 and idx_list[-1][1] == len(s) - 1:
        ans -= 1
    print(ans)
else:
    is_start = False
    idx_list = []
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            # print(i, i + 1)
            if not is_start:
                is_start = True
                s_idx = i
        else:
            if is_start:
                is_start = False
                e_idx = i
                idx_list.append((s_idx, e_idx))
    else:
        if is_start:
            idx_list.append((s_idx, len(s) - 1))
    # print(idx_list)
    ans = 0
    for s_idx, e_idx in idx_list:
        ans += (e_idx - s_idx + 1) // 2
    # print(ans)
    ans *= k
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  cnt2 = 0
  p2 = 1
  s2 = s + s
  while p2 < len(s2):
    if s2[p2-1] == s2[p2]:
      cnt2 += 1
      p2 += 1
    p2 += 1

  if 2*cnt < cnt2:
    print(k*cnt + (k - 1))
  else:
    print(k*cnt)
    
s = s*k

Submission status: Runtime Error

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

T = S * 2
T2 = 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.insert(0, T[-1])
    countC = 0
    for i in range(len(C) - 1):
        if C[i] == C[i + 1]:
            countC += 1
            C[i + 1] = 'A'

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

Submission status: Runtime Error

Solution code:
import sys
import os

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

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:
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j
ans = K*ans
if S[0] == S[-1]:
    if S[0] != S[1] and S[-2] != S[-1]:
        ans += K-1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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
while(1):
    if i == len(S)-1:
        if S[i] == S[0] and S[0] != S[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(total_sum * K + sum_ketu)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


def LI(): return list(map(int, input().split()))


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()
    S=S.rstrip('\n')
    K = II()

    i = 0
    ans = 0
    A = 1
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            count += 1
            i += 1
        if(S[i] != S[i+1]):
            i += 1
        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
    if K >= 2 and S[0] == S[-1]:
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    

    return

# B


def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return

# C


def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return

# D


def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    # go = g
    # myset = set([1])
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            # myset.add(i)
        if g == 1:
            break
        if i == sq - 1:
            # myset.add(go)
            ans += 1

    print(ans)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    A()

Submission status: Runtime Error

Solution code:
s = list(map(str,input()))
k = int(input())
cnt =0
bool =True
x=-1
while bool:
    if s[x]==s[x+1]:
        #print('+B')
        cnt+=1
        x+=2
        if x==len(s)-1 or x==len(s)-2:
            #print('fiB')
            break
    else:
        x+=1
        if x==len(s)-1 or x==len(s)-2:
            #print('fiC')
            break
#print(cnt)
print(cnt*k)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
cnt=0
leng=len(s)
next_eq=0
if leng==1:
  if k%2==0:
    cnt=k//2
  else:
    cnt=(k-1)//2
elif leng==2:
  if s[0]==s[1]:
    cnt=k
  else:
    exit()
else:
  for i in range(k):
    if s[i]==s[i+1]:
        next_eq+=1
        cnt=cnt+next_eq%2
  if s[0]==s[-1]:
    if s[0]==s[1] or s[-1]==s[-2]:
        exit()
    else:
        cnt+=k-1
        exit()
  else:
    exit()
print(cnt)

Submission status: Runtime Error

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

def solve2(b,k):
    if len(b)*k<50:#0**5:
        return naive(b,k)
    
    rt=naive(b,1)*k
    if b[0]==b[-1] and (len(b)>=3 and (b[0]==[1] or b[-2]==[-1])):
        rt+=-1
    return rt
    
def solve(b,k):
    '''
    b=StoI()
    a=b+[b[0]]
    k=I()
    '''
    if len(b)*k<100:#0**5:
        return naive(b,k)
    else:
        naive(b,3)
        
    a=b+[b[0]]
    n=len(a)
    
    
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
    
    ans=t*k
    if n==2:
        ans=k//2
    if len(Counter(b))==1:
        ans=(k*len(b))//2
    if b[0]==b[-1]:
        ans=(naive(b,4))*(k//4)+(naive(b,k%4) if k%4!=0else 0)-1
    #print(ans)
    return ans

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


b=StoI()
k=I()
print(naive(b,k))

Submission status: Runtime Error

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

T = S * 2
T2 = 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]:
    countC = 0
    C = T[-1] + T2
    for i in range(len(C) - 1):
        if C[i] == C[i + 1]:
            countC += 1
            C[i + 1] = 'A'

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

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
def main():
    input = sys.stdin.readline
    S = input().strip()
    K = input()

    len_S = len(S)
    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 = np.zeros(1, dtype="int64")
    ans[0] = np.int64(cnts_repeat)
    ans[0] *= np.int64(K)
    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:
            ans[0] += np.int64(K) - np.int64(1)
        else:
            pass
    print(ans[0])
    #print(cnts_S)
    #print(char_S)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s=list(input())
k=int(input())
count=0
re=""
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        if i==len(s)-1:
            re=s[-1]
        s[i]="A"
        count +=1

if s[0]==s[-1]:
    count +=1
    re=s[-1]
    s[-1]="A"

if (s[-1]=="A") and (s[-2]!=re):
    print((count*k)-1)
    exit()
print(count*k)

Submission status: Runtime Error

Solution code:
S = str(input())
N = int(input())
S = S*N
S_list=list(S)
count = 0
for i in range(len(S_list)-2):
    if i < len(S_list)-1 and S_list[i] == S_list[i+1] and S_list[i+1] == S_list[i+2]:
        count += 1
        S_list[i+1] = "1"
    elif S_list[i] == S_list[i+1]:
        count += 1
    elif S_list[i+1] == S_list[i+2] and i == len(S_list)-3:
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
t=0
for i in range(1,len(s)):
	if i==1:
		sf=s[0]
		c=0
	if sf==s[i]:
		c+=1
	else:
		t+=(c+1)//2
		c=0
	sf=s[i]
t+=(c+1)//2
ans=t*k
if c%2==0 and sf==s[0]:
	ans+=k-1
print(ans)

Submission status: Runtime Error

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())
    s_set = set(s)
    if len(s) == len(s_set):
        return 0
    pool = list(set(string.ascii_lowercase) - s_set)
    length = len(pool)
    if k <= 2:
        print(count_changes(s, k, pool)[0])
    else:
        base_count, i, prev = count_changes(s, 3, pool)
        d, rest = divmod(k, 3)
        print(base_count * d + count_changes(s, rest, pool, prev)[0])

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
n = len(s)
ans = 0
i = 0
while i < n:
  if s[i] == s[i+1]:
    t = 0
    while s[i] == s[i+1]:
      t += 1
      i += 1
    ans += t//2
if k > 1:
  if s[0] ==s[n-1]:
    ans += 1
  ans = ans*k - 1
print(ans)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
count=0
if S[0] == S[-1]:
  count+= (K-1)  
if S[-1]==S[-2]:
  count+=1
A=[]
D=1
for i in range(len(S)-2):
  if S[i]==S[i+1]:
    D+=1
  else:
    A.append(D)
    D=1
A.append(D)
B=[i for i in A if not i==1]
C=[(i//2) for i in B]

print(sum(C)*K+count)

Submission status: Runtime Error

Solution code:
s = list(map(str,input()))
k = int(input())
cnt =0
bool =True
x=0
while bool:
    if s[-1]==s[0]:
        cnt+=1
        s.pop(-1)
        if x==len(s)-1 or x==len(s)-2:
            break
    if s[x]==s[x+1]:
        cnt+=1
        x+=2
        if x==len(s)-1 or x==len(s)-2:
            break
    else:
        x+=1
        if x==len(s)-1 or x==len(s)-2:
            break
#print(cnt)
print(cnt*k)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
def main():
    input = sys.stdin.readline
    S = input().strip()
    K = input()

    len_S = len(S)
    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 = np.zeros(1, dtype="int64")
    ans[0] = np.int64(cnts_repeat)
    ans[0] *= np.int64(K)
    if S[-1] == S[0]:
        if cnts_S[-1] == 0:
            if cnts_S[0] == 0:
                ans[0] += np.int64(K) - np.int64(1)
        #if cnts_S[0] + cnts_S[-1] == 0:
        #    if len(list(set(char_S))) != 1:
        #        ans[0] += np.int64(K) - np.int64(1)
    print(ans[0])
    #print(cnts_S)
    #print(char_S)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = input()
k = (n.find(" "))
a = n[0:k]
b = int(n[k+1:len(n)+1])
c = a * b
count = 0
flag = 0
c1 = "a"
for aa in range(len(c)):   
    if aa != 1:        
        if ((c1 == c[aa]) & (flag == 0)):
            count += 1
            flag = 1
        elif flag == 1:
            flag = 0
    c1 = c[aa]
print(count)

Submission status: Runtime Error

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

T = ""
for _ in range(K):
    T += S

for ti in range(1, len(T)):
    if T[i] == T[i - 1]:
        T = T[: i] + "?" + T[i + 1:]
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
s=input()
k=int(input())
a,b,i=0,0,0
if k<=10000:
    s=s*k
    while True:
        if s[i]==s[i+1]:
            a+=1
            i+=1
        i+=1
        if i>=len(s)-1:
            print(a)
            sys.exit()
if len(s)==1:
    print(k//2)
    sys.exit()
while True:
    if s[i]==s[i+1]:
        a+=1
        i+=1
    i+=1
    if i>=len(s)-1:
        break
print(a*k)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
S = str(input())
N = input()
S = S*N
S_list=list(S)
count = 0
for i in range(len(S_list)-2):
    if i < len(S_list)-1 and S_list[i] == S_list[i+1] and S_list[i+1] == S_list[i+2]:
        count += 1
        S_list[i+1] = "1"
    elif S_list[i] == S_list[i+1]:
        count += 1
    elif S_list[i+1] == S_list[i+2] and i == len(S_list)-3:
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
c = 1
i = 1
l = []
c2 = 0
while i < len(s):
    if i == len(s)-1:
        if s[i] == s[i-1]:
            c = c+1
            l.append(c)
            i = i+1
        else:
            l.append(c)
            l.append(1)
            i = i+1
    elif s[i] == s[i-1]:
        c = c + 1
        i = i + 1
    else:
        l.append(c)
        c = 1
        i = i+1
if len(l) == 1:
    print(len(s)*k//2)
elif s[0] == s[len(s)-1]:
    for i in range(1,len(l)-1):
        c2 = c2 + l[i]//2
    print(c2*k+((l[0]+l[len(l)-1])//2)*(k-1)+l[0]//2+l[len(l)-1]//2)
else:
    for i in range(0,len(l)):
        c2 = c2 + l[i]//2
    print(c2*k)

Submission status: Runtime Error

Solution code:
s=input()
n=int(input())
i = 0
replace = 0
str = s*n
while i < len(str):
    c = str[i]
    count=0
    for c in str[i+1:-1]:
        if str[i] != c:
            break
        count += 1
    if count > 0:
        if count % 2 == 1:
            replace += (count + 1) / 2
        else:
            replace += (count ) / 2
    i += count + 1
#if s[0] != s[-1]:
print(int(replace))
#else:
#    print(int(replace * n) + n)

Submission status: Runtime Error

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

itr = 0
cnt = 1
len_cnt = []
for x in S :
  if itr == 0 :
    pre = x
    itr += 1
    continue
  now = x
  if pre == now :
    cnt += 1
    itr += 1
    if itr == len(S) :
      len_cnt.append(cnt)
    continue
  if pre != now :
    len_cnt.append(cnt)
    itr+=1
    if itr == len(S) and cnt == 1 :
      len_cnt.append(cnt)
    cnt = 1
    pre = x
    continue

ans = 0
for x in len_cnt :
  if x == 1 :
    continue
  else :
    ans += int(x / 2)

if S[0] == S[-1] and len_cnt[-1] > 1 and len_cnt[-1] % 2 == 1 and K > 1:
  ans += 1

ANS = K * ans
if len_cnt[-1] % 2 == 1 and len_cnt[-1] != 1 :
  ANS -= 1
print(ANS)

Submission status: Runtime Error

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  s = s*k
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  print(cnt)

Submission status: Runtime Error

Solution code:
import copy
def main():
    S = input()
    K = int(input())
    a = list(S)
    b = copy.copy(a)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
    elif len(a) ==1:
        cnt += K//2
    else:
        cnt_2 = 0
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
        for j in range(len(b)-1):
            if b[j+1] == b[j]:
                b[j] = str(j)
                cnt_2 += 1
        cnt_2 *= K
        if b[0] == b[-1] and len(b) > 1:
            cnt_2 += K-1

    print(min(cnt,cnt_2))

    
if __name__ == '__main__':
    main()