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:
S = input()
K = int(input())

len_hist = []
moji_hist = []
p = []
pre_s =S[0]
p.append(pre_s)
for s in S[1:]:
  if s == pre_s:
    p.append(s)
  else:
    moji_hist.append(p[0])
    len_hist.append(len(p))
    p = [s]
  pre_s = s

moji_hist.append(s)
len_hist.append(len(p))
n_trans_base = sum([l//2 for l in len_hist]
if moji_hist[0] != moji_hist[-1]:
  print(n_trans_base * K)
else:
  join_hist = len_hist[1:-1]
  join_hist.append(len_hist[0] + len_hist[-1])
  n_trans_join = sum([l//2 for l in join_hist])
  print(n_trans_base + n_trans_join *(K-1))

Submission status: Runtime Error

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

T = s * k

ans = T[:1]
bef = T[:1]
cnt = 0
changestr = ''

for a in T[1:]:
	if a == bef:
		changestr = codecs.decode(a, 'rot13')
		ans = ans + changestr
		bef =  changestr
		cnt = cnt +1
	else:
		ans = ans + a
		bef = a

print(str(cnt))

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
count=0
SS=S*K
flag=True
print(SS)
for i in range(1, len(SS)):
  if not flag:
    flag=True
    continue

Submission status: Runtime Error

Solution code:
def a(s):
    count = 0
    for i in range(len(s) - 2):
        if s[i] == s[i + 1]:
            s = s[:i + 1] + 'A' + s[i + 2:]
            count += 1
    if s[-1] == s[-2]:
        count += 1
    return count


s = input()
k = int(input())
if s[0] != s[-1] or k == 1:
    print(a(s) * k)
elif k == 2:
    new_s = s * 2
    print(a(new_s))
else:
    new_s = s * 3
    count_3 = a(new_s)
    count_4 = a(new_s + s)
    diff = count_4 - count_3
    print(count_3 + (k-3) * diff)

Submission status: Runtime Error

Solution code:
import math

s = list(input())
k =int(input())
cnt1 = 0
cnt2 = 0
ans = 0
i = 0
j = len(s)-1
p = False
n = 0
while s[0] == s[n]:
  n += 1
  if n == len(s)-1:
    p = True
    break
if p == True:
  answer = math.floor(len(s)*k/2)
if p == False:
  t = False
  while s[0] == s[i]:
    i+= 1
  cnt1 = i 
  while s[-1] == s[j]:
    j = j-1
  cnt2 = len(s)-j-1
  if s[0] == s[-1]:
    t = True
  new_s = s[cnt1:len(s)-cnt2]
  for i in range(len(new_s)-1):
      if new_s[i] == new_s[i+1]:
        new_s[i+1] = "0"
        ans += 1
  if t == True:
    ans_2 = math.floor((cnt1+cnt2)/2)
    answer = ans*k + ans_2*(k-1) + math.floor(cnt1/2) + math.floor(cnt2/2)
  if t == False:
    answer = ans*k + math.floor(cnt1/2)*k + math.floor(cnt2/2)*k
print(answer)

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)-2):
                ans+=(l[i]//2)*k
print(int(ans))

Submission status: Runtime Error

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

end=0
if K!=1:
    S_old = S
    i=-1
    while S_old[0] == S_old[i]:
        S= S + S[0]
        i-=1
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break
    if renzoku >1:
        ans+=renzoku//2
    i+=1
if K== 1:
    pass
else:
    S=S_old
    ans = ans*(K-1)
    i=0
    while S[i] == S[0]:
        i+=1
    S=S[i:]
    i=0
    while i<len(S)-1:
        renzoku=1
        x=i
        while S[x] == S[i+1]:
            renzoku+=1
            if i+2 < len(S):
                i=i+1
            else:
                break
        if renzoku >1:
            ans+=renzoku//2
        i+=1
print(ans)

Submission status: Runtime Error

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

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

# sys.setrecursionlimit(10000)

#### input
S = list(input())
K = int(input())
# 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

total = score * K
if last_chk == 1: total = total - 1
print(total)


# 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: 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 :
  ans += 1

print(K * ans)

Submission status: Runtime Error

Solution code:
#import sys

#input = sys.stdin.readline

#import heapq
#import math
#import fractions

#def sosuhante(n):
#    for k in range(2, int(math.sqrt(n))+1):
#        if n% k ==0:
#            return False
#    return True

def main():
#    h,w,a,b = map(int, input().split())
    s = input()
    k= int(input())
    a= []
    b= []
    n=len(s)
    for i in range(n):
        a.append(s[i])
        b.append(s[i])

    a[0]="a"
    if n > 1:
        for i in range(1,n):
            if b[i]==b[i-1]:
                a[i]="a" if a[i-1]=="a" else "b"
            else:
                a[i]="b" if a[i-1]=="a" else "a"
#    print(a)
    temp=1
    henka=0
    if n > 1:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    henka+=temp//2
                    temp=1
            else:
                henka+=temp//2
                temp=1
    temp=1
    hasi=0
    mae=1
    usiro=1
    if k > 1 and n > 1 and b[0]==b[n-1]:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    mae=temp
            else:
                mae=temp
                break
        temp=1
        for i in range(n-2,0,-1):
            if a[i]==a[i+1]:
                temp+=1
                if i==0:
                    usiro=temp
            else:
                usiro=temp
                break
        if (mae+usiro)//2 > mae//2 + usiro//2:
            hasi=1
#    print(henka,"henka")
    count=0
    if k > 1 and n>1:
        count=henka*k + hasi*(k-1)
    if k == 1 and n>1:
        count=henka
    if n==1 and k==1:
        count=0
    if n==1 and K>1:
        count=k//2


    print(count)





if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
S = str(input())
K = int(input())
C = i = 0

while i + 1 < len(S):
    if S[i] == S[i + 1]:
        C += 1
        i += 1
    i += 1

if (S[0] == S[-1]) & (S[-1] != S[-2]):
    C += 1

print(C * K)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
cnt = 0
i = 0
c0 = 0
c1 = 0
key = 0
endkey = 0
while i < len(s) - 1:
    if s[i] == s[i + 1]:
        start = i
        end = i
        for j in range(i + 1, len(s)):
            end = j
            if s[j] != s[i]:
                break

        if end == len(s) - 1: #もし最後の一文字が連続に含まれるとき
            if (end - start) % 2 == 0 and s[-1] == s[0]: #最後の一文字が連続でかつ最初の文字と同じでかつ奇数の時
                end += 1
                endkey = 1
            end += 1
        cnt += (end - start) // 2 * k

        if start == 0 and end == len(s) and len(s) % 2 == 1:
            key = 1
            cnt += (end - start) * k // 2
            if k % 2 == 1:
                cnt += (end - start) // 2
        i = end - 1
        # print(start, end)
    i += 1

if len(s) == 1:
    cnt += k // 2
if endkey:
    cnt -= 1
print(cntissii
2)

Submission status: Runtime Error

Solution code:
def a(ll):
	cc = 0
	for xx in ll:
		if 0 < xx:
			cc += xx//2 + xx % 2
	return cc

s = input()
C = int(input())
s_ = ""
c = 0
l=[]
for x in s*C+"$":
	#print(s_,x)
	if s_ == x:
		c += 1
	else:
		l.append(c)
		c = 0
	s_ = x
#print(l)
print(a(l))

Submission status: Runtime Error

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

if s[0]==s[-1]:
  p+=1

l=[1]
for i in range(len(s)-1):
  if s[i]!=s[i+1]:
    l.append(1)
  else:
    l[-1]+=1
x=[i//2 for i in l]
y=sum(x)
if s[-1]==s[-2]:
  print((y-1)*k+p*k)
else:
  print(y*k+p*(k-1))

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
count_list = []
count = 0
for idx, s in enumerate(S):
    if idx == 0:
        continue
    else:
        if S[idx - 1] == s:
            count += 1
        else:
            if count > 0:
                count_list.append(count)
            count = 0
if count > 0:
    count_list.append(count)
if S[0] != S[1] and S[-1] != S[-2] and S[0] == S[-1]:
    count_list.append(1)
sum = 0
for count in count_list:
    sum += -(-count // 2)
print(sum * K)

Submission status: Runtime Error

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

ans = 0

s = s+s
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    s[i] = "0"
    if i>=len(s)//2:
      ans += k-1
    else:
      ans += 1
      
if len(set(list(t))) == 1 and len(t)%2 == 1:
  ans = len(t) * (k//2) + (k%2==1)*(k//2)
  ans = 1/0
print(ans)

Submission status: Runtime Error

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

count = 0

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

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

print(count)

Submission status: Runtime Error

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

T = S

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

count *= K

if T[0] == T[-1]:
    if T[0] != T[1]:
        count += (K-1)

print(count)

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
    prev = prev or t[0]
    for cur in t[1:]:
        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 <= 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, i, prev)[0])

Submission status: Runtime Error

Solution code:
import math

def main():
    s = input()
    k = int(input())
    cnt = 0
    if (len(s) == 1):
        cnt = math.floor(k/2)
    elif (len(s) == 2):
        if (s[0] == s[1]):
            cnt = k
        elif (s[0] != s[1]):
            cnt = 0
    else:
        l = list()
        prev = ''
        for c in s:
            if(c == prev):
                l[-1][1] += 1
            else:
                l.append([c, 1])
                prev = c
        for e in l:
            cnt += math.floor(e[1]/2)
        if (l[0][0] == l[-1][0]):
            p = l.pop()
            l[0][1] += p[1]
        cnt *= k
    print(cnt, end='')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

L = [i for i in S]


if len(S) == 1:
    if K == 1:
        print(0)
    else:
        print(S[2])
    sys.exit()

count = 0
tmp = S[0]
for i in range(1, len(S)):
    if S[i] == tmp:
        count += 1
        S[i] = "A"
        tmp = S[i]
    else:
        tmp = S[i]

count2 = 0
if S[0] == S[-1]:
    L[0] = "A"
    count2 += 1
    tmp = L[0]
    for i in range(1, len(L)):
        if L[i] == tmp:
            count2 += 1
            L[i] = "A"
            tmp = L[i]
        else:
            tmp = L[i]
    print((K//2)*count2 + (K//2+K%2)*count)
else:
    print(count * K)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
if (count+a[0])%2 == 0 and count!=0 and len(a)!=0 and S[0]==S[1] :
    ans+=K-1

print(ans)

Submission status: Runtime Error

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

count1 = 0
x = "A"
con = 1
#仮に1回でもダメなやつ
for i in range(N):
  if x == S[i]:
    con += 1 
  else:
    x = S[i]
    if con != 1:
      count1 += con //2
    con = 1
  #print(count1, i)
if con != 1:
  count1 += con //2
  
if con == N:
  print(N * K // 2)
  quit()
#print(count1)
count2 = 0
if S[0] == S[-1]:
  p = 0
  q = 0
  count0 = 1
  countf = 1
  i = 0
  j = -1
  while p == 0:
    if S[i] == S[i + 1]:
      count0 += 1
      i += 1
    else:
      p = 1
  while q == 0:
    if S[j] == S[j - 1]:
      countf += 1
      j -= 1
    else:
      q = 1

if (count0 % 2 == 1) and (countf % 2 == 1):
  count2 = 1
print(count1 * K + count2 * (K - 1))

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] and s[-1]!="A":
    count +=1
    re=s[-1]
    s[-1]="A"

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

Submission status: Runtime Error

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

L = len(S)
rtn = 0
las = False

if (L == 1):
    rtn = K // 2

else:
    ichi = 0
    mae = S[0]
    i = 1
    while(i < L):
        las = False
        if (S[i] == mae):
            ichi += 1
            i += 1
            las = True
        if (i < L):
            mae = S[i]
        i += 1

    if (las and (i == L+1)): #らすかえた
        rtn = ichi * K

    else: #らすかえてない
        ni = 0
        mae = S[L-1]
        i = 0
        while(i < L):
            las = False
            if (S[i] == mae):
                ni += 1
                i += 1
                las = True
            mae = S[i]
            i += 1

        if (las and (i == L+1)): #らすかえた
            T = K // 2
            rtn = ichi * (K - T) + ni * T
        else:
            rtn = ichi + ni * (K - 1)

print(rtn)

Submission status: Runtime Error

Solution code:
from sys import stdin


def change(S, K):
    c = 0 

    if len(S) == 1:
        return K/2

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

    if S[-1] == S[0] and S[-1] != S[-2]:                                                                                                                                                 
        c += 1 

    return K * c 


if __name__ == "__main__":
    S = stdin.readline().strip()
    K = int(stdin.readline())

    print(change(S, K))

Submission status: Runtime Error

Solution code:
gip

Submission status: Runtime Error

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

T = s * k

ans = T[:1]
bef = T[:1]
cnt = 0

for a in T[1:]:
	if a == bef:
		ans = ans + codecs.decode(a, 'rot13')
		cnt = cnt +1
	else:
		ans = ans + a
	bef = a
    
print(str(cnt))

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
li=[]
lili=[]
lilili=[]
count = 0
counts = 0
#li(i+1が同じだったところを記録)
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    li.append(i)

li.append(0)


#lili(各連続の長さをリストに入れていきたいその１)
for i in range(len(li)-1):
  if li[i]+1 == li[i+1]:
    lili.append(1)
  else:
    lili.append(0)

ones = 0
#lilili（その２）
for i in range(len(lili)-1):
  if lili[i] == lili[i+1] and lili[i] == 1:
    lilili[count] += 1
  else:
    count += 1

ones = lili.count(0)


for i in range(len(lilili)):
  if (lilili[i]+2) %2 == 0:
    counts += (lilili[i]+2)/2
  else:
    counts += (lilili[i]+1)/2




counts += ones

counts *= k

if s[0]==s[-1]:
  counts += (k-1)
print(counts)

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
c1 = "a"
for aa in range(len(c)):    
    if aa != 1:
        if c1 == c[aa]:
            count += 1            
    c1 = c[aa]
print(count)

Submission status: Runtime Error

Solution code:
import string


def count_changes(s, k, pool, i=0, prev=None):
    if k == 0:
        return 0
    t = s * k
    count = 0
    prev = prev or t[0]
    for cur in t[1:]:
        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 <= 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, i, prev)[0])

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
a = [0 for i in range(len(a))]
cnt = 0
i = 0
while i < len(s)-1:
  if s[i] == s[i+1]:
    a[i] = 1
    a[i+1] = 1
    i += 2
    cnt += 1
  elif s[i] != s[i+1]:
    i += 1

if a[0] == 0 and a[-1] == 0 and s[0] == s[-1]:
  print(cnt*k + k-1)
else:
  print(cnt*k)

Submission status: Runtime Error

Solution code:
import string


def count_changes(s, k, pool, i=0, prev=None):
    if k == 0:
        return 0
    t = s * k
    count = 0
    prev = prev or t[0]
    for cur in t[1:]:
        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 <= 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, i, prev))

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
a = [0]
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
if (count+a[1])%2 == 0 and count!=0:
    ans+=K-1

print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
a = [0 for i in range(len(a))]
cnt = 0
i = 0
while i < len(s)-1:
  if s[i] == s[i+1]:
    i += 2
    a[i] = 1
    a[i+1] = 1
    cnt += 1
  elif s[i] != s[i+1]:
    i += 1

if a[0] == 0 and a[-1] == 0 and s[0] == s[-1]:
  print(cnt*k + k-1)
else:
  print(cnt*k)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

l = len(S)
pair = []
current = S[0]
seq = 1
for i in range(1, l):
    if(i == l-1):
        if(S[i]==current):
            seq += 1
        if(seq != 1):
            pair.append(seq)
    elif(S[i] == current):
        seq += 1
    else:
        if(seq != 1):
            pair.append(seq)
        seq = 1
        current = S[i]

ans = 0
#print(pair)
if(len==1):
    ans = K-1
elif(S[0] != S[l-1] or len == 2):
    for i in range(len(pair)):
        ans += K * (pair[i]//2)
else:
    if(S[l-2] != S[l-1]):
        pair.append(1)
    if(len(pair) == 1):
        ans += ((pair[0]+1)//2)*(K-1) + (pair[0]//2)
    else:
        for i in range(len(pair)-1):
            ans += K * i
        ans += ((pair[i+1]+1)//2)*(K-1) + (pair[i+1]//2)

print(ans)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
if (count+a[0])%2 == 0 and count!=0:
    ans+=K-1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import collections
import copy
import math
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
S= input()
K =int(input())
count = 1
ans_list = []
ans = 0
if S [0] != S[-1]:
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            if count ==1:
                continue
            else:
                ans_list.append(count)
                count = 1
    if count !=1:
        ans_list.append(count)
    for j in range(len(ans_list)):
        if ans_list[j]%2 == 1:
            ans += (ans_list[j]-1)//2
        else:
            ans += (ans_list[j])//2
    print(ans*K)
else:
    a = 0
    while S[a] == S[a+1] :
        a +=1
        if a == len(S)-1:
            break
    a += 1
    b = 0
    while S[-1-b] == S[-2-b]:
        b += 1
        if b == len(S)-1:
            break
    b += 1
    if a != len(S):
        S_alt = S[a:len(S)-b]
        for i in range(len(S_alt)-1):
            if S_alt[i] == S_alt[i+1]:
                count += 1
            else:
                if count ==1:
                    continue
                else:
                    ans_list.append(count)
                    count = 1
        if count !=1:
            ans_list.append(count)
        for j in range(len(ans_list)):
            if ans_list[j]%2 == 1:
                ans += (ans_list[j]-1)//2
            else:
                ans += (ans_list[j])//2
        ans = (ans * K)
        ans += a//2 + b//2
        ans += (a+b)//2*(K-1)
        print(ans)
    else:
        ans += len(S)*K//2
        print(ans)

Submission status: Runtime Error

Solution code:
s=list(input())
k=int(input())
l=len(s)
s1=0
s2=0
if s[-1]==s[0]:
    if s[0]==s[1]:
        s[0]='.'
if l%2 and len(set(s))==1:
    print(l*k//2)
    exit()
for i in range(1,l):
    if s[i]==s[i-1]: s[i]='.'
ans=s.count(('.'))*k
if s[0]==s[-1]: ans+=k-1 
print(ans)

Submission status: Runtime Error

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

ss = ""
for _ in range(k):
    ss += s

ss_list = list(ss)
count = 0
answer = 0
prev = ""
for sss in ss_list:
    if prev == "" or sss == prev:
        count+=1
        if count == 2: 
            answer+=1
            count=0

    else:
        count=1
    prev = sss

print(answer)

Submission status: Runtime Error

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

pre = S[0]
flg = 0
cnt = 0
cnt_list =[]

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

if S[0] == S[-1] and (S[-1] == S[-2] or S[0] == S[1]):
    print(len(cnt_list)*K)
elif S[0] == S[-1]:
    print(len(cnt_list)*K+K)
else :
    print(len(cnt_list)*K)

Submission status: Runtime Error

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

count=1
L=[]
for i in range(len(S)):
    try:
        if S[i] == S[i+1]:
           count = count + 1
        else:
            L.append(count)
            count = 1
    except:
        # 最後の文字セットがOutOfRangeになるので結果に出力
        L.append(count)

if S[-1]==S[1]:
  flag=0
  if len(L)==1:
    print(len(S)*K//2)
  else:
    if L[-1]%2==1 and L[0]%2==1:
      flag=1
    L[0]=(L[0]+L[-1])//2*2
    L[-1]=0
    print(sum([x//2 for x in L])*K-flag)
else:
  print(sum([x//2 for x in L])*K)

Submission status: Runtime Error

Solution code:
SR=S+[S[0]]
cnt=0
for i in range(len(S)-1):
  if SR[i]==SR[i+1]==SR[i+2]:
    SR[i+1]=0
    cnt+=1

for i in range(len(S)):
  if SR[i]==SR[i+1]:
    SR[i]=0
    cnt+=1
    
if SR[-2]==0 and S[-1]!=S[-2]:
  ans = cnt*K-1
else:
  ans = cnt*K
print(ans)

Submission status: Runtime Error

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

count = 0
change = False

for i in range(1, len(s)):
    if s[i-1] == s[i] and not change:
        count += 1
        change = True
    else:
        change = False
if s[0] == s[-1] and s[-2] != s[-1]:
    count += 1
    print(count*k-1)
else:
    print(count*k)

Submission status: Runtime Error

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

l = len(S)
pair = []
current = S[0]
seq = 1
for i in range(1, l):
    if(i == l-1):
        if(S[i]==current):
            seq += 1
        if(seq != 1):
            pair.append(seq)
    elif(S[i] == current):
        seq += 1
    else:
        if(seq != 1):
            pair.append(seq)
        seq = 1
        current = S[i]

ans = 0
#print(pair)
if(S[0] != S[l-1]):
    for i in range(len(pair)):
        ans += K * (pair[i]//2)
else:
    if(S[l-2] != S[l-1]):
        pair.append(1)
    if(len(pair) == 1):
        ans += ((pair[0]+1)//2)*(K-1) + (pair[0]//2)
    else:
        for i in range(len(pair)-1):
            ans += K * i
        ans += ((pair[i+1]+1)//2)*(K-1) + (pair[i+1]//2)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
# 文字列Tを作る
s = input()
k = int(input())

for i in range(k-1):
    T = s*k

# 文字列Tのリスト化
t = list(T)

# t内にて，もしj番目とj+1番目の文字が同じならj+1番目の文字を書き換える
count = 0
for j in range( len(t) - 1 ):
    if t[j] == t[j+1]:
        t[j+1] = "TT"     # 絶対に他と被らない文字にする
        count += 1       # 書き換えた際には，その回数を数えておく

# 出力
print(count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

L = [i for i in S]


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

count = 0
tmp = S[0]
for i in range(1, len(S)):
    if S[i] == tmp:
        count += 1
        S[i] = "A"
        tmp = S[i]
    else:
        tmp = S[i]

count2 = 0
if S[0] == S[-1]:
    L[0] = "A"
    count2 += 1
    tmp = L[0]
    for i in range(1, len(L)):
        if L[i] == tmp:
            count2 += 1
            L[i] = "A"
            tmp = L[i]
        else:
            tmp = L[i]
    print((K//2)*count2 + (K//2+K%2)*count)
else:
    print(count * K)

Submission status: Runtime Error

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

T = s * k

ans = ''
bef = s[:1]
cnt = 0

for a in s[1:]:
	if a == bef:
		ans = ans + codecs.decode(a, 'rot13')
		cnt = cnt +1
	bef = a

all = ans * k

ans = ''
bef = all[:1]
for a in all[1:]:
	if a == bef:
		ans = ans + codecs.decode(a, 'rot13')
		cnt = cnt +1
	bef = a  
    
print(str(cnt))