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

Solution (Python):
s = list(input())
k = int(input())
lis = []
n = 1
for i in range(len(s)-1):
    if s[i] == s[i + 1]:
        n += 1
    else:
        lis.append(n)
        n = 1
lis.append(n)
if len(lis) == 1:
    print(len(s) * k // 2)
else:
    if s[0] == s[len(s) - 1]:
        if len(lis) >= 2:
            m = lis[0] // 2
            m = m + lis[len(lis)-1]//2
            m = m + (lis[0] + lis[len(lis)-1]) // 2 * (k - 1)
        if len(lis) >= 3:
            for i in range(len(lis) - 2):
                m = m + lis[i + 1] // 2 * k
        print(m)
    else:
        m = 0
        for i in range(len(lis)):
            m = m + lis[i] // 2 * k
        print(m)

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  if len(s)%2==1 and k%2==1:
    print(len(s)//2 + len(s)*(k-1)//2 )
  else:
    print(len(s)*k//2)
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))

Solution (Python):
s=input()
k=int(input())
ans=0
if len(set(list(s)))==1:
  ans=(k*len(s))//2
  print(ans)
  exit()
x=0
y=0
t=s*2
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    x+=1
    s=s[:i+1]+'0'+s[i+2:]
for i in range(len(t)-1):
  if t[i]==t[i+1]:
    y+=1
    t=t[:i+1]+'0'+t[i+2:]   
ans=x+(k-1)*(y-x)
print(ans)

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

ans = 0
tmp = 0
head = 1
hip = 1
if S[0] == S[-1]: key = True
else: key = None
itr = 0
b = 0
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        tmp += 1
    else:
        ans += (tmp+1)//2
        if (key == True) and (b == 0):
            head = tmp
            b+=1
        tmp = 0

    if (key == True) and (itr == len(S)-2):
        hip = tmp
    itr += 1
ans += (tmp+1)//2
ans = ans*K
if key==True and b==0:
    ans = len(S)*K//2
if (head%2 == 0) and (hip%2 == 0):
    ans += K-1
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline

def f(s):
    i = 0
    n = 0
    while i < len(s)-1:
        if s[i] == s[i+1]:
            n += 1
            i += 2
        else:
            i += 1
    return n

def main():
    S = input().strip()
    K = int(input())
    a1 = f(S)
    a2 = f(S*2)
    a3 = f(S*3)
    if K == 1:
        print(a1)
        return
    if K == 2:
        print(a2)
        return

    r1 = a2 - a1
    r2 = a3 - a2
    ans = a1 + r1 * (K//2) + r2 * ((K-1)//2)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
s =  input()
num = int(input())
out = 0

cha = s[0]
countf = 1
countl = 0
for i in range((len(s)-1)//2):
  if s[i+1] == cha:
    countf += 1
  else:
    break
for i in range((len(s)+1)//2):
  if s[-i-1] == cha:
    countl += 1
  else:
    break

fs = s[:-countl]
ms = s[countf:-countl]
ls = s[countf:]

if countf == 1 and countl == 0:
  fs = s
  ms = s
  ls = s

if countf != 1 and countl == 0:
  fs = s
  ms = s[countf:]
  ls = s[countf:]
  
count = 1
for i in range(len(fs)-1):
  if fs[i] == fs[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2
  
count = 1
for i in range(len(ls)-1):
  if ls[i] == ls[i+1]:
    count += 1
  else:
    out += count//2
    count = 1
out += count//2    

count = 1
tout = 0
for i in range(len(ms)-1):
  if ms[i] == ms[i+1]:
    count += 1
  else:
    tout += count//2
    count = 1
tout += count//2
out += tout*(num-2)

out += ((countf+countl)//2) * (num-1)

if s.count(s[0]) == len(s):
  out = (len(s)*num)//2

print(out)

Solution (Python):
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
a.append(0)
if len(S) == 1:
    ans = K//2
else:    
    
    if (count+1)%2==1 and a[0]%2 == 1 and S[0]== S[1] and S[0] == S[len(S)-1]:
    
        ans+=K-1
    if S[0]!= S[1] and (count+1)%2 ==1 and  S[0] == S[len(S)-1]:
        ans+=K-1
    if count+1 == len(S):
        if len(S)%2==1:
            ans = (len(S)*K)//2
 
print(ans)

Solution (Python):
import copy

S = input()
K = int(input())
S_list = list(S)

if len(S_list) == 1:
    print(K//2)
else:
    for i in range(1, len(S_list)):
        if S_list[i-1] == S_list[i]:
            S_list[i] = "change"
    S1_count = S_list.count("change")

    if S_list[0] == S_list[-1]:
        S2_list = list(S)
        S2_list[0] = "change"
        for i in range(1, len(S2_list)):
            if S2_list[i-1] == S2_list[i]:
                S2_list[i] = "change"
        S2_count = S2_list.count("change")
        if S2_list[-1] == S_list[0]:
            print(S1_count + (K-1)*S2_count)
        else:
            print((((K//2)+1)*S1_count) + ((K//2)*S2_count))
    else:
        print(K * S1_count)

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

import re

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

if len(S)==1:
  print(K//2)
  
elif S[0]!=S[-1]:
  S_l = list(nchars(S,2))
  answer = 0
  for s in S_l:
    answer += len(s)//2
  print(answer*K)

else:
  if (S[0]!=S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2

    answer = S_lr + (1+S_lr)*(K-1)
    print(answer)
  
  elif (S[0]!=S[1])&(S[-1]==S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[-1])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
  
  elif (S[0]==S[1])&(S[-1]!=S[-2]):
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    for s in S_l:
      S_lr += len(s)//2
    
    a=len(S_l[0])
    answer = S_lr + ((a+1)//2 + (S_lr-a//2))*(K-1)
    print(answer)
    
  else:
    answer = 0
    S_lr=0
    S_l = list(nchars(S,2))
    if len(S_l)==1:
      print(len(S)*K//2)
    else:
      for s in S_l:
        S_lr += len(s)//2

      a=len(S_l[0])
      b=len(S_l[-1])
      answer = S_lr + ((a+b)//2 + (S_lr-a//2-b//2))*(K-1)
      print(answer)

Solution (Python):

s = input()
check = False
if len(set(s)) == 1:
    check = True
n = len(s)
K = int(input())
s = s*2
s = list(s)
cnt = 0
for i in range(1, len(s)):
    if s[i] == s[i-1]:
        s[i] = 'A'
        cnt += 1
res = 0
if cnt % 2 == 1:
    res = (cnt//2)*K + K-1
else:
    res = cnt//2 * K
if check:
    res = n * K // 2
if n == 1:
    res = K//2
print(res)

Solution (Python):
S = input().replace(" ", "")
K = int(input())

str1 = S
str2 = S * 2
str3 = S * 3
str4 = S * 4
n1 = 0
n2 = 0
n3 = 0
n4 = 0
ln1 = []
ln2 = []
ln3 = []
ln4 = []

for c in str1:
  ln1.append(c)
for i in range(len(ln1)):
  if i == 0:
    pass
  elif ln1[i-1] == ln1[i]:
    n1 += 1
    ln1[i] = "A"
    
for c in str2:
  ln2.append(c)
for i in range(len(ln2)):
  if i == 0:
    pass
  elif ln2[i-1] == ln2[i]:
    n2 += 1
    ln2[i] = "A"
    
for c in str3:
  ln3.append(c)
for i in range(len(ln3)):
  if i == 0:
    pass
  elif ln3[i-1] == ln3[i]:
    n3 += 1
    ln3[i] = "A"
    
for c in str4:
  ln4.append(c)
for i in range(len(ln4)):
  if i == 0:
    pass
  elif ln4[i-1] == ln4[i]:
    n4 += 1
    ln4[i] = "A"
  
if len(S) == 1:
  print(K//2)
elif n1 == (n2 - n1) and (n2 - n1) == (n3 - n2):
  print(int(n1 * K))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2) and n1 == (n4 - n3):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2 - 1))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
elif n1 != (n2 - n1) and (n2 - n1) == (n3 - n2):
  if K >= 3:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  elif K == 2:
    print(int(n2))
  else:
    print(int(n1))
else:
  if K % 2 == 1:
    print(int(n1 + (n3 - n1) * (K - 1)//2))
  else:
    print(int(n2 * K / 2))

Solution (Python):
T = input()

K = int(input())

S_ = T*2
N = len(T)
last = T[-1]
mod = 10**9+7
"""
後ろを前につなげて、一番長くなるのはいくつ？
cooooooooonteeeeeeeeeest
3
"""
def check(S):
    S += "1"
    cnt = 1
    pre = "-1"
    flg = False
    ret = 0
    for i, s in enumerate(S):
        if pre == s:
            cnt += 1
            flg = True
        else:
            if flg:
                ret += cnt // 2
                flg = False
            cnt = 1
        pre = s
    return ret

s = 0
for i in range(N):
    if last != T[i]:
        s = i
        break
e = 0 
for i in range(N):
    if last != T[-i]:
        e = -i
        break

if K == 1:
    print(check(T))
    exit()

if len(set(T)) == 1:
    ans = check(T*2)*(K//2)+check(T)*(K%2)
    print(ans)
    exit()

if s == e:
    print(check(T)*K)
    exit()



pre, mid, end = T[:s], S_[s:s+N], S_[s+N:]



a = check(pre)    
b = check(mid)    
c = check(end)

print(a+b*(K-1)+c)

Solution (Python):
S=list(input())
k=int(input())

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

if len(S)==1:
  print(k//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(k)
  else:
    print(0)
  exit()
  
if len(set(T))==1:
  if len(S)%2==0:
    print(len(S)//2*k)
  else:
    out=(len(S)//2)*(k-(k//2))+(len(S)//2+1)*(k//2)
    print(out)
  exit()
      
print(tmp+(ans-tmp)*(k-1))

Solution (Python):
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
if q[-1]%2==0:
	cnt=True
else:
	cnt=False
ans=0
if cnt==True:
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)
	exit()
if len(q)==1:
	print(len(s)*k//2)
	exit()
for i in range(len(q)-2):
	ans+=q[i+1]//2
ans*=k
ans+=q[0]//2
if s[0]==s[-1]:
	ans+=(q[0]+q[-1])//2*(k-1)
	ans+=q[-1]//2
	print(ans)
	exit()
else:
	ans=0
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

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


ans=0

if len(s)==1:
	ans=(k)//2
	print(ans)
	exit(0)

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)





from itertools import groupby
m=[]
for key, g in groupby(s):
	
	m.append([key,len(list(g))])

p(m)

if len(m)==1:
	i=m[0][1]
	ans+=((i*k)//2)
	print(ans)
	exit(0)
		

for a,i in m:
	
	ans+=(i//2)*k


if m[0][0]==m[-1][0]:
	if m[0][1]%2==1:
		if m[-1][1]%2==1:
			ans+=(k-1)

print(ans)

Solution (Python):
s = input()
k = int(input())
A = []
b = 1
if s[0] != s[-1]:
  for i in range(len(s)-1):
    if s[i] == s[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  print(k*sum(a//2 for a in A))
elif all(a  == s[0] for a in s):
  print((k*(len(s))//2))
else:
  t = s[0]
  ahead = 0
  back = 0
  ans = 0
  for i in range(len(s)):
    if s[i] == t:
      ahead += 1
    else:
      break
  for i in reversed(range(len(s))):
    if s[i] == t:
      back += 1
    else:
      break
  ans += ahead//2 + back//2 + (k-1)*((ahead+back)//2)
  l = s[ahead:-back]
  for i in range(len(l)-1):
    if l[i] == l[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  ans += (k*sum(a//2 for a in A))
  print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
 
s = str(input()).replace('\n', '')
k = int(input())
 
s = s + s[0]
 
lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)
 
    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k

if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2
elif s[0] == s[-2] and lst[-1]%2 == 0 and lst[0]%2 == 0:
  answer -= k-1

print(answer)

Solution (Python):
S=list(str(input()))
s=set(S)
K=int(input())
num=0
i=0
lst=[]
def dp(i,lst):
  if i==len(S)-1:
    return lst
  if S[i]!=S[i+1]:
    return dp(i+1,lst)
  j=i+1
  while j<=len(S)-1  and S[i]==S[j]:
    j+=1
  lst.append(list(S[i:j]))
  return dp(j-1,lst)

if 2<=len(s):
  L=dp(i,lst)
  for i in range(len(L)):
    num+=(len(L[i])//2)
  if num==0:
    if S[0]!=S[-1]:
      print(0)
    else:
      print(K-1)
  elif L[-1][-1]==S[0] and len(L[-1])%2==len(L[0])%2==1:
    print(num+(K-1)*(num+1))
  else:
    print(num*K)
else:
  print((len(S)*K)//2)

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

count = 0
i=0
join_flag =0
join_n = 0

if len(s)==1:
  print(k//2)
    
elif len(s)==2:
  if s[0]==s[1]:
    print(k)
  else:
    print(0)
  
elif len(set(s))==1:
  print(len(s)*k //2)
  
else:
  while i<len(s)-1:
    if s[i] == s[i+1]:
      count+=1
      i+=2
    else:
      i+=1
  
  if (s[-1] == s[0]) :
    join_flag =1
    
  if join_flag:      
    left_len = 1
    right_len = 1

    for i in range(len(s)-1):
      if s[i] == s[i+1]:
        left_len +=1
      else:
        break

    for i in range(len(s)-1):    
      if s[-1-i]==s[-2-i]:
        right_len +=1
      else:
        break            
    
    if left_len %2 ==1 and right_len %2 ==1:
      join_n = k-1
    else:
      join_n =0
    
  print(count*k + join_n)

Solution (Python):

s = list(input())
t = int(input())
answer=0

def classify(s):
    ans=0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = '0'
            ans +=1
    return ans

if t == 1:
    print(classify(s))
else:
    s2 = s*2
    s21 = s*2 + list(s[0])
    t2 = t//2
    t2a = t%2
    c1 = classify(s)
    c2 = classify(s2)
    c21 = classify(s21)
    
    if c1*2 == c2:
        answer = c1 * t
    elif c2 == c21:
        
        answer = c2 * t2 + c1 * t2a
    else:
        answer = c1 * t + t - 1
         
    print(answer)

Solution (Python):
s = input()
k = int(input())
cnt = 0
cntw = 0
cntwflg = False
i = 0

if len(s) == 1:
    print(k//2)
    exit()

if len(list(set(s))) == 1:
    print((k*len(s))//2)
    exit()
while True:
    
    if len(s)-1 < i:
        break
    if len(s)-1 == i:
        if s[i] == s[0]:
            cntw = 1
            if s[i-1]==s[i-2]:
                j = 0
                while s[j] == s[j+1]:
                    if j%2 == 0:
                        cntw = 0
                    else:
                        cntw = 1
                    j += 1
    elif s[i] == s[i+1]:
        cnt+= 1
        i += 1
    i += 1


ans = cnt*k+cntw*(k-1)
print(ans)

Solution (Python):
import math

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


before = ""
count = 0
if len(S) == 1:
    out = K // 2
else:
    for s in S:
        if before == s:
            count += 1
            before = ""
        else:
            before = s
    
    
    if before == S[0]:
        first_same_num = 1
        before2 = S[0]
        for s in S[1:]:
            if before2 == s:
                first_same_num += 1
            else:
                break
        if first_same_num % 2 == 0:
            first_last_replace = 0      
        else:
            if first_same_num != len(S):
                first_last_replace = K - 1
            else:
                first_last_replace = math.ceil((K - 1) / 2)
    else:
        first_last_replace = 0

    out = count * K + first_last_replace

print(out)

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

if len(S) == 1:
    print(K // 2)
    exit()
    
cnt, s, l, r = 1, S[0], 0, len(S)-1
while l + 1 <= r and S[l+1] == s:
    cnt += 1
    l += 1
while l <= r - 1 and S[r] == s:
    cnt += 1
    r -= 1
if cnt == len(S):
    print(len(S) * K // 2)
    exit()
changed = [False] * len(S)
for i in range(1, len(S)):
    if i > 1 and S[i] == S[i-1] == S[i-2] and changed[i-1]:
        continue
    elif S[i] == S[i-1]:
        changed[i] = True
ans = sum(changed)
res = ans * K
if changed[-1] or S[0] != S[-1] or cnt & 1:
    print(res)
    exit()
res += K - 1
print(res)

Solution (Python):
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[1] == s[len(s) // 3]:
            n += 1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

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

if len(s) == 1:
    print(k // 2)
    exit()

if s[0] != s[-1]:
    count = 1
    box = []
    for i in range(len(s)-1):
        if s[i+1] == s[i]:
            count += 1
        else:
            box.append(count)
            count = 1
    box.append(count)

    ans = 0
    for b in box:
        ans += b // 2
    print(ans * k)
    exit()

flag = 0
word = s[0]
for si in s:
    if si != word:
        flag = 1

if flag == 0:
    print(len(s) * k // 2)
    exit()

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

    ans = 0
    for b in box:
        ans += b // 2
    print(ans)
    exit()


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

front = box[0] // 2
back = box[-1] // 2

mid = 0
for i in range(len(box)-1):
    if i == 0:
        continue
    mid += box[i] // 2

joint = (box[0] + box[-1]) // 2

print(front + back + mid * k + joint * (k-1))

Solution (Python):
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[len(s) // 3] ==s[1]:
            n += 1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Solution (Python):
a=input()
b=int(input())
s=""
zn=0
n=0
nn=0
i=0
aa=[]
ab=[]
abb=0
abc=0

for x in a:
    if aa==[]:
        n+=1
    elif x==s:
        n+=1
    elif x!=s:
        ab.append(n)
        n=1
    s=x
    i+=1
    aa.append(x)
    if i==len(a):
        ab.append(n)
if aa[0]==aa[-1] and len(ab)>2:
    if ab[-1]!=1:
        abb=ab[-1]
        del ab[-1]
    if ab[0]!=1:
        abc=ab[0]
        del ab[0]
    s=aa[0]
    i=0
    for x in aa:
        if aa[i]==s:
            nn+=1
        else:
            break
        i+=1
    i=1
    for x in aa:
        if aa[-i]==s:
            nn+=1
        else:
            break
        i+=1    
for x in ab:
    zn+=(x//2)
if len(a)==1:
    print(b//2)
elif len(ab)==1 and abb==0 and abc==0:
    print((b*ab[0])//2)
else:
    print(zn*b+(nn//2)*(b-1)+abb//2+abc//2)

Solution (Python):
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0] == s[-1] == s[-2]== s[-3]== s[-4]== s[-5]== s[len(s)//3] \
                and s[0] == s[1] == s[2]== s[3]== s[4]== s[5]:
            n+=1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n*(k-1))

Solution (Python):
from itertools import groupby

S = input()
K = int(input())
A = []
for s in S:
  A.append(s)

def cont(A):
  B = []
  b = 0
  for key, value in groupby(A):
    B.append([key, list(value)])
  for i in range(len(B)):
    b += len(B[i][1]) // 2
  return b

def plus1(A):
  plu1 = 0
  for i in range(1,len(A)):
    if A[0] == A[i]:
      plu1 += 1
    else:
      break
  return plu1

def plus2(A):
  plu2 = 0
  for j in range(1,len(A)):
    if A[0] == A[-j]:
      plu2 += 1
    else:
      break
  return plu2

X = len(S) - len(list(set(A)))
if len(list(set(A))) == 1:
  an = len(S)*K
  ans = an // 2
elif X == 0:
  ans = 0
elif A[0] != A[-1]:
  ans = cont(A) * K
else:
  if plus1(A)%2 == 0 and plus2(A)%2 == 1:
    ans = cont(A)*K + (K-1)
  else:
    ans = cont(A) * K
print(ans)

Solution (Python):
S=list(input())
K=int(input())
c=0
f=1
if len(S) == 1:
    ans=K//2
else:
    for a in range(len(S)-1):
        if S[a] == S[a+1]:
            c+=1
            if a != len(S)-2 and S[a+1] == S[a+2]:
                f+=1
            else:
                c-=f//2
                f=1
    ans=c*K
    m=1
    u=1
    if S[0] == S[len(S)-1]:
        for i in range(len(S)-1):
            if S[i]==S[i+1]:
                m+=1
            else:
                break
        for i in reversed(range(len(S)-1)):
            if S[i]==S[i+1]:
                u+=1
            else:
                break
        if m%2 == 1 and u%2==1:
            if m == len(S):
                ans+=K//2
            else:
                ans+=K-1
print(ans)

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


if len(set(s)) == 1:
    print(len(s) * k // 2)

else:
    count_l = []
    before_mozi = ''

    for mozi in s:
        if before_mozi == mozi:
            count_l[-1] += 1
        else:
            count_l.append(1)
        before_mozi = mozi

    
    if s[0] == s[-1]:
        result = sum([x//2 for x in count_l[1:-1]]) * k + count_l[0]//2 + count_l[-1]//2 + (count_l[0] + count_l[-1])//2 * (k-1)

    else:
        result = sum([x//2 for x in count_l]) * k

    print(result)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)


def main():
    s = input()
    k = int(input())

    if len(s) == 1:
        print(k//2)
        exit()

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

    cnt2 = 1
    ans2 = 0
    s2 = s*2
    for i in range(len(s2)-1):
        if s2[i] == s2[i+1]:
            cnt2 += 1
        else:
            ans2 += cnt2 // 2
            cnt2 = 1
    ans2 += cnt2 // 2

    if s.count(s[0]) == len(s) and len(s) % 2 != 0:
        if k % 2 == 0:
            print(ans2*k//2)
        else:
            print(ans2*(k-1)//2+ans)
        exit()

    if ans * 2 != ans2:
        print(ans*k+k-1)
        exit()

    print(ans*k)


if __name__ == '__main__':
    main()

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

import math

C = 0
if S[0]==S[-1]:
    C = 1
if all([True if S[0]==S[i] else False for i in range(len(S))]):
    C = 2

f = '!'
count = 0
if C == 0:
    
    for i in range(len(S)):
        if S[i]==f:
            count += 1
            f = '!'
        else:
            f = S[i]
    print(K*count)
elif C==1:
    
    count = 0
    
    Fflag = 0
    for i in range(len(S)):
        if S[i]==S[0]:
            Fflag += 1
        else:
            break
    Bflag = 0
    for j in range(len(S))[::-1]:
        if S[j]==S[-1]:
            Bflag += 1
        else:
            break
    f = '!'
    for k in range(Fflag,len(S)-Bflag):
        if S[k]==f:
            count += 1
            f = '!'
        else:
            f = S[k]
    print(math.floor(Fflag/2) + math.floor(Bflag/2)+ count*K + math.floor((Fflag+Bflag)/2)*(K-1))
elif C == 2:
    print(math.floor(len(S)*K/2))

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


if len(set(s)) == 1:
    print(len(s) * k // 2)

else:
    count_l = []
    before_mozi = ''

    for mozi in s:
        if before_mozi == mozi:
            count_l[-1] += 1
        else:
            count_l.append(1)
        before_mozi = mozi

    
    if s[0] == s[-1]:
        result = sum([x//2 for x in count_l[1:-1]]) * k + count_l[0]//2 + count_l[-1]//2 + (count_l[0] + count_l[-1])//2 * (k-1)

    else:
        result = sum([x//2 for x in count_l]) * k

    print(result)

Solution (Python):
slist=list(input())
k=int(input())
n=len(slist)
kaisu=0
i=1
shirushi=0
kotae=0
rouge=0
flag=0

for l in range(1,n):
	if slist[l]==slist[l-1]:
		rouge+=1
	else:
		break
		
if rouge==n-1:
	flag=1

if n>1 and flag==0:

	while 1<=i<=n-1:
		if slist[i]==slist[i-1]:
			kaisu+=1
			if i==n-1:
				shirushi=1
			i+=2
		else:
			i+=1
	
	if k>1:
		if shirushi==0 and slist[0]==slist[n-1]: 
			if slist[0]==slist[1]: 
				yabaikaisu=1
				i=2
				shirushi=0
			
				while 1<=i<=n-1:
					if slist[i]==slist[i-1]:
						yabaikaisu+=1
						if i==n-1:
							shirushi=1
						i+=2
					else:
						i+=1
					
				kotae=kaisu+(k-1)*yabaikaisu
			else: 
				kotae=k+kaisu*k-1
	
		else: 
			kotae=kaisu*k
			
	else: 
		kotae=kaisu

elif flag==1:
	kosuu=n*k
	if kosuu%2==0:
		kotae=kosuu/2
	else:
		kotae=(kosuu-1)/2

else: 
	if k%2==0:
		kotae=k/2
	else:
		kotae=(k-1)/2

print(int(kotae))

Solution (Python):
from itertools import groupby
s=input()
k=int(input())
l=len(s)
g=groupby(s)
a=[]
for i,j in g:
  a.append(len(list(j)))
if len(a)==1:
  x=len(s)*k
  if x%2==0:
    print(x//2)
  else:
    print((x-1)//2)
else:
  c=0
  for i in a:
    if i%2==0:
      t=i//2
    else:
      t=(i-1)//2
    c+=t
  if s[0]==s[l-1] and k!=1:
    a[0]=a[0]+a[len(a)-1]
    a=a[:len(a)-1]
    z=0
    for i in a:
      if i%2==0:
        t=i//2
      else:
        t=(i-1)//2
      z+=t
    print(z*(k-1)+c)
  else:
    print(c*k)

Solution (Python):
s = str(input())
k = int(input())

def func(s, k):
    ans = 0
    n = len(s)
    s_list = list(s)
    for i in range(n - 1):
        if s_list[i] == s_list[i + 1]:
            ans += 1
            s_list[i + 1] = str(i + 1)
    ans *= k
    if s_list[0] == s_list[n - 1]:
        ans += k - 1
    return ans

ans1 = func(s, k)
s_inv = ''.join(list(reversed(s)))
ans2 = func(s_inv, k)

if len(s) % 2 == 1:
    check = True
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            pass
        else:
            check = False
            break
    if check:
        ans3 = len(s) * k // 2
    else:
        ans3 = ans1
else:
    ans3 = ans1

print(min(ans1, ans2, ans3))

Solution (Python):
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
    else:
        if cnt != 1:
            
            ans += (cnt//2)
            cnt = 1
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break

ans = ans*K

if S[0] == S[-1]:
    ans+=((K-1)*((rev_eq+for_eq)//2))
    ans-=((rev_eq//2)*(K-1))
    ans-=((for_eq//2)*(K-1))
    """
    if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
        ans += ((K+1)//2)
    """
if len(set(S))==1:
    ans = (len(S)*K)//2
print(ans)

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

ans = 0
now = 0
flag = 1
onaji = False
kazu = 1

if len(s) == 1:
    print(int(k//2))
    exit()

for i in range(len(s)):
    
    if now >= len(s) - 1:
        break
    if s[now] == s[now+1]:
       ans += 1
       if now+1 == len(s) -1:
           flag = 0
       now += 2
    else:
        now += 1

if len(s) == 2:
    print(int(ans*k))
    exit()

for i in range(len(s)-1):
    if s[i] != s[i+1]:
        break
    kazu += 1
    if i == len(s) - 2:
        onaji = True

if flag == 1:
    if s[0] == s[-1]:
        if s[0] == s[1]:
            flag = 2
        else:
            flag = 1
    else:
        flag = 0


if flag != 2:
    print(int(ans*k+flag*(k-1)))
else:
    if onaji == True:
        print(int(ans*k+k//2))
    else:
        if kazu % 2 == 0:
            print(int(ans*k))
        else:
            print(int(ans*k+1*(k-1)))

Solution (Python):
import re
import math

def f(S):
    n = len(S)
    if n == 0 or n == 1:
        return 0
    elif n == 2:
        return 1 if S[0] == S[1] else 0
    else:
        if S[-1] == S[-2] and S[-2] == S[-3]:
            return f(S[0:-2]) + 1
        elif S[-1] == S[-2] and S[-2] != S[-3]:
            return f(S[0:-1]) + 1
        else:
            return f(S[0:-1])

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

reg_exp = S[0] + '+'
l = len( re.match(reg_exp, S).group() ) 

reg_exp = S[-1] + '+$'
r = len( re.findall(reg_exp, S)[0] ) 

if len(S) == l: 
    result = math.floor(len(S) * K / 2)
elif S[0] == S[-1] and l % 2 == 1 and r % 2 == 1:
    result = K * f(S) + K - 1
else:
    result = K * f(S)

print(result)

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

prev = ""
count1 = 0
count2 = 0
count3 = 0
flag = 0
for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count1 += 1
        flag = 1
    prev = i



for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count2 += 1
        flag = 1
    prev = i

for i in s:
    if flag == 1:
        prev = i
        flag = 0
        continue
    if prev == i:
        count3 += 1
        flag = 1
    prev = i

if k % 2 == 1:
    ret = count1 + (k-1) // 2 * (count2 + count3)
else:
    ret = count1 + ((k-1)//2+1) * count2 + (k-1)//2 * count3

print(ret)

Solution (Python):
S = str(input())
K = int(input())
tmp = 1
cnt1 = 0
cnt2 = 0
for i in range(1, len(S)):
    if S[i-1] == S[i]:
        tmp += 1
    else:
        cnt1 += tmp // 2
        tmp = 1
if tmp == len(S):
        print(len(S) * K // 2)
        exit()
if tmp > 1:
    cnt1 += tmp//2
S2 = S + S
tmp = 1
for i in range(1, len(S2)):
    if S2[i-1] == S2[i]:
        tmp += 1
    else:
        cnt2 += tmp // 2
        tmp = 1
if tmp > 1:
    cnt2 += tmp//2
if K == 1:
    print(cnt1)
else:
    print((cnt2 - cnt1) * (K-2) + cnt2)