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, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf, eps, mod = 10 ** 20, 1.0 / 10**10, 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def _cumsum(l): return list(itertools.accumulate(l))

"""
なかのものと端のもの
両端が一緒かどうか
一緒なら、K-1個変える
中を変える/2切り下げ
まず、端が一緒か
"""
S = input()
K = _I()
ans = 0

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

if S[0] == S[-1]:
    if S[-1] == S[-2]:
        if S[-2] != S[-3]:
            ans += 1
    S = S[:-1] + '?'
    ans += K - 1
    # 端は上で変えられた
else:
    pass

# 端変えないので、中だけ
previous = 'HOGE'
for s in S:
    if s == previous:
        ans += K
        previous = 'HOGE'
    else:
        previous = s
print(ans)

Submission status: Runtime Error

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

#(方針)Sの「先頭(の連続)」と「末尾(の連続)」が同じ文字ならそこも連続に
#2連続→1回で置換できる,3連続→1回で置換できる,　「N連続→N//2回で置換できる」

A = [] #連続でちぎるようのリスト
count = 1
for i in range(n): 
 if(i != 0):
  if(S[i]== tmp): count += 1
  if((S[i] != tmp)or(i == n-1)):
    A.append([tmp,count])
    count = 1
 tmp = S[i]
if(n == 1): A.append(S[0],1)

ans = 0
for i in range(len(A)):
  if((i != 0)and(i != len(A)-1)):
   ans += (A[i][1]//2)*K
     
if(len(A) == 1):
   ans += (A[0][1]*K)//2  
elif(A[0][0] == A[-1][0]):
   ans += (A[0][1]//2) + (A[-1][1]//2) + ((A[0][1]+A[-1][1])//2)*(K-1) #最初の二項 : 結合後の先頭と末尾分 
elif(A[0][0] != A[-1][0]):
   ans += (A[0][1]//2)*K  
   ans += (A[-1][1]//2)*K
print(ans)

Submission status: Runtime Error

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

#(方針)Sの「先頭(の連続)」と「末尾(の連続)」が同じ文字ならそこも連続に
#2連続→1回で置換できる,3連続→1回で置換できる,　「N連続→N//2回で置換できる」

A = [] #連続でちぎるようのリスト
count = 1
for i in range(n): 
 if(i != 0):
  if(S[i]== tmp): count += 1
  if((S[i] != tmp)or(i == n-1)):
    A.append([tmp,count])
    count = 1
 tmp = S[i]
if(n == 1): A.append(A[0],1)

ans = 0
for i in range(len(A)):
  if((i != 0)and(i != len(A)-1)):
   ans += (A[i][1]//2)*K
     
if(len(A) == 1):
   ans += (A[0][1]*K)//2  
elif(A[0][0] == A[-1][0]):
   ans += (A[0][1]//2) + (A[-1][1]//2) + ((A[0][1]+A[-1][1])//2)*(K-1) #最初の二項 : 結合後の先頭と末尾分 
elif(A[0][0] != A[-1][0]):
   ans += (A[0][1]//2)*K  
   ans += (A[-1][1]//2)*K
print(ans)

Submission status: Runtime Error

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

#(方針)Sの「先頭(の連続)」と「末尾(の連続)」が同じ文字ならそこも連続に
#2連続→1回で置換できる,3連続→1回で置換できる,　「N連続→N//2回で置換できる」

A = [] #連続でちぎるようのリスト
count = 1
for i in range(n): 
 if(i != 0):
  if(S[i]== tmp): count += 1
  if((S[i] != tmp)or(i == n-1)):
    A.append([tmp,count])
    count = 1
 tmp = S[i]

ans = 0
for i in range(len(A)):
  if((i != 0)and(i != len(A)-1)):
   ans += (A[i][1]//2)*K
     
if(len(A) == 1):
   ans += (A[0][1]*K)//2  
elif(A[0][0] == A[-1][0]):
   ans += (A[0][1]//2) + (A[-1][1]//2) + ((A[0][1]+A[-1][1])//2)*(K-1) #最初の二項 : 結合後の先頭と末尾分 
elif(A[0][0] != A[-1][0]):
   ans += (A[0][1]//2)*K  
   ans += (A[-1][1]//2)*K
print(ans)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf, eps, mod = 10 ** 20, 1.0 / 10**10, 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def _cumsum(l): return list(itertools.accumulate(l))

"""
なかのものと端のもの
両端が一緒かどうか
一緒なら、K-1個変える
中を変える/2切り下げ
まず、端が一緒か
"""
S = input()
K = _I()

ans = 0
if S[0] == S[-1]:
    if S[-1] == S[-2]:
        ans += 1
    S = S[:-1] + '?'
    ans += K - 1
    # 端は上で変えられた
else:
    pass

# 端変えないので、中だけ
previous = 'HOGE'
for s in S:
    if s == previous:
        ans += K
        previous = 'HOGE'
    else:
        previous = s
print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
H=S[0]

flag=0
if(S[0]==S[len(S)-1]):
    flag=1
if(flag==0):
    for i in range(len(S)-1):
        if(S[i]==S[i+1]):
            S[i+1]="#"
            count+=1        
    print(count*K)
    exit()

flag2=0
for i in range(len(S)-1):
    if(S[i]!=S[i+1]):
        flag2=1
if(flag2==0):
    print((len(S)*K)//2)
    exit()

S1=S[:]
S2=S[:]+S[:]
S3=S[:]+S[:]+S[:]
RS=S[:]

count1=0
for i in range(len(S1)-1):
    if(S1[i]==S1[i+1]):
        S1[i+1]="#"
        count1+=1
count2=0
for i in range(len(S2)-1):
    if(S2[i]==S2[i+1]):
        S2[i+1]="#"
        count2+=1
count3=0
for i in range(len(S3)-1):
    if(S3[i]==S3[i+1]):
        S3[i+1]="#"
        count3+=1

N=count3-count2
E=count2-N

while(True):
    if(RS[0]==H):
        RS.pop(0)
    elif(RS[len(RS)-1]==H):
        RS.pop(len(RS)-1)
    else:
        break
rcount=0
for i in range(len(RS)-1):
    if(RS[i]==RS[i+1]):
        RS[i+1]="#"
        rcount+=1

print(count1+(K-1)*(count2-count1))

Submission status: Runtime Error

Solution code:
from itertools import groupby
s = input()
k = int(input())
l = [[k, len(list(g))] for k, g in groupby(s)]
ans = 0
ans2 = 0
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
if len(l) > 1:
  if l[0][0] == l[-1][0]:
    if l[0][1] % 2 == 1 and  l[-1][1] % 2 == 1 :
        ans2 = k - 1
if len(s) == 1:
  print(k // 2)
elif len(l) == 1:
  if l[0][1] % 2 == 1:
    print(((l[0][1] // 2) * k + (k - 1))
  else:
    print((l[0][1] // 2) * k)
else:
  print(ans * k + ans2)

Submission status: Runtime Error

Solution code:
strs = input()
n = int(input())
import itertools

if(n == 1):
  print(0)
  exit()

if(len(strs) == 1):
  print(1)
  exit()

res = 0
for k, g in itertools.groupby(strs**n):
  cnt = len(list(g))
  if(cnt >= 2):
    res += cnt // 2
print(res)

Submission status: Runtime Error

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

if(len(set(S)) == 1):
    print(len(S) // 2 * K)
else:
    m = 0
    ans = 0
    if(S[0] == S[-1]):
        l = 0
        r = len(S) - 1
        while(S[l] == S[l+1]):
            l += 1
        while(S[-r] == S[-r-1]):
            r += 1
        m = l//2 + r//2 - (l+r)//2
        m *= K - 1

    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            ans += 1
            S[i+1] = "3"
    ans *= K
    ans -= m
    print(ans)

Submission status: Runtime Error

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

if(len(set(S)) == 1):
    print(len(S) // 2 * K)
else:
    m = 0
    ans = 0
    if(S[0] == S[-1]):
        l = 0
        r = len(S) - 1
        while(S[l] == S[l+1]):
            l += 1
        while(S[-r] == S[-r]):
            r += 1
        m = l//2 + r//2 - (l+r)//2
        m *= K - 1
    
    for i in range(len(S)-1):
        if(S[i] == S[i+1]):
            ans += 1
            S[i+1] = "3"
    ans *= K
    ans -= m
    print(ans)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
Ss = K * S
i = 0
count = 0
end = 0

N = len(S)
Ns = len(Ss)
while i <= Ns-3:
  if Ss[i] == Ss[i+1] == Ss[i+2]:
    count += 1
    i += 2
  
  elif Ss[i] == Ss[i+1] != Ss[i+2]:
    count += 1
    i += 2
    
  else:
    i += 1
    
if S[N-2] == S[N-1]:
  end += 1

else:
  end = 0
    
print(count + end)

Submission status: Runtime Error

Solution code:
S = list(input())
K = int(input())
Ss = K * S
i = 0
count = 0
end = 0

N = len(S)
Ns = len(Ss)
while i <= Ns-3:
  if Ss[i] == Ss[i+1] == Ss[i+2]:
    count += 1
    i += 2
  
  elif Ss[i] == Ss[i+1] != Ss[i+2]:
    count += 1
    i += 1
    
  else:
    i += 1
    
if S[N-2] == S[N-1]:
  end += 1

else:
  end = 0
    
print(count + end)

Submission status: Runtime Error

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

i = 0
count = 0
end = 0

N = len(S)
while i <= N-2:
  if S[i] == S[i+1] == S[i+2]:
    count += 1
    i += 2
  
  elif S[i] == S[i+1] != S[i+2]:
    count += 1
    i += 1
    
  else:
    i += 1
    
if S[0] == S[N-1] and S[0] != S[1] and S[N-1] != S[N-2]:
  end += K-1

else:
  end = 0
    
print(count * K + end)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if len(s)==2:
  if s[0]==s[1]:
    c+=1
    s="*$"
if len(s)==3:
  if s[0]==s[1] and s[0]==s[2]:
    s="%*}"
    c=4
    k=1
  elif s[0]==s[1] and s[0]!=s[2]:
    s="*`|"
    c+=1
if 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[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
  a=True
  s="|"+s[1:]
  c+=1
if s[len(s)-1]==s[len(s)-2]:
  c+=1
  s=s[0:len(s)-1]+"?"
if s[0]==s[1]:
  c+=1
  s=s[0]+"<"+s[2:]
for i in range(len(s)-2):
  if s[i]==s[i+1]:
    c+=1
    s=s[0:i+1]+">"+s[i+2:]
if a==True:
  c=c*k-1
else:
  c=c*k

print(c)

Submission status: Runtime Error

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

print(c)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
a=False
c=0
if len(s)==2:
  if s[0]==s[1]:
    c+=1
    s="*@"
if len(s)==3:
  if s[0]==s[1] and s[0]==s[2]:
    s="@*"
    c=4
    k=1
if 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[len(s)-1] and s[len(s)-1]!=s[len(s)-2]:
  a=True
  s="@"+s[1:]
  c+=1
if s[len(s)-1]==s[len(s)-2]:
  c+=1
  s=s[0:len(s)-1]+"@"
if s[0]==s[1]:
  c+=1
  s=s[0]+"@"+s[2:]
for i in range(len(s)-2):
  if s[i]==s[i+1]:
    c+=1
    s=s[0:i+1]+"@"+s[i+2:]
if a==True:
  c=c*k-1
else:
  c=c*k

print(c)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
n = len(list(S))
i = 1
c = 0
while i < n-1:
	if S[i-1]==S[i] or S[i]==S[i+1]:
    	c += 1
    i += 2
c *= N
if S[0]==S[-1]:
	c += N-1
print(c)

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
n = len(S)
i = 1
c = 0
while i < n-1:
	if S[i-1]==S[i] or S[i]==S[i+1]:
    	c += 1
    i += 2
c *= N
if S[0]==S[-1]:
	c += N-1
print(c)

Submission status: Runtime Error

Solution code:
s=input()
k=int(input())
t=s*k
ans_1=0
ans_2=0
#文字列が全一致かどうかを判定
"""
if len(s)==s.count(s[0]):
    print((len(s)/2)*k)
"""
if len(set(s))==1:
    print((len(s)/2)*k)
else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            ans_1+=1
    if s[0]==s[len(s)-1]:
        ans_2=ans_1+1
        print(ans_2*(k-1)+ans_1)
    else:
        print(ans_1*k)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- Coding: utf-8 -*-

import sys


def main():
    S = input()
    K = int(input())
    ary = []
    ans = 0
    n = 1

    for i, s in enumerate(S[1:]):
        if s == S[i]:
            n += 1
        else:
            ary.append(n)
            n = 1
    if n != 1:
        ary.append(n)

    if S[0] == S[-1]:
        for num in ary[1:-1]:
            ans += num//2
        ans *= K
        if len(ary) == 1:
            ans += ary[0]*K//2
        else:
            ans += (ary[0]+ary[-1])//2*(K-1)
            ans += ary[0]//2+ary[-1]//2
    else:
        for num in ary:
            ans += num//2
        ans *= K
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = list(input())
k = int(input())
count = 0
flag = 0
if s[0]==s[-1]:
    if s[0]==s[1]:
        s[0]='_'
        count += 1*k
        flag += 1
    if s[-1]==s[-2]:
        s[-1]='_'
        count += 1*k
        flag += 1
    if flag==0:
        count += 1*(k-1)
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        s[i+1]='_'
        count+=1*k
print(count)

Submission status: Runtime Error

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

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

con = list(nchars(S, 1))
#print(con)
if len(con)==1:
    print(int(len(S*K)/2))

else:
    cons = []
    for coni in con:
        cons.append(int(len(coni)/2))

    c1 = cons[0]

    c2 = 0
    for i in range(len(cons)):
        if i != 0 and i != len(cons)-1:
            c2 += cons[i]

    if con[0][0]==con[-1][0]:
        c3 = int(len(con[0]+con[-1])/2)
    else:
        c3 = int(len(con[0])/2) + int(len(con[-1])/2)

    c4 = int(cons[-1])

    print(int(c1 + c2*K + c3 *(K-1) + c4))

Submission status: Runtime Error

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

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

con = list(nchars(S, 2))
cons = []
for coni in con:
    cons.append(int(len(coni)/2))

c1 = cons[0]

c2 = 0
for i in range(len(cons)):
    if i != 0 and i != len(cons)-1:
        c2 += consi[i]

c3 = int(cons[0]+cons[len(cons)-1])/2

c4 = int(cons[len(cons)-1])

print(int(c1 + c2*K + c3 *(K-1) + c4))

Submission status: Runtime Error

Solution code:
#A
s = input()
k = int(input())
s_sum = 0

s_f = 1
s_l = 1

if s[0] == s[-1]:
    
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                s_f += 1
            else:
                break
    
    for i in range(len(s)-1 , -1, -1):
        if i != len(s)-1:
            if s[i] == s[i+1]:
                s_l += 1
            else:
                break
if s_f == len(s) and len(s) % 2 == 1:
    print((k * len(s)) / 2)
    else:   
    if s_f + s_l % 2 == 0 and s_f != 1 and s_l != 1:
        s_sum += 1

    n = 1
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                n += 1
            else:
                if n != 1:
                    if n % 2 == 0:
                        s_sum += (n / 2)
                    else:
                        s_sum += ((n - 1)/ 2)
                    n = 1

        if i == (len(s) - 1):
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)

    print(int(s_sum * k))

Submission status: Runtime Error

Solution code:
#A
s = input()
k = int(input())
s_sum = 0

if s[0] == s[-1]:
    s_f = 1
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                s_f += 1
            else:
                break
    s_l = 1
    for i in range(len(s)-1 , -1, -1):
        if i != len(s)-1:
            if s[i] == s[i+1]:
                s_l += 1
            else:
                break
                
if s_f + s_l % 2 == 0:
    s_sum += 1

n = 1
for i in range(len(s)):
    if i != 0:
        if s[i] == s[i-1]:
            n += 1
        else:
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)
                n = 1
                
    if i == (len(s) - 1):
        if n != 1:
            if n % 2 == 0:
                s_sum += (n / 2)
            else:
                s_sum += ((n - 1)/ 2)

print(int(s_sum * k))

Submission status: Runtime Error

Solution code:
s = input()
s = s + "1"
k = int(input())
ans = 0
lis = []
tem = 1
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tem += 1
  else:
    lis.append(tem)
    tem = 1
if s[0] == s[-2]:
  t = lis[0]+lis[-1]
  for i in range(1,len(lis)-1):
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans*= k
  if t%2 == 0:
    ans += (k-1)*t//2
  else:
    ans += (k-1)*(t-1)//2
  if lis[0]%2 == 0:
    ans += (k-1)*lis[0]//2
  else:
    ans += (k-1)*(lis[0]-1)//2
  if lis[-1]%2 == 0:
    ans += (k-1)*lis[-1]//2
  else:
    ans += (k-1)*(lis[-1]-1)//2
else:
  for item in lis:
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans *= k
  
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
s = s + "1"
k = int(input())
ans = 0
lis = []
tem = 1
for i in range(len(s)):
  if s[i] == s[i+1]:
    tem += 1
  else:
    lis.append(tem)
    tem = 1
if s[0] == s[-2]:
  t = lis[0]+lis[-1]
  for i in range(1,lis(lis)-1):
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans*= k
  if t%2 == 0:
    ans += (k-1)*t//2
  else:
    ans += (k-1)*(t-1)//2
  if lis[0]%2 == 0:
    ans += (k-1)*lis[0]//2
  else:
    ans += (k-1)*(lis[0]-1)//2
  if lis[-1]%2 == 0:
    ans += (k-1)*lis[-1]//2
  else:
    ans += (k-1)*(lis[-1]-1)//2
else:
  for item in lis:
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans *= k
  
print(ans)

Submission status: Runtime Error

Solution code:
import re

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

frag = False

def nchars(s, n):
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
s_ = list(nchars(s,1))

for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i

if s1 == s2:
  frag = True

l = [len(i) for i in s_]

c = len(l[0])//2

c1 = 0
c2 = 0

for i in range(len(l)-1):
  if i != len(l)-1:
    c1 += l[i+1]//2
  else:
    if frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
  
if not frag:
  print(c1*k)
else:
  print(c1)

Submission status: Runtime Error

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

if len(set(S))==1:
    ans =(len(S)*K)//2

else:
    S_ans =  0
    cnt = 1
    for i in range(1,len(S)):
        if S[i] == S[i-1]:
            cnt += 1
            if i ==len(S)-1:
                S_ans += cnt//2
        else:
            S_ans += cnt//2
            cnt = 1
    ans =S_ans*K

    if S[0] == S[-1]:
        a, i =1, 1
        while S[0]==S[i]:
            a += 1
            i += 1
        b, i =1, 1
        while S[-1]==S[-1- i]:
            b += 1
            i += 1
    
    ans = ans - (a//2 +b//2 -(a+b)//2)*(K-1)        
    

print(ans)

Submission status: Runtime Error

Solution code:
#coding: utf-8
s = str(input())
k = int(input())

count = 0

for i in range(len(s)-1):
	# print(s[i+1])
	if s[i]==s[i+1]:
		s = s[:i+1] + str(i+1) + s[i+2:]
		count += 1

ans = count * k
if s[0]==s[len(s)-1] and s[0]!=s[1] and s[len(s)-1]!=s[len(s)-2]:
	ans = ans + k -1

# print(s)
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2*(-len(s)~g[0])or k-1))

Submission status: Runtime Error

Solution code:
S = input()
N = int(input())
c=0
f=0
if S[0]==S[len(S)-1]:
    c+=1
    f=1
    s=1
for i in range(len(S)-1):
   if S[i+1] == S[i]:
        if f!=1:
            c+=1
            f=1
            if i==(len(S)-2) and s==1:
                c-=1
        else:f=0
   else:
        f=0
print(c*N)

Submission status: Runtime Error

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt

def connect_cnt_ab(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	return cnt[0], cnt[-1]


if S[0] != S[-1]:

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:

	connect_cnt = connect_cnt(S)
	a, b = connect_cnt_ab(S)
	ans = connect_cnt*K - ((a//2 + b//2 - (a+b)//2)) (K-1)
	# a : 最初の何文字連続 b : 最後の何文字連続
	# 全体から最初と最後の重なりでの増減分を引く
	print(ans)

Submission status: Runtime Error

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

count =[]
while len(x)>0:
    if len(x)==1:
        count.append(1)
        x =x[:-1]
        break
    if x[-1] != x[-2]:
        count.append(1)
        x =x[:-1]
    else:
        k = 2
        while True:
            if len(x) == k :
                count.append(k)
                break
            if x[-1] == x[-1-k]:
                k +=1
            else:
                count.append(k)
                break
        x =x[:-k]       
#print(count[::-1])    
ans = sum(list(map(lambda x :x//2,count)))
print(ans)

Submission status: Runtime Error

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

s_list = list(S)

modified = "mdf"

def solve():
    # その単語に1種類の文字しかなければ、合計の文字数を2で割捨てした数
    if len(set(s_list)) == 1:
        return (len(s_list * K))//2
    
    cnt = 1
    cnt_list = []
    for i in range(len(S)-1):
        if s_list[i] == s_list[i+1]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    
    # 最後の対応
    # cnt = 1の時、最後は1文字だけ
    # cnt >= 2の時、最後は同じ文字が続いてる
    cnt_list.append(cnt)
    
    # 最初と最後が同じ時は最初と最後を足したやつを作る必要。
    head = cnt_list[0]
    tail = cnt_list[-1]
    body = cnt_list[1:-1]
    
    if s_list[0] == s_list[-1]:
        head_tail = cnt_list[0] + cnt_list[-1]
        return head//2 + tail//2 + (sum(body)//2)*K + (head_tail//2)*(K-1)
    else:
        return (head//2 + tail//2 + sum(body)//2) * K

result = solve()

print(result)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=S*K
s=1
b=0 
for i in range(len(T)-1):
    if T[i]==T[i+1]:
        s+=1
        if i+1==len(T)-1:
            b+=s//2
    else:
        b+=s//2
        s=1
print(b)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=S*K
i=0
s=1
b=0 #sは連続して並ぶ文字の数 #b繰り替えし
while i<len(T)-1:
    if T[i]==T[i+1]:
        s+=1
        if i+1==len(T)-1:
            b+=s//2
    else:
        b+=s//2
        s=1
    i+=1
print(b)

Submission status: Runtime Error

Solution code:
S=input()
K=int(input())
T=S*K
i=0
s=1
b=0 #sは連続して並ぶ文字の数 #b繰り替えし
while i<len(T)-1:
    if T[i]==T[i+1]:
        s+=1
        i+=1
    else:
        b+=s//2
        s=1
        i+=1
print(b)

Submission status: Runtime Error

Solution code:
word = input()  # 繰り返す単位単語
times = int(input())  # 繰り返す回数
count = 0
words = ""

# ここで繰り返す

for i in range(times):
    words += word

# ここで文字列が完成するので、後は他の文字に変えて相違なるようにする。
#　ここでは繰り返し処理を行うようにするので、while文を用いる。

wordlist = list(words)  # 入力された単語ひとつひとつをリストの要素とする。

# リストの分だけ繰り返し処理を行い、もし右隣のものと一致してしまうときには別のものに変えるが、
#　最初と最後の要素以外は、そのあとに左隣のものと比較する必要がある。ここではアスキーコードを用いる。９７から122までにアルファベットは存在している。

if ord(wordlist[0]) == ord(wordlist[1]):
    wordlist[0] = chr(ord(wordlist[0]) + 1)
    count += 1
  
for j in range(1, len(wordlist) - 1):
    if ord(wordlist[j - 1]) != ord(wordlist[j]) and ord(wordlist[j]) != ord(wordlist[j + 1]):
        count += 0
    else:
        while ord(wordlist[j - 1]) == ord(wordlist[j]) or ord(wordlist[j]) == ord(wordlist[j + 1]):
            if (ord(wordlist[j]) + 1) > 122:
                wordlist[j] = chr(ord(wordlist[j]) + 1 - 122 + 96)
            else:
                wordlist[j] = chr(ord(wordlist[j]) + 1)
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
word = input()  # 繰り返す単位単語
times = int(input())  # 繰り返す回数
count = 0

# ここで繰り返す

for i in range(times):
    words += word

# ここで文字列が完成するので、後は他の文字に変えて相違なるようにする。
#　ここでは繰り返し処理を行うようにするので、while文を用いる。

wordlist = list(words)  # 入力された単語ひとつひとつをリストの要素とする。

# リストの分だけ繰り返し処理を行い、もし右隣のものと一致してしまうときには別のものに変えるが、
#　最初と最後の要素以外は、そのあとに左隣のものと比較する必要がある。ここではアスキーコードを用いる。９７から122までにアルファベットは存在している。

if ord(wordlist[0]) == ord(wordlist[1]):
    wordlist[0] = chr(ord(wordlist[0]) + 1)
    count += 1
  
for j in range(1, len(wordlist) - 1):
    if ord(wordlist[j - 1]) != ord(wordlist[j]) and ord(wordlist[j]) != ord(wordlist[j + 1]):
        count = count
    else:
        while ord(wordlist[j - 1]) == ord(wordlist[j]) or ord(wordlist[j]) == ord(wordlist[j + 1]):
            if (ord(wordlist[j]) + 1) > 122:
                wordlist[j] = chr(ord(wordlist[j]) + 1 - 122 + 96)
            else:
                wordlist[j] = chr(ord(wordlist[j]) + 1)
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
word = input()  # 繰り返す単位単語
times = int(input())  # 繰り返す回数
count = 0

# ここで繰り返す

for i in range(times):
    words += word

# ここで文字列が完成するので、後は他の文字に変えて相違なるようにする。
#　ここでは繰り返し処理を行うようにするので、while文を用いる。

wordlist = list(words)  # 入力された単語ひとつひとつをリストの要素とする。

# リストの分だけ繰り返し処理を行い、もし右隣のものと一致してしまうときには別のものに変えるが、
#　最初と最後の要素以外は、そのあとに左隣のものと比較する必要がある。ここではアスキーコードを用いる。９７から122までにアルファベットは存在している。

if ord(wordlist[0]) == ord(wordlist[1]):
    wordlist[0] = chr(ord(wordlist[0]) + 1)
    count += 1
  
for j in range(1, len(wordlist) - 1):
    if ord(wordlist[j - 1]) != ord(wordlist[j]) || ord(wordlist[j]) != ord(wordlist[j + 1]):
        count = count
    else:
        while ord(wordlist[j - 1]) == ord(wordlist[j]) || ord(wordlist[j]) == ord(wordlist[j + 1]):
            if (ord(wordlist[j]) + 1) > 122:
                wordlist[j] = chr(ord(wordlist[j]) + 1 - 122 + 96)
            else:
                wordlist[j] = chr(ord(wordlist[j]) + 1)
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k - 1
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left // 2) + (mid // 2) * (k - 1) + (right // 2)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0
else:
    assert (0 != 0)
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    if sz > 1:
        ans += (sz // 2)
        sz = 0

    ans *= k

print ans