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:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    return li

lin = cou(s)

if len(lin) == 1:
    print(lin[0]*k//2)
    exit()

if k > 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0]
else:
    if len(s) == 1:
        print(0)
        exit()

point = 0
for i in range(1,len(lin)):
    point += lin[i]//2
point = point*k+lin[0]//2
print(point)

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 :
  ANS -= 1
print(ANS)

Submission status: Runtime Error

Solution code:
s=list(input())
k=int(input())
if k>1:
	s.append(s[0])
ans=0

last=1
for n in range(len(s)-1):


	if s[n]==s[n+1]:

		last+=1
		if last%2==0:
			ans+=1

	else:
		last=1

if s[-1]==s[-2] and s[-2]==s[-3]:
	ans-=1

print(ans*k)

Submission status: Runtime Error

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)

if S[0] == S[len(S)-1] and S[0] != S[1] and S[len(S)-2] != S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
T = S * K
count = 0
for i, this in enumerate(T):
  if i==0:
    pre = this
  else:
    if pre == this:
      this = 0
      pre = this
      count += 1
    else:
      pre = this
print(count)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
s = s*k
s = list(s)
h = len(s)
j = 0
num = 0

for i in range(1,h):
    if s[i] == s[i-1] and j == 0:

        num = num + 1
        j = 1
    else:
        j = 0

print(num)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
brob = 0
temp = 1
first = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        temp += 1
    else:
        if first == 0:
            first = temp
        brob += temp // 2
        temp = 1S = input()
K = int(input())
brob = 0
temp = 1
first = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        temp += 1
    else:
        if first == 0:
            first = temp
        brob += temp // 2
        temp = 1
        
brob += temp // 2
last = temp

if K == 1:
    print(brob)

else:
    if last == len(S):
        print(len(S) * K // 2)
    
    elif S[-1] != S[0]:
        print(brob * K)
    

    elif last % 2 and first % 2:
        print(brob * K + K - 1)

    else:
        print(brob * K)

        
brob += temp // 2
last = temp

if K == 1:
    print(brob)

else:
    if len(S) == 1:
        print(K // 2)
    
    elif S[-1] != S[0]:
        print(brob * K)

    elif last % 2 and first % 2:
        print(brob * K + K - 1)

    else:
        print(brob * K)

Submission status: Runtime Error

Solution code:
S = input()
K = int(input())
count_list = []
count = 0
sum = 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)
                sum += -(-count // 2)
            count = 0
if count > 0:
    sum += -(-count // 2)
    count_list.append(count)
if S[0] != S[1] and S[-1] != S[-2] and S[0] == S[-1]:
    count_list.append(1)
    sum += 1
print(sum * K)

Submission status: Runtime Error

Solution code:
from itertools import groupby


s = input()
s_doub = s + s
k = int(input())
s_sub = [[h, len(list(g))] for h, g in groupby(s)]
s_doub_sub = [[h, len(list(g))] for h, g in groupby(s_doub)]
cnt_s = 0
for i in range(len(s_sub)):
    cnt_s += s_sub[i][1] // 2
cnt_s_doub = 0
for i in range(len(s_doub_sub)):
    cnt_s_doub += s_doub_sub[i][1] // 2
if len(list(set(s))) == 1:
    if s_sub[0][1] % 2 == 1:
        ans = cnt_s * k + 1
    else:
        ans = cnt_s * k
else:
    if cnt_s_doub % cnt_s == 0:
        ans = cnt_s * k
    else:
        ans = cnt_s * k + k - 1
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
s = s*k
s = list(s)
h = len(s)
s.append("A")
j = 0
num = 0

for i in range(1,h):
    if s[i] == s[i-1] and j == 0:
        
        num = num + 1
        j = 1
    else:
        j = 0

print(num)

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 * 2
    count_2 = a(new_s)
    count_3 = a(new_s + s)
    diff = count_3 - count_2
    print(count_3 + (k-3) * diff)

Submission status: Runtime Error

Solution code:

from sys import stdin

S = str(stdin.readline().rstrip())
K = int(stdin.readline().rstrip())

lenS = len(S)
ans = 0

if S[0] == S[lenS-1]:
    S2 = S*2
    a = 0#位置
    while a < lenS*2-1:
        ans2 = 0
        if S2[a] == S2[a+1]:
            b = 2
            x = S2[a]
            for i in range(1,lenS*2-a):
                try:
                    if x == S2[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans2 += b//2
            a += 1+b
        else:
            a += 1
    a = 0#位置
    while a < lenS-1:
        ans1 = 0
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans1 += b//2
            a += 1+b
        else:
            a += 1
    differ = ans2-ans1
    ans = differ*(K-1) + ans1*K
else:
    a = 0#位置
    while a < lenS-1:
        if S[a] == S[a+1]:
            b = 2
            x = S[a]
            for i in range(1,lenS-a):
                try:
                    if x == S[a+1+i]:
                        b += 1
                    else:
                        break
                except:
                    b = 2
            ans += b//2
            a += 1+b
        else:
            a += 1
    ans = ans*K


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())
    pool = list(set(string.ascii_lowercase) - set(s))
    length = len(pool)
    print(count_changes(s, k, pool)[0])

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
s = s*k
s = list(s)
h = len(s)
s.append("A")
j = 0
num = 0

for i in range(1,h):
    if s[i] == s[i-1] and j == 0:
        print(i)
        num = num + 1
        j = 1
    else:
        j = 0

print(num)

Submission status: Runtime Error

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)
if S[0]==S[len(S)-1] and S[0]!=S[1] and S[len(S)-2]!=S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Runtime Error

Solution code:
s=input()
s=[i for i in s]
k=int(input())
a=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        a+=1
        s[i+1]='z'
if s[0]==s[-1]:
    a+=1
    print(a*k-1)
else:
    pritn(a*k)

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 = list(input())
k = int(input())
cnt = 0
if s[0] == s[-1]:
  cnt += 1
  s[1] = "A"
f = False
for i in range(1, len(s)):
  if f:
    f = False
    continue
  if s[i] == s[i - 1]:
    cnt += 1
    f = True
ans = cnt * k
print(ans)

Submission status: Runtime Error

Solution code:
s=list(str(input()))
n=int(input())
s_base = copy.deepcopy(s)
i0 = s[0]
count = 0
if len(s) == 1:
    ans = n//2
else:
    #r1 = n//2 + n%2
    #s

    for i, name in enumerate(s[1:]):
        if name == i0:
            i0 = -1
            count += 1
            s[i+1] = i0
        else:
            i0 = name
    if s[0] == s[-1]:
        al = n-1
        if s[1] == -1:
            s2 = copy.deepcopy(s)
            for i in s_base:
                s2.append(i)
            i0 = s2[0]
            count2 = copy.deepcopy(count)
            for i, name in enumerate(s2[1:]):
                if name == i0:
                    i0 = -1
                    count2 += 1
                    s2[i+1] = i0
                else:
                    i0 = name
            al = (count2 - count*2)*(n//2)
    else:
        al = 0
    ans = count*n + al
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin


def main():
    S = stdin.readline().rstrip()
    K = int(stdin.readline().rstrip())
    count = 0
    skip = False
    for i, c in enumerate(S[:-1]):
        if skip:
            skip = False
            continue
        nc = S[i + 1]
        if c == nc:
            count += 1
            skip = True
    count = count * K
    if S[0] == S[-1] and S[-1] != S[-2]:
        count += K - 1
    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
n = len(s)
count = 0
C = 0
change = 0
A =[]
for i in range(n-1):
  if s[i] == s[i+1]:
    count += 1
    C += 1
  else:
    if count == 1:
      change += 1
    elif count%2 == 0:
      change += count//2
    else:
      change += (count+1)//2
    A.append(count)
    count = 0
    C = 0 
if count == 1:
  change += 1
elif count%2 == 0:
  change += count//2
else:
  change += (count+1)//2

if count != 0:
  A.append(count)
if C%2 == 0 and A[0]%2 == 0 and s[0] == s[n-1]:
  print(change*k + k-1)
else:
  print(change*k)

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

san = s*2

def cou(st):
    li = []
    point = 1
    for i in range(len(st)-1):
        if st[i] == st[i+1]:
            point += 1
            if i == len(st)-2:
                li.append(point)
        else:
            li.append(point)
            point = 1
    return li

lin = cou(s)

if len(lin) == 1:
    print(lin[0]*k//2)
    exit()

if k >= 1:
    if s[0] == s[-1]:
        lin[-1] += lin[0] 

point = 0
for i in range(1,len(lin)):
    point += lin[i]//2
point = point*k+lin[0]//2
print(point)

Submission status: Runtime Error

Solution code:
##################################################################

if __name__ == "__main__":
    Input = [input() for i in range(2)]
    # = list(map(int,Input[0].split()))
    S = str(Input[0])
    K = int(Input[1])

    n = len(S)
    ren = 1
    bef = S[0]
    cont = []
    for s in range(1,n):
        if bef == S[s]:
            ren += 1
            bef = S[s]
            if s == n-1:
                cont.append(ren)                
        else:
            cont.append(ren)
            ren = 1
            bef = S[s]

    ans = (sum([c//2 for c in cont]))*K
    if S[0] == S[-1]:
        if cont[0]%2==1 and cont[-1]%2==1:
            ans += K-1
    print(ans)

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]
            if (p[1] == 6):
                cnt -= 1
        cnt *= k
    print(cnt, end='')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
n = len(s)
count = 0
C = 0
change = 0
A =[]
for i in range(n-1):
  if s[i] == s[i+1]:
    count += 1
    C += 1
  else:
    if count == 1:
      change += 1
    elif count%2 == 0:
      change += count//2
    else:
      change += (count+1)//2
    A.append(count)
    count = 0
    C = 0 
if count == 1:
  change += 1
elif count%2 == 0:
  change += count//2
else:
  change += (count+1)//2

if count != 0:
  A.append(count)
print(C,change,count)
if C%2 == 0 and A[0]%2 == 0 and s[0] == s[n-1]:
  print(change*k + k-1)
else:
  print(change*k)

Submission status: Runtime Error

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

T = ''
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 = list(input())
k = int(input())
cnt = 0
if s[1] == s[-1]:
  cnt += 1
  s[1] = "A"
f = False
for i in range(1, len(s)):
  if f:
    f = False
    continue
  if s[i] == s[i - 1]:
    cnt += 1
    f = True
ans = cnt * k
print(ans)

Submission status: Runtime Error

Solution code:
s = input()
k = int(input())
s = s + s[0]

cnt = 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)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0

for i in range(l-1):
  if i==0 and d[0]==d[l-1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    else:
      d[i+1]="1"
      count+=1
print(count*n)

Submission status: Runtime Error

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

end=0
if K!=1:
    S_old = S
    i=0
    while S_old[-1] == S_old[i]:
        S= S + S[i]
        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:
    print(ans)
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:
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] or len(S)==2:
  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:
().strip()
K = int(input())
if len(S)==1:
    cnt = K//2
else:
    A = []
    cur = 0
    for i in range(1,len(S)):
        if S[i]!=S[i-1]:
            if i-cur>=2:
                A.append([S[i-1],cur,i])
            cur = i
    if S[-1]==S[-2]:
        A.append([S[-1],cur,len(S)])
    cnt = 0
    for j in range(len(A)):
        cnt += (A[j][2]-A[j][1])//2
    if S[0]!=S[-1]:
        cnt = cnt*K
    else:
        if S[0]!=S[1] and S[-1]!=S[-2]:
            cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]!=S[-2]:
            if (A[0][2]-A[0][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]!=S[1] and S[-1]==S[-2]:
            if (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
        elif S[0]==S[1] and S[-1]==S[-2]:
            if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
                cnt = cnt*K
            else:
                cnt = cnt+(cnt+1)*(K-1)
print(cnt)

Submission status: Runtime Error

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

cnt = 0
i = 0

while i < len(T):
    if T[i] == T[i+1]:
        cnt += 1
        i += 2
        
    else:
        i += 1

print(cnt)

Submission status: Runtime Error

Solution code:
# 入力                                                                                   
S = input()
K = int(input())

# kindリストに同じ文字が続くものごとに区切ったものを格納                                 
kind = []
back = "0"
len_S = len(S)
i = 0
for moji in S:
    if i == 0:
        back = moji
        i += 1
        continue
    if moji == back[-1]:
        back = moji + back
        if i == len_S-1:
            kind.append(back)
    else:
        kind.append(back)
        back = moji
    i += 1

score = 0
# 最初の文字と最後の文字が異なる時                                                       
if S[0] != S[-1]:
    for word in kind:
        len_word = len(word)
        score += len_word // 2
    print(score*K)
# 最初の文字と最後の文字が一緒の時                                                       
else:
    if len(set(kind)) == 1:
        print((len(kind[0])//2)*K)
    else:
        # 最初と最後がつながることでできる文字列の長さ                                   
        len_sg = len(kind[0] + kind[-1])
        for word in kind[1:-1]:
            len_word = len(word)
            score += len_word // 2
        print(score*K + (len_sg//2)*(K-1) + (len(kind[0]))//2 + (len(kind[-1]))//2 )

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


s = S()
k = I()
L = []
cnt = 0
ret = 1
if len(s) == 1:
    print(k // 2)
else:
    for j in range(1, len(s) - 1):
        if s[j] != s[0]:
            break

    for l in range(len(s) - 1):
        if s[-1] != s[-1 - l]:
            break


    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            ret += 1
        else:
            if ret > 1:
                cnt += ret // 2
            ret = 1


    cnt += ret // 2


    ans = cnt * k
    if s[0] == s[-1] and j % 2 and l % 2:
        ans += k - 1


    print(ans)

Submission status: Runtime Error

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

if len(S) == 1:
    print(K // 2)
else :
    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_list = []
count = 0
for idx, s in enumerate(S):
    if idx == 0:
        s0 = s
    if idx == 1:
        s1 = s
    if idx == len(S)-1:
        s_last = s
    if idx == len(S)-2:
        s_last2 = 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 s0 != s1 and s_last != s_last2 and s0 == s_last:
    count_list.append(1)
sum = 0
for count in count_list:
    sum += -(-count // 2)
print(sum * K)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-

def is_onaji(S):
    pre_moji = S[0]
    for i in range(1, len(S)):
        if S[i] == pre_moji:
            pre_moji = S[i]
            continue
        else:
            return False
    return True


def solve():
    S = input()
    K = int(input())
    N = len(S)

    if N == 1:
        print(K//2)
        return

    # 1回目
    base1 = 0
    pre_moji = S[0]
    for i in range(1, N):
        if S[i] == pre_moji:
            base1 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 1:
        print(base1)
        return

    # 2回目
    base2 = 0
    for i in range(0, N):
        if S[i] = pre_moji:
            base2 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 2:
        print(base1+base2)

    # 3回目
    base3 = 0
    for i in range(0, N):
        if S[i] = pre_moji:
            base3 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 3:
        print(base1+base2+base3)

    if base1 == base2 and base2 == base3:
        print(base1*K)
    elif base1 != base2 and base2 == base3:
        print(base1 + base2*(K-1))
    else:
        # base1とbase2が交互にくる
        print(base1*((K+1)//2) + base2*(K//2))


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
s=input()
s=[i for i in s]
k=int(input())
a=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        a+=1
        s[i+1]='z'
if s[0]==s[-1]:
    ans+=1
print(a*k)

Submission status: Runtime Error

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

l = list(s)

count = 0
new_l = l.copy()

for i, c in enumerate(s[1:]):

    if c == new_l[i]:
        new_l[i + 1] = 0
        count += 1

result = count * k

if new_l[0] == new_l[-1] and s[0]!=s[1]:
    result = +k - 1
print(result)

Submission status: Runtime Error

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

# 先頭と後頭が一致していた場合の例外処理
Sfront = S[0]
Srear  = S[-1]
if Sfront == Srear and K > 1:
    Sleft = S.lstrip(Srear)
    kazu_left = len(S) - len(Sleft)
    ans += int(kazu_left/2)
    
    Sleftright = Sleft.rstrip(Srear)
    kazu_right = len(Sleft) - len(Sleftright)
    ans += int(kazu_right/2)
    
    kazu_wa = len(S) - len(Sleftright)
    ans += int(kazunoko/2) * (K-1)
    
    S = Sleftright
else:
    pass

while len(S) > 0:
#     print(S)
    current = S[0]
    Snext = S.lstrip(current)
    kazu = len(S) - len(Snext)
    ans += int(kazu/2) * K
    S = Snext
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+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
if (count+1+a[0])%2 == 0  and len(a)!=0 and S[0]==S[1] :
    ans+=K-1

print(ans)

Submission status: Runtime Error

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

# 先頭と後頭が一致していた場合の例外処理
Sfront = S[0]
Srear  = S[-1]
if Sfront == Srear and K > 1:
    Sleft = S.lstrip(Srear)
    kazu_left = len(S) - len(Sleft)
    ans += int(kazu_left/2)
    
    Sleftright = Sleft.rstrip(Srear)
    kazu_right = len(Sleft) - len(Sleftright)
    ans += int(kazu_right/2)
    
    kazu_wa = len(S) - len(Sleftright)
    ans += int(kazunoko/2) * (K-1)
    
    S = Sleftright
else:
    pass

while len(S) > 0:
    print(S)
    current = S[0]
    Snext = S.lstrip(current)
    kazu = len(S) - len(Snext)
    ans += int(kazu/2) * K
    S = Snext
print(ans)

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:
s=list(input())
k=int(input())
s=s*k
ans=0

last=1
for n in range(len(s)-1):


	if s[n]==s[n+1]:

		last+=1
		if last%2==0:
			ans+=1

	else:
		last=1


print(ans)

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)*(t//2)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
S = input()
K = int(input())
res = 0
if len(S) == 1:
    print(int(K/2))
    exit(-1)
if len(S) == 2:
    if S[0] == S[-1]:
        res += 1
if len(S) > 2:
    n = 0
    while n <= len(S)-2:
        if S[n] == S[n+1]:
            res += 1
            n += 1
        n += 1
    if S[0] == S[-1]:
        res += K-1
print(res*K)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import codecs
s= str(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())

Sr=S*K+"*"

count=1
result=0
for x in range(len(Sr)-1):
    if Sr[x]==Sr[x+1]:# and S[x+1]!="*":
        count+=1
    else:
        if count>1:
             result+=int(count/2)
        count=1
    #print(S[x],":",S[x+1])
    #print(count)
    #print(result)
#result*=K
print(result)

Submission status: Runtime Error

Solution code:
S = input().strip()
K = int(input())
A = []
cur = 0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        if i-cur>=2:
            A.append([S[i-1],cur,i])
        cur = i
if S[-1]==S[-2]:
    A.append([S[-1],cur,len(S)])
cnt = 0
for j in range(len(A)):
    cnt += (A[j][2]-A[j][1])//2
if S[0]!=S[-1]:
    cnt = cnt*K
else:
    if S[0]!=S[1] and S[-1]!=S[-2]:
        cnt = cnt+(cnt+1)*(K-1)
    elif S[0]==S[1] and S[-1]!=S[-2]:
        if (A[0][2]-A[0][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
    elif S[0]!=S[1] and S[-1]==S[-2]:
        if (A[-1][2]-A[-1][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
    elif S[0]==S[1] and S[-1]==S[-2]:
        if (A[0][2]-A[0][1])%2==0 or (A[-1][2]-A[-1][1])%2==0:
            cnt = cnt*K
        else:
            cnt = cnt+(cnt+1)*(K-1)
print(cnt)