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


import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if K == 1:
        return simple_count(S)[0]
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S * 2)
        
        if changed:
            return count2 * (K // 2) + simple_count(S, changed=False)[0] * (K % 2)
        else:
            return (count2) * (K // 2) + 1 * (K // 2-1 + K % 2) + simple_count(S, changed=True)[0] * (K % 2)
        


def test(S, K):
    SS = "".join([c for c in reversed(S)])
    
    assert solve(S, K) == simple_count(S * K)[0], "{} {} = {} {}".format(S, K, solve(S, K), simple_count(S * K)[0])


assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
test("qq", 81)

assert solve("qqq", 2) == simple_count("qqq" * 2)[0]

assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]

test("iqi", 3)
test("iqqqqiiii", 1)
test("iqi", 2)
test("iqqqqiiii", 2)
test("iqii", 3)
test("iqii", 4)
test("iqqqqiiii", 6)
test("iqqqqiiii", 7)
test("qqqqqiiiii", 7)
test("q", 7)
test("qiq", 1)
test("qiq", 3)
test("qiq", 7)
test("qqq", 7)
test("qq", 7)
test("abcdef", 7)
test("qbbbq", 7)
test("qqqq", 7)
test("qqqqq", 7)

S = sys.stdin.readline().rstrip()
K, = map(int, sys.stdin.readline().rstrip().split(" "))
SS = "".join([c for c in reversed(S)])
print(min(solve(S, K), solve(SS, K)))

exit(0)

Solution (Python):
s=input()
k=int(input())
p=0
if s[0]==s[-1]:
  p+=1

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

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

flag = False

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

if sum(len_cnt) != len(S) :
  len_cnt.append(1)

if len(len_cnt) == 1 :
  print(int(len_cnt[0] * K / 2))
  exit()

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] % 2 == 1 and len_cnt[0] % 2 == 1 :
  ans += 1
  flag = True

ANS = K * ans

if flag :
  ANS -= 1



print(ANS)

Solution (Python):



from collections import Counter
import string


def main():
    
        S = input()
        K = int(input())
        S_MAP = Counter(S)

        count_1st = 0
        S1 = list(S * 1)
        for i in range(len(S) - 1):
            if S1[i] == S1[i+1]:
                count_1st += 1
                S1[i+1] = '_'
                
                
                
                
                
                
                
                
                
                
                

        count_2nd = 0
        S2 = list(S * 2)
        for i in range(len(S) * 2 - 1):
            if S2[i] == S2[i+1]:
                count_2nd += 1
                S2[i+1] = '_'

        count_3rd = 0
        S3 = list(S * 3)
        for i in range(len(S) * 3 - 1):
            if S3[i] == S3[i+1]:
                count_3rd += 1
                S3[i+1] = '_'

        count_4th = 0
        S4 = list(S * 4)
        for i in range(len(S) * 4 - 1):
            if S4[i] == S4[i+1]:
                count_4th += 1
                S4[i+1] = '_'

        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
        if len(S_MAP) == 1 and len(S) % 2 == 1:
            d1 = count_2nd - count_1st
            d2 = count_3rd - count_2nd
            
            
            print(d1 * K//2 + d2 * K//2)
        
        else:
            d = count_3rd - count_2nd
            
            print(count_1st + d * (K-1))
            
            
            
            
            
        
        
        
        


if __name__ == '__main__':
    main()

Solution (Python):
s=list(input())
K=int(input())
S=s.copy()

if len(S)==1:
  print(K//2)
  exit()

pc=0
ju=0
for i in range(len(S)):
  if S[i]==S[0]:
    pc+=1
    if i==len(S)-1:
      ju=1
  else:
    break

lc=0
if ju==0 and S[0]==S[-1]:
  for i in reversed(range(len(S))):
    if S[i]==S[0]:
      lc+=1
    else:
      break
  
if S[0]==S[1]:
  S[0]="
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    S[i]="

ans=S.count("
if pc%2==1 and pc>1:
  ans-=1
  
  
if ju==1 and len(S)%2==1:
  print((len(S)*K)//2)
  exit()
  
if s[-1]==s[0] and s[1]!=s[0] and s[-2]!=s[0]:
  print(K*ans+(K-1))
  exit()


if S[0]=="
  print(ans*K+K-1)
  exit()


print(ans*K)

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    r = sum(x // 2 for x in a) * k
    if s[0] == s[-1] and a[0] & a[-1] & 1:
        r += k - 1
    print(r)

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

    c = []
    
    i = 0
    if S[0] == S[-1]:
        connected = True
    else:
        connected = False

    while i < len(S):

        char = S[i]

        cnt = 1
        cnt2 = 1
        for j in range(i + 1, len(S) * 2):
            i = j
            if char == S[j % len(S)]:
                if j >= len(S):
                    cnt2 += 1
                else:
                    cnt += 1
                    cnt2 +=1
            else:
                if j-1 < len(S):
                    cnt2 = 0
                    i = j
                else:
                    i = len(S)
                break
        
        if cnt >= 2:
            c.append([char, cnt, cnt2])
        else:
            c.append([char, 0, cnt2])
    
    

    ans = 0
    flg = True
    for elem in c:
        if connected:
            if flg and len(c) >= 2:
                flg = False
                continue
            if len(c) == 1:
                ans = elem[1] * K // 2 
            elif elem[2] >= 2:
                ans += (elem[2] // 2) * (K - 1)  + (elem[1] // 2) + c[0][1] // 2
            else:
                ans += (elem[1] // 2) * K

        else:
            ans += (elem[1] // 2) * K
    
    if len(S) == 1:
        ans = K // 2
        
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):
s=list(input())
ss=s[:]
k=int(input())
if len(s)==1: 
    print(k//2)
elif len(set(ss))==1:
        print((k*len(s))//2)    
else:
    count1=0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            s[i+1]='0'
            count1+=1
    count2=0
    ss=ss+ss
    for i in range(len(ss)-1):
        if ss[i]==ss[i+1]:
            ss[i+1]='0'
            count2+=1
    print(count1+(k-1)*(count2-count1))

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







if len(S) == 1:
    print(K//2)
    exit()


cnt = 0
ruisekiwa = [1]
now_s = S[0]
for i in range(1, len(S)):
    if now_s == S[i]:
        ruisekiwa[-1] += 1
    else:
        ruisekiwa.append(1)
    now_s = S[i]

if len(ruisekiwa) == 1:
    if ruisekiwa[0] % 2 == 0:
        print(ruisekiwa[0]//2*K)
        exit()

    
    if K % 2 == 0:
        cnt += (ruisekiwa[0]//2 + 1) * (K//2)
        cnt += (ruisekiwa[0]//2) * (K//2)
    else:
        cnt += (ruisekiwa[0]//2) * (K//2 + 1)
        cnt += (ruisekiwa[0]//2 + 1) * (K//2)
    print(cnt)
    exit()



cnt = 0
for i in range(len(ruisekiwa)):
    cnt += ruisekiwa[i]//2


if S[0] != S[-1]:
    print(cnt*K)
    exit()







if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()

if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 1:
    print(cnt * K)
    exit()


if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()




if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 1:
    
    
    
    
    
    print((cnt+1) * K -1)
    exit()

Solution (Python):
def p(st):
    l=len(st)
    tc=1
    ac=0
    p=s[0]
    for i in range(1,l):
        if st[i]==p:
            tc+=1
        else:
            ac+=tc//2
            tc=1
            p=st[i]
    else:
        ac+=tc//2
    return ac

s=input()
k=int(input())
n=len(s)
if n==1:
    print(k//2)
    exit()
if n==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
    exit()
ac=p(s)
if k>1:
    ac4=p(s+s+s+s)
    if ac*4==ac4:
        print(ac*k)
    elif ac4-ac*4==3:
        print(ac*k+(k-1))
    elif ac4-ac*4==2:
        print(ac*k+(k//2))
else:
    print(ac)

Solution (Python):
s = list(x for x in input())
s2 = s + s
s_num = s.count(s[0])
count = 0
count2 = 0
k = int(input())

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = 0
    count += 1
for i in range(len(s2)-1):
  if s2[i] == s2[i+1]:
    s2[i+1] = 0
    count2 += 1

if len(s) == 1:
  print(k//2)
elif s_num == len(s):
  if k%2 == 0:
    print(count2*(k//2))
  else:
    print(count2*(k//2)+count)
else:
  print((count2-count)*(k-1) + count)

Solution (Python):
s = list(input())
k = int(input())
ln = len(s)
if ln == 1:
    ans = k//2
else:
    i = 0
    j = 0
    m = []
    for i in range(ln-1):
        if s[i] == s[i+1]:
            j += 1
        else:
            if j > 0:
                m.append(int(j+1))
                j = 0
    if j > 0:
        m.append(int(j+1))
    if j == ln-1:
        ans = (ln * k) // 2
    else:
        if m == []:
            if s[0] != s[ln-1]:
                ans = 0
            else:
                ans = k-1
        else:
            if k == 1:
                m = list(map(lambda x: x//2, m))
                ans = sum(m)*k
            else:
                if s[0] != s[ln-1]:
                    m = list(map(lambda x: x//2, m))
                    ans = sum(m)*k
                else:
                    if s[0] == s[1] and s[ln-2] == s[ln-1]:
                        ter = (m[0]+m[len(m)-1]) // 2
                        ter_sum = m[0] // 2 + m[len(m)-1] // 2 + ter * (k-1)
                        m = list(map(lambda x: x//2, m[1:len(m)-1]))
                        ans = k * sum(m) + ter_sum
                    elif s[0] == s[1]:
                        ter = (m[0]+1) // 2
                        ter_sum = m[0] // 2  + ter * (k-1)
                        m = list(map(lambda x: x//2, m[1:len(m)]))
                        ans = k * sum(m) + ter_sum
                    elif s[ln-2] == s[ln-1]:
                        ter = (1+m[len(m)-1]) // 2
                        ter_sum = m[len(m)-1] // 2 + ter * (k-1)
                        m = list(map(lambda x: x//2, m[0:len(m)-1]))
                        ans = k * sum(m) + ter_sum
                    else:
                        m = list(map(lambda x: x//2, m))
                        ans = k * sum(m) + k - 1
print(ans)

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

L = len(S)

if L == 1:
    print(K//2)
    exit()

if len(set(S)) == 1:
    print((L*K)//2)
    exit()

L = len(S)
acc = [0] * L
tmp = 0
cnt1 = 0
for i in range(1, L):
    if S[i - 1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt1 += 1
    else:
        tmp = 0
    acc[i] = tmp

if K == 1:
    print(cnt1*K)


S += S
L = len(S)
acc = [0] * L
tmp = 0
cnt2 = 0

for i in range(1, L):
    if S[i - 1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt2 += 1
    else:
        tmp = 0
    acc[i] = tmp

diff = cnt2 - cnt1

print(cnt1 + diff*(K-1))

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    r = sum(x // 2 for x in a) * k
    if s[0] == s[-1]:
        r += (a[0] & a[-1] & 1) * (k - 1)
    print(r)

Solution (Python):
s = list(map(str, input()))
k = int(input())
basic = 0
repeat = 0
counter = 0
s_a = s*2
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        counter += 1
        basic += (counter % 2)
    else:
        counter = 0
if counter == len(s)-1:
    print(len(s)*k // 2)
else:
  counter = 0
  for i in range(1,len(s_a)):
    if s_a[i] == s_a[i-1]:
        counter += 1
        repeat += (counter % 2)
    else:
        counter = 0
  repeat -= basic
  print(basic + repeat * (k - 1))

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


if s.count(s[0]) == len(s):
  print((len(s) * k) // 2)
  sys.exit()

c_c = 0
c_e = 0
counter = 1

for i in range(-1, -len(s)-1, -1):

  if s[0] == s[i]:
    counter += 1
  else:
    break
if counter % 2 == 0:
  c_e = 1



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


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



ans = (c_c + c_e) * (k-1) + c_f
print(ans)

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

import random
import string


def count(string):
    total = 0
    i = 1
    while i < len(string):
        if string[i-1] == string[i]:
            total += 1
            i += 1
        i += 1
    return total

if S == len(S) * S[0]:
    res = (K * len(S)) // 2      
elif S[0] == S[-1]:
    new = S.strip(S[0])
    start = len(S) - len(S.lstrip(S[0]))
    end = len(S) - len(S.rstrip(S[0]))
    res = start // 2 + end // 2 + K * count(new) + (K - 1) * ((start + end) // 2)
else:
    res = K * count(S)

print(res)

Solution (Python):
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0

if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)
    ans += math.ceil(count/2)
    ans += math.ceil(firstCount/2)
  else:
    ans += math.ceil(count/2)*K
    ans += math.ceil(firstCount/2)*K
else:
  ans += (count+1)*K//2
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K//2))
else:
  print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
  s = ns() + "0"
  k = ni()
  n = len(s) - 1

  if n * s[0] == s[:-1]:
    print((n * k) // 2)
    return

  isSame = True if s[0] == s[-2] else False

  cnt = 1
  w = []
  w_cnt = []
  pre = s[0]
  ans = 0
  tmp = 0
  for i in range(1, n + 1):
    if pre == s[i]:
      cnt += 1
    else:
      w_cnt.append(cnt)
      tmp += cnt // 2
      cnt = 1
      w.append(pre)
      pre = s[i]

  if isSame:
    joins = (w_cnt[0] + w_cnt[-1]) // 2
    ans = 0
    for c in w_cnt[1:-1]:
      ans += c // 2
    print(ans * k + joins * (k - 1) + w_cnt[0] // 2 + w_cnt[-1] // 2)
  else:
    ans = tmp
    print(ans * k)
  
  
  

main()

Solution (Python):
def f(S, K):
    if K == 0: return 0
    cnt = 0
    p = ''
    for i, s in enumerate(S):
        if p == s:
            cnt += 1
            p = ''
        else:
            p = s
    cnt *= K
    if p == S[0]:
        cnt += K - 1
    return cnt


if __name__ == '__main__':
    S = input()
    K = int(input())
    cnt1 = 0
    cnt2 = 0

    if len(S) % 2 == 1 and K > 1:
        S2 = S * 2
        K2 = K // 2
        cnt1 = f(S2, K2)

        S2 = S[::-1] * 2
        cnt2 = f(S2, K2)

        K = K % 2
    cnt1 += f(S, K)
    cnt2 += f(S[::-1], K)

    print(min(cnt1, cnt2))

Solution (Python):
import copy
S = list(input())
K = int(input())


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1, len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

a = S[-1]
last = 0
for i in range(2, len(S)+1):
    last += 1
    if S[-i] != a:
        break

if all([e == S[0] for e in S[1:]]) == True:
    if len(S)%2==0:
        print(int(len(S)/2*K))
    else:
        print(int(len(S)//2*((K+1)//2)+(len(S)//2+1)*(K//2)))
elif S[0] == S[-1] and last % 2 == 1:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2, len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Solution (Python):
s=input()
k=int(input())
l=list(s)
r=list(s*2)
z=set(l)
x=0
y=0
if len(z)==1 :
    print(int((len(l)*k)/2))
else:
    for i in range(1,len(l)):
      if l[i-1]==l[i]:
        l[i]=0
        x+=1
    for i in range(1,len(r)):
       if r[i-1]==r[i]:
        r[i]=0
        y+=1
    if 2*x==y:
         print(k*x)
    else:
         print(k*x+k-1)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(input())
K = int(input())
N = len(S)
ans = 0
last_same = False
if N==1:
    print(K//2)
    exit()
for i in range(1,N):
    if S[i]!=S[i-1]:
        break
else:
    print(N*K//2)
    exit()
p = False


pre_num = 0
l_num = 1
if S[0]==S[-1]:
    pre_num = 1
    for i in range(1,N-1):
        if S[i]==S[i-1]:
            pre_num += 1
        else:
            break
    l_num = 1
    for i in range(N-1,0,-1):
        if S[i]==S[i-1]:
            l_num += 1
        else:
            break

for i in range(pre_num,N-l_num):
    if p:
        p = False
        continue
    if S[i]==S[i+1]:
        ans += 1
        p = True

ANS = (pre_num//2) + ans*K + ((pre_num+l_num)//2)*(K-1) + (l_num//2)

print(ANS)

Solution (Python):
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k

if not(s[0] == s[len(s)-1]):
    pass
elif len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
elif len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[len(s)-1-i] == s[len(s)-i-2]:
            cnt3 = cnt3 + 1
        else:
            break

    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
    if cnt2 == 0 and cnt3 == 0:
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Solution (Python):
import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

S = list(input())
K = int(input())
i = 0
ans = 0

if len(S) == 1 and K>1:
    print(K//2)
    exit()

while i+1 < len(S):
    tempc= 1
    while S[i] == S[i+1]:
        tempc += 1
        if tempc == len(S):
            print(tempc * K // 2)
            exit()
        if i+2 < len(S):
            i += 1
        else:
            break

    ans += tempc //2
    if i+2 >= len(S):
        break
    i += 1

j=2
frag = 0
if S[0] == S[-1] and K >1:


    last_num = 1
    while S[-1] == S[-j]:
        last_num += 1
        if (j+1) <= len(S):
            j += 1
        else:
            break
    i=0
    first_num = 1
    while S[i] == S[i+1]:
        first_num += 1
        if i+2 < len(S):
            i += 1
        else:
            break

    if last_num%2 + first_num%2 == 2:
        frag = 1


print((ans+frag)*K-frag)

Solution (Python):
S = list(input())
K = int(input())
S2 = S[:]

if len(S) == 1:
    print(K//2)
    exit()
cons = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cons += 1
        S[i+1] = '_'
if K == 1:
    print(cons)
    exit()
if S[0] != S[-1]:
    print(cons * K)
    exit()
cons3 = 0
if len(set(S2)) == 1:
    S2 = S2 * 2 + ['']
    for i in range(len(S2) - 1):
        if S2[i] == S2[i+1]:
            cons3 += 1
            S2[i+1] = '_'
    print(cons3 * (K//2) + cons * (K%2))
    exit()


cons2 = 1
j = 0
while True:
    if S2[j] == S2[j+1]:
        cons2 += 1
        j += 1
    else:
        break
if cons2 % 2 == 0:
    minus = 0
else:
    minus = 1
print(cons * K + minus * (K-1) )

Solution (Python):
def a(s):
    count = 0
    test = s[0]
    for i in range(len(s) - 1):
        if s[i] == s[i + 1] and test[i] != 'A':
            
            test += 'A'
            count += 1
        else:
            test += s[i]
    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))
elif len(s) == s.count(s[0]):
    new_s = s * 2
    count_2 = a(new_s)
    print(count_2 * (k // 2) + a(s) * (k % 2))
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)

Solution (Python):
import collections
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()
        K = int(input())

        if len(S) == 1:
            print(K // 2)
            exit()

        ans = 0
        start = 0
        end = len(S)
        cnt_1 = 1
        cnt_2 = 1

        if len(collections.Counter(S)) == 1:
            print((len(S) * K) // 2)
            exit()

        if S[0] == S[-1]:
            for i in range(1, len(S) - 1):
                if S[i] != S[0]:
                    start = i
                    break
                else:
                    cnt_1 += 1

            for i in range(1, len(S) - 1)[::-1]:
                if S[i] != S[0]:
                    end = i + 1
                    break
                else:
                    cnt_2 += 1

            ans += (cnt_1 // 2) + ((cnt_1 + cnt_2) // 2) * (K - 1) + (cnt_2 // 2)

        before = S[start]
        length = 1
        changes = 0
        for i in range(start + 1, end):
            if S[i] == before:
                length += 1
            else:
                changes += length // 2
                length = 1
                before = S[i]

        changes += length // 2
        ans += changes * K
        print(ans)



if __name__ == '__main__':
    AtCoder().main()

Solution (Python):
S = str(input())
K = int(input())
ans = int(0)
count = int(0)
count_first = int(0)
onelen = int(0)
if S[0]==S[-1]:
    count_first += 1

for i in range(1,len(S)):
    if S[i] == S[i - 1] and i != len(S) - 1:
        count += 1
    elif S[i] == S[i - 1] and i == len(S) - 1:
        count += 1
        if count != len(S)-1:
            onelen += (count + 1) // 2
        else:
            ans = ((count+1) * K) // 2

    else:
        onelen += (count + 1) // 2
        count = 0
if ans == 0:
    ans = onelen * K


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


if count_first==1 and count != len(S)-1:
    i = int(0)
    while S[i] == S[i + 1]:
        count_first += 1
        i += 1
    i = int(-1)
    while S[i] == S[i - 1]:
        count_first += 1
        i -= 1
    if count_first % 2 == 1:
        ans += K-1

print(ans)

Solution (Python):

s = input()
k = int(input())
ls = len(s)

word_list = []

a = ""
b = 1

for i, v in enumerate(s):
    if a != v:
        if a != "":
            word_list.append((a,b))
        a = v
        b = 1
    else:
        b += 1
    if i == ls-1:
        word_list.append((a,b))

ans = 0
if len(word_list) == 1:
    ans = (word_list[0][1] * k) // 2
else:
    if word_list[-1][0] != word_list[0][0]:
        for i in word_list:
            ans += (i[1])//2
        ans *= k
    else:
        head, *body, tail = word_list

        for i in body:
            ans += (i[1])//2
        ans *= k
        ans += head[1] // 2
        ans += tail[1] // 2
        headtail = (head[1]+tail[1]) //2 *(k-1)
        ans += headtail

print(ans)

Solution (Python):

from sys import stdin

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

l = len(S)
ans = 0

for i in range(l):
    mo = S[0]*l
    if mo == S:
        ans = l*K//2
        print(ans)
        exit()
    else:
        pass


if S[0] == S[l-1]:
    S2 = S*2
    l2 = len(S2)
    a = 0
    ans2 = 0
    while a < l2-1:
        if S2[a] == S2[a+1]:
            t = S2[a]
            c = 1
            for i in range(1,l2-a):
                if t == S2[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans2 += j
            a += c
        else:
            a += 1
    a = 0
    ans1 = 0
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans1 += j
            a += c
        else:
            a += 1
    differ = ans2-ans1*2
    ans = differ*(K-1) + ans1*K
else:
    a = 0
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans += j
            a += c
        else:
            a += 1

    ans = ans*K

print(ans)

Solution (Python):
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:

    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

    for i in range(len(S)):
        if t == S[-i-1]:
            count_b += 1
        else:
            break

    T = S[count_a:-count_b]

    m = None
    for i in range(len(T)):

        if i == 0:
            m = T[i]
            continue

        if m == T[i]:
            result += 1
            m = None
            continue

        m = T[i]

    count = count_a + count_b
    change = count // 2
    change_a = count_a // 2
    change_b = count_b // 2

    print(change_a + change_b + (K - 1) * change + K * result)

Solution (Python):
s = input()
k = int(input())
n = 0
j = s[0]
l = 1
p = 0
fi,la = 0,0
c = 1
if len(set(list(s))) == 1:
  print(len(s)*k//2)
else:
  for i in range(1,len(s)):
    if s[i] == j:
      l += 1
    else:
      n += l//2
      if s[i-1] == s[0] and c == 1:
        fi = l
        c = 0
      l = 1
      j = s[i]
  n += l//2
  la = l
  if s[0] == s[-1] and fi%2 == 1 and la%2 == 1:
    p = k-1
  print(n*k+p)

Solution (Python):
S=list(input())
S_=[]
S__=[]
K=int(input())
ans=0
i=0
if S[0]==S[-1]:
    s=S[0]
    S_.append(S.pop(0))
    while len(S)>0:
        if s==S[0]:
            S_.append(S.pop(0))
        else:
            break
    while len(S)>0:
        if s==S[-1]:
            S__.append(S.pop(-1))
        else:
            break
if len(S)>0:
    while i<len(S)-1:
        if S[i+1]==S[i]:
            ans+=K
            i+=1
        i+=1
if len(S)>0:
    ans+=(K-1)*((len(S_)+len(S__))//2)
    ans+=len(S_)//2
    ans+=len(S__)//2
else:
    ans+=K*(len(S_)+len(S__))//2
print(ans)

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

ans1 = 0
old = ""
flag = 0
for i in range(N):
    if old == "":
        old = S[i]
    else:
        if old == S[i]:
            old = ""
            ans1+=1
        else:
            old = S[i]
            
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 1

if flag == 0:
    print(ans1*K)
else:
    ans2 = 0
    old = ""
    for i in range(N):
        if i == 0:
            if S[0] == S[-1]:
                ans2+=1
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 2
    
    k = K//2
    if flag == 2:
        print(ans1*1+ans2*(K-1))
    else:
        print(ans1*(K-k)+ans2*(k))

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    r = sum(x // 2 for x in a) * k
    if s[0] == s[-1]:
        x, y = a[0], a[-1]
        r += ((x + y) // 2 - x // 2 - y // 2) * (k - 1)
    print(r)

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  cnt2 = 0
  p2 = 1
  s2 = s + s
  while p2 < len(s2):
    if s2[p2-1] == s2[p2]:
      cnt2 += 1
      p2 += 1
    p2 += 1

  flg = 1
  for i in range(len(s)-1):
    if s[i] != s[i+1]:
      flg = 0
  
  if 2*cnt < cnt2:
    if flg == 1:
      print(k*cnt + (k//2))
    else:
      print(k*cnt + (k - 1))
  else:
    print(k*cnt)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)
K = INT()

A = []
cur = S[0]
cnt = 1
for i in range(1, N):
    if S[i-1] == S[i]:
        cnt += 1
    else:
        A.append((S[i-1], cnt))
        cnt = 1
A.append((S[N-1], cnt))

if len(A) == 1:
    print(N*K//2)
    exit()

change = 0
for s, cnt in A:
    change += cnt // 2

if S[0] == S[N-1] and A[-1][1] % 2 == 1 and A[0][1] % 2 == 1:
    print((change+1)*K-1)
else:
    print(change*K)