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 math
import copy

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

cnt = cnt * k
if len(s[-1]) == 1:
  if len(s) == 1:
    cnt += math.floor(k / 2)
  elif s[0] == s[-1]:
    cont_count = 0
    for j in range(len(orig_s)):
      if orig_s[j] == orig_s[j + 1]:
        cont_count += 1
      else:
        break
    # 先頭2n文字続きで置換されていたらこれ要らないよね
    if cont_count % 2 == 0:
      cnt += k - 1
    
print(cnt)

Submission status: Runtime Error

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

s_list = []
times = 0
flow = 0



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


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

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

if s.count(s[len(s)-1]) % 2 == 1:
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)

Submission status: Runtime Error

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

s_list = []
times = 0
flow = 0



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


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

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

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)

Submission status: Runtime Error

Solution code:
n = int(input())
s = []
for _ in range(n):
    s.append(input())
check = []
testc = 1000
for i in range(n):
    test = 0
    for c in s[i]:
        test += int(c)
    if test == testc:
        check.append(i)
    elif test < testc:
        check.clear()
        testc = test
        check.append(i)

res = -1
for i in check:
    tres = 1
    flag = [-2 for _ in range(n)]

    flag[i] = 1
    que = [i]
    while que:
        target = que.pop()
        for j in range(n):
            if s[target][j] == "1":
                if flag[j] == -2:
                    flag[j] = flag[target] + 1
                    que.append(j)
                elif flag[j] != flag[target] + 1 and flag[j] != flag[target] - 1:
                    tres = -1
                    break
        if tres == -1:
            break
    if tres == -1:
        continue
    else:
        res = max(res, max(flag))

print(res)

Submission status: Runtime Error

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

s_list = []
times = 0
flow = 0


print(s.count(s[len(s)-1]))

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


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

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

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)

Submission status: Runtime Error

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

s_list = []
times = 0
flow = 0

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

print(s.count(s[len(s)-1]))

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


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

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

if s.count(s[len(s)-1]) % 2 == 1 :
    if s[0] == s[len(s)-1]:
        times += 1



print(times*k)

Submission status: Runtime Error

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

s=input()
k=int(input())
cnt=[]
ans=0
c=1
if zen(s):
    print(len(s)*k//2)
else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            c+=1
        else:
            cnt.append(c)
            c=1
        if i==len(s)-2:
            cnt.append(c)
    ans=0
    if s[0]==s[-1] and cnt[-1]%2==1 and cnt[0]%2==1:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k+(k-1))
    else:
        for i in range(len(cnt)):
            ans+=cnt[i]//2
        print(ans*k)

Submission status: Runtime Error

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

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

print(cnt*k+a)

Submission status: Runtime Error

Solution code:
#文字列wordの文字の種類の構成の表現のためのリストを返す関数
def connection_count(word)->list:
    i=0
    a=[]
    count=1
    while i<(len(word)-1):
        while word[i]==word[i+1]:
            count+=1
            i+=1
            if i>=(len(word)-1):
                break
        a.append(count)
        count=1
        i+=1
    if a[-1]==1:
        a.append(count)
    return a

#S:文字列、K：繰り返す回数
S=input()
K=int(input())

#Sの文字の種類の構成を表現するためのリスト
connection=connection_count(S)

#結果の表示

#すべての文字が同じ場合
if len(connection)==1:
    print(((connection[0]*K)//2))
#最初と最後の文字が異なる場合
elif S[0]!=S[-1]:
    connection2=map(lambda x:x//2,connection)
    print((sum(connection2)*K))
#最初と最後の文字が同じ場合（すべての文字が同じ場合を除く）
else:
    t=S*K
    connection3=connection_count(t)
    connection4=map(lambda x:x//2,connection3)
    print(sum(connection4))

Submission status: Runtime Error

Solution code:
s, k = [input() for i in range(2)] 
S=str(s)
K=int(k)
print(S)
print(K)
a=0
c=0
for a in range(len(S)):
    if S[a] == S[a+1]:
        c = c+1
        a = a+1
    else:
        a = a+1

print(c*K)

Submission status: Runtime Error

Solution code:
s = input()
s2 = s + '0'
l = int(input())
i = 0
c = 0
c2 = 0
c3 = 0
start = 0
end = 0
k = 1
while i < (len(s)):
	if s2[i] == s2[i+1]:
		c += 1
		i += 1
	else:
		c2 += -(-c//2)
		c = 0
		i += 1
if s[0] == s[-1]:
	while s[-k] == s[-k-1]:
		end += 1
		k += 1
		if k == len(s):
			break
	k = 0
	while s[k] == s[k+1]:
		start += 1
		k += 1
		if k == len(s)-1:
			break
	c3 -= -(-start//2)
	c3 -= -(-end//2)
	c3 += -((-(start + end+1))//2)
	c2 += c3		
		
print(c2*l +(-(start + end)//2) - (-end//2))
#c2wordhenkou

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import re

sentence = input()
sentence2 = sentence * int(input())

def char_n_gram(sentence2, N):
    result = []
    for it in range(len(sentence2)):
        if it + N > len(sentence2):
            return result
        result.append(sentence2[it: it+N])

sk = char_n_gram(sentence2, N=2)

count = 0
for i in  sk:
  if i[0:1] == i[1:2]:
    count += 1

print(count)

Submission status: Runtime Error

Solution code:

S = input()
K = int(input())
 
buf=''
cnt = 1
total = 0
cnts = []
 
for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)
 
if len(S)==1:
    print(int(K/2))
    
elif len(set(list(S)))==1:
    print(int(len(S*K)/2))
elif S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
    else:
        print(total*K)
else:
    print(total*K)

Submission status: Runtime Error

Solution code:
import re

sentence = input()
sentence2 = sentence * int(input())

def char_n_gram(sentence2, N):
    result = []
    for it in range(len(sentence2)):
        if it + N > len(sentence2):
            return result
        result.append(sentence2[it: it+N])

sk = char_n_gram(sentence2, N=2)
print(sk)

count = 0
for i in  sk:
  print(i)
  print(i[0:1])
  if i[0:1] == i[1:2]:
    count += 1

print(count)

Submission status: Runtime Error

Solution code:

S = input()
K = int(input())
 
 
buf=''
cnt = 1
total = 0
cnts = []
 
 
for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)
 
if len(S)==1:
    print(int(K/2))
    
elif len(set(list(S)))==1:
    print(int(len(S*K)/2))
 
elif S[0]==S[-1] and cnts[1] % 2 != 0:
    if (cnts[1]+cnts[-1]) % 2 == 0:
        print(total*K+K-1)
    else:
        print(total*K)
else:
    print(total*K)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]

    s1, s2, s3 = '-', S[0], S[1]
    for i,s in enumerate(SK[2:]+'--', start=2):
        s1, s2, s3 = s2, s3, s
        if s3 and s1==s2:
            s2 = '='
            swap_count[i//l] += 1

    res = swap_count[0] + max(0, K-1)*swap_count[1]
    return str(res)
    

if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
#文字列wordの文字の種類の構成のリストを返す関数
def connection_count(word)->list:
    i=0
    a=[]
    count=1
    while i<(len(word)-1):
        while word[i]==word[i+1]:
            count+=1
            i+=1
            if i>=(len(word)-1):
                break
        a.append(count)
        count=1
        i+=1
    if a[-1]==1:
        a.append(count)
    return a

#S:文字列、K：繰り返す回数
S=input()
K=int(input())

connection=[]

connection=connection_count(S)
print(connection)
if len(connection)==1:
    print(((connection[0]*K)//2))
elif S[0]!=S[-1]:
    connection2=map(lambda x:x//2,connection)
    print((sum(connection2)*K))
else:
    t=S*K
    connection3=connection_count(t)
    print(connection3)#
    connection4=map(lambda x:x//2,connection3)
    print(connection4)
    print(sum(connection4))

Submission status: Runtime Error

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
if s[0] == s[-1] and s[0] != s[1] and s[0] != s[-2]:
  tmp += k-1
print(cnt*k + tmp)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def solve(debug=0):
    S = input()a# -*- coding: utf-8 -*-
def solve():
    S = input()
    l = len(S)
    K = int(input())
    SK = S*min(2,K)
    swap_count = [0,0]

    s1, s2, s3 = '-', S[0], S[1]
    for i,s in enumerate(SK[2:]+'--', start=2):
        s1, s2, s3 = s2, s3, s
        if s3 and s1==s2:
            s2 = '='
            swap_count[i//l] += 1

    res = swap_count[0] + max(0, K-1)*swap_count[1]
    return str(res)
    

if __name__ == '__main__':
    print(solve())

    l = len(S)
    K = int(input())
    #SK = S*min(2,K)
    SK =  S*K
    #swap_count = [0,0]
    swap_count = 0

    #T = ''
    s1, s2, s3 = '-', S[0], S[1]
    for i,s in enumerate(SK[2:]+'--', start=1):
        s1, s2, s3 = s2, s3, s
        if s3 and s1==s2:
            s2 = '='
            #swap_count[i//l] += 1
            swap_count += 1

        #if i >= 1:
        #    T += s1

    #debug and print(SK)
    #debug and print(T)
    #debug and print(swap_count)
    #res = swap_count[0] + max(0, K-1)*swap_count[1]
    res = swap_count
    return str(res)
    

if __name__ == '__main__':
    print(solve())

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

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

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


buf=''
cnt = 1
total = 0
cnts = []


for s in S:
    #print(buf,s,cnt)
    if s == buf:
        cnt += 1       
    else:
        cnts.append(cnt)
        total += int(cnt/2)
        cnt = 1        
    buf = s
cnts.append(cnt)    
total += int(cnt/2)

if len(S)==1:
    print(int(K/2))
    
elif len(set(list(S)))==1:
    print(int(len(S*K)/2))

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

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0]+"@"+s[2:]
    s=s[0:len(s)-1]+"@"
    c+=2
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    c+=1
elif s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s="@"+s[1:]
    c+=1
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
for j in range(len(s)-2):
  if s[j]==s[j+1] and s[j+1]==s[j+2]:
    s=s[0:j+1]+"@"+s[j+2:]
    c+=1
    j+=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
if a==False:
    print(str(c*k))
else:
    print(str(c*k-1))

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
T = list(S * K)
count = 0

for c in range(len(T)):
    if c == 0:
        pass
    else:
        s1 = T[c - 1]
        s2 = T[c]
        if s1 == s2:
            T[c] = "*"
            count += 1
print(count)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
s = list(s)
ind = 0
anss = [[] for i in range(100)]
for i in range(len(s)-1):
  anss[ind].append(s[i])
  if s[i] != s[i+1] and i != len(s)-2:
    ind += 1
  elif i == len(s)-2:
    ind += 1
#print(ind)
#print(anss)
for i in range(100-ind):
  anss.remove([])
#print(anss)
f = 0
pf = 0
if len(anss) == 0:
  anss.append([])
if s[0] == s[len(s)-1] and s[len(s)-2] == s[len(s)-1] and len(anss)>1 and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0] += anss[len(anss)-1]
  anss.remove(anss[len(anss)-1])
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    f = 1
elif s[0] == s[len(s)-1] and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    #print(len(anss[0]),pf)
    f = 1
elif s[len(s)-2] == s[len(s)-1] and k > 1:
  anss[len(anss)-1].append(s[len(s)-1])
else:
  anss.append([s[len(s)-1]])
pans = 0
#print(anss)
for i in anss:
  pans += len(i) // 2
#print(f)
if len(anss) == 1 and len(anss[0]) % 2 == 1:
  print(len(s*k)//2)
elif f == 1:
  print(pans*k-1)
else:
  print(pans*k)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def solve(debug=0):
    S = input()
    l = len(S)
    K = int(input())
    #SK = S*min(2,K)
    SK =  S*K
    #swap_count = [0,0]
    swap_count = 0

    #T = ''
    s1, s2, s3 = '-', S[0], S[1]
    for i,s in enumerate(SK[2:]+'--', start=1):
        s1, s2, s3 = s2, s3, s
        if s3 and s1==s2:
            s2 = '='
            #swap_count[i//l] += 1
            swap_count += 1

        #if i >= 1:
        #    T += s1

    #debug and print(SK)
    #debug and print(T)
    #debug and print(swap_count)
    #res = swap_count[0] + max(0, K-1)*swap_count[1]
    res = swap_count
    return str(res)
    

if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
import re

sentence = input()
sentence2 = sentence * int(input())

def char_n_gram(sentence2, N):
    result = []
    for it in range(len(sentence2)):
        if it + N > len(sentence2):
            return result
        result.append(sentence2[it: it+N])

sk = char_n_gram(sentence2, N=2)

for i in  sk:
  count = 0
  if i[0:0] == i[1:1]:
    count += 1

print(count)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0]+"@"+s[2:]
    s=s[0:len(s)-1]+"@"
    c+=2
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    c+=1
elif s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s="@"+s[1:]
    c+=1
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
for j in range(len(s)-2):
  if s[j]==s[j+1] and s[j+1]==s[j+2]:
    s=s[0:j+1]+"@"+s[j+2:]
    c+=1
    j+=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
if a==False:
    c=c*k
else:
    c=c*k-1
print(c)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0]+"@"+s[2:]
    s=s[0:len(s)-1]+"@"
    c+=2
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    c+=1
elif s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s="@"+s[1:]
    c+=1
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
for j in range(len(s)-2):
  if s[j]==s[j+1] and s[j+1]==s[j+2]:
    s=s[0:j+1]+"@"+s[j+2:]
    c+=1
    j+=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
if a=False:
    c=c*k
else:
    c=c*k-1
print(c)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
t = s
cnt = 0
loop = 0
t = [i for i in t]
if t[::-1][0] == t[0] and t[::-1][1] != t[0] and t[::-1][0] != t[1]:
    cnt = 1
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
aa = cnt*(k-1)+t.count("?")*k
print(aa)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0]+"@"+s[2:]
    s=s[0:len(s)-1]+"@"
    c+=2
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
elif s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s="@"+s[1:]
    a=True
    c+=1
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
for j in range(len(s)-2):
  if s[j]==s[j+1] and s[j+1]==s[j+2]:
    s=s[0:j+1]+"@"+s[j+2:]
    c+=1
    j+=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
c=c*k
print(c)

Submission status: Runtime Error

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


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

print(count)

Submission status: Runtime Error

Solution code:
from itertools import groupby
import sys
sys.setrecursionlimit(1000000000)
S,k= [input() for i in range(2)]
K=int(k)
c=0
strs=S*K
strs_1 = [[k, len(list(g))] for k, g in groupby(strs)]
strs_2 = [x[1] for x in strs_1]
strs_2
for k in strs_2:
    if k >=2:
        a = k//2
        c +=a
print(c)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
if s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0]+"@"+s[2:]
    s=s[0:len(s)-1]+"@"
    c+=2
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]==s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
elif s[0]==s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s="@"+s[1:]
    a=True
    c+=1
elif s[0]!=s[1] and s[0]==s[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
    s=s[0:len(s)-1]+"@"
    a=True
    c+=1
for j in range(len(s)-2):
  if s[j]==s[j+1] and s[j+1]==s[j+2]:
    s=s[0:j+1]+"@"+s[j+2:]
    c+=1
    j+=1
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
c=c*k
print(c)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
t = s
print(s*k)
cnt = 0
loop = 0
t = [i for i in t]
if t[::-1][0] == t[0] and t[::-1][1] != t[0] and t[::-1][0] != t[1] :
    cnt = 1
b = 0
while b < len(t) - 1:
    if len(list(set(t[b:]))) == len(t[b:]):
        break
    if t[b] == t[b + 1]:
        t[b + 1] = '?'
    b += 1
aa = cnt*(k-1)+t.count("?")*k
print(aa)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
d=[[0 for _ in range(100)] for _ in range(4)]
ans = [0,0,0,0]
l = len(S)
odd_diff = 0
even_diff = 0
if l == 1:
    ans = K // 2
    print(ans)
if l > 1:
    for i in range(4):
        s = S * (i + 1)
        l = len(s)
        for j in range(l-1):
            
            if j == 0 and s[j] == s[j+1]:
                ans[i] += 1
                d[i][0] = 1
            if j > 0 and s[j] == s[j+1] and d[i][j-1] == 0:
                ans[i] += 1
                d[i][j] = 1
    if K <= 4:
        print(ans[K-1])
    elif K > 4 and K % 2 == 0:
        even_diff = ans[3] - ans[1]
        print(int(ans[1]+(K/2)*even_diff))
    elif K > 4 and K % 2 == 1:
        odd_diff = ans[2] - ans[0]
        print(int(ans[0]+(K//2)*odd_diff))

Submission status: Runtime Error

Solution code:

s = input()
k = int(input())
s = list(s)
ind = 0
anss = [[] for i in range(100)]
for i in range(len(s)-1):
  anss[ind].append(s[i])
  if s[i] != s[i+1] and i != len(s)-2:
    ind += 1
  elif i == len(s)-2:
    ind += 1
#print(ind)
#print(anss)
for i in range(100-ind):
  anss.remove([])
#print(anss)
f = 0
pf = 0
if len(anss) == 0:
  anss.append([])
if s[0] == s[len(s)-1] and s[len(s)-2] == s[len(s)-1] and len(anss)>1 and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0] += anss[len(anss)-1]
  anss.remove(anss[len(anss)-1])
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    f = 1
elif s[0] == s[len(s)-1] and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    #print(len(anss[0]),pf)
    f = 1
elif s[len(s)-2] == s[len(s)-1] and k > 1:
  anss[len(anss)-1].append(s[len(s)-1])
else:
  anss.append([s[len(s)-1]])
pans = 0
#print(anss)
for i in anss:
  pans += len(i) // 2
#print(f)
if len(anss) == 1 and len(anss[0]) % 2 == 1:
  print(len(list(s*k))//2)
elif f == 1:
  print(pans*k-1)
else:
  print(pans*k)

Submission status: Runtime Error

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

if s[0] == s[-1] and s[0] != s[1] and s[0] != s[-2]:
  tmp += k
print(cnt*k + tmp)

Submission status: Runtime Error

Solution code:
from itertools import groupby
import sys
sys.setrecursionlimit(200000)
S,k= [input() for i in range(2)]
K=int(k)
c=0
strs=S*K
strs_1 = [[k, len(list(g))] for k, g in groupby(strs)]
strs_2 = [x[1] for x in strs_1]
strs_2
for k in strs_2:
    if k >=2:
        a = k//2
        c +=a
print(c)

Submission status: Runtime Error

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

count = 0
S = S + "0"

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

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

if len(S) == 1:
    result = K
elif len(S) == 2:
    result = count * K
else: #S[0]からの連続文字列数とS[-1]からの連続文字列数がどちらも奇数ならば「+ 1 × (K-1)」が必要
    if S[0] == S[-1]:
        len_first = 1
        i = 0
        while S[i] == S[i+1]:
            len_first += 1
            i += 1
        
        len_last = 1
        i = -1
        while S[i] == S[i-1]:
            len_last += 1
            i -= 1
        
        if len_first % 2 == 1 and len_last % 2 == 1:
            result = count * K + (K-1)
        else:
            result = count * K

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

    else:
        result = count * K
        #print("else2")


print(result)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

s = [v for v in S]

block = []
count = 1

for i in range(len(s)-1):
    if s[i] == s[i+1]:
        count += 1
        if i+2 == len(s):
            block.append(s[i]*count)
    else:
        block.append(s[i]*count)
        count = 1

z = len(block)
t = 0
d = 0

if not z == 1:
    for j in range(len(block)):
        x = len(block[j])
        if x == 1:
            continue
        else:
            if x%2 == 0:
                t += x//2
            elif x%2 == 1:
                t += x - (x+1)//2
    
    if block[0][0] == block[-1][-1]:
        joint = block[0] + block[-1]
        y = len(joint)
        if y%2 == 1:
            d = 0
        else:
            d = 1
else:
    p = len(block[0])
    if p%2 == 0:
        t = p//2
    else:
        t = (p-1)//2
        if K%2 == 1:
            ans = t + p*(K-1)//2                       
            print(ans)
            exit()
        else:
            ans = p + p*(K-2)//2
            print(ans)
            exit()

ans = t*K + d*(K-1)
print(ans)