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 = input()
n = len(s)
k = int(input())
start = True
start_count = 0
start_char = s[0]
inside_num = 0
end_count = 0
end_char = ""
memory = s[0]
count = 1
for i in range(1,n):
    if s[i] == memory:
        count += 1
    else:
        if start:
            start_count = count
            start = False
        else:
            inside_num += count//2
        count = 1
        memory = s[i]
end_count = count
end_char = memory
ans = 0
if start_char == end_char:
    if end_count == n:
        ans = n*k//2
    else:
        ans += inside_num*k
        ans += (start_count+end_count)//2*(k-1)+start_count//2+end_count//2
else:
    inside_num += start_count//2
    inside_num += end_count//2
    ans += inside_num*k
print(ans)

Solution (Python):
import sys
s = input()
k = int(input())
n = len(s)
if n==1:
    print(k//2)
    sys.exit()
if n==2:
    print(k if s[0]==s[1] else 0)
    sys.exit()
i = 1
change = [0, 0]
same = True
while i<n:
    if s[i-1]==s[i]:
        if i==n-1:
            same = False
        change[0] += 1
        i += 1
    i += 1
if s[0]!=s[-1]:
    same = False
if not same:
    print(change[0]*k)
    sys.exit()
i = 2
change[1] = 1
same = True
while i<n:
    if s[i-1]==s[i]:
        if i==n-1:
            same = False
        change[1] += 1
        i += 1
    i += 1
if same:
    print(change[0]+change[1]*(k-1))
else:
    print(change[0]*((k+1)//2)+change[1]*(k//2))

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

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

def n_changes(s):
    sc = s.copy()
    count = 0
    for i in range(1, len(sc)):
        if sc[i] == sc[i-1]:
            sc[i] = "X"
            count += 1
    return count

print(n_changes(S) + (K-1) * (n_changes(2*S) - n_changes(S)))

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(S) + (K-1) * (n_change(2*S) - n_change(S)))

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

ans = 0



tmp = ''
tmp_num = 0

check = ''
check_num = 0

if len(collections.Counter(s).keys()) == 1:
    ans = len(s)*k//2

else:
    for i, w in enumerate(s):
        if i == 0:
            tmp += w
            tmp_num += 1
            continue
        
        if tmp[-1] == w:
            tmp += w
            tmp_num += 1
        
        elif tmp[-1] != w:
            if check_num == 0:
                check = tmp
                check_num = 1

            ans += (tmp_num // 2) * k
            
            tmp = w
            tmp_num = 1
        
        if i == len(s)-1:
            if len(tmp) == 1:
                if check[0] == tmp and (len(tmp)%2 == 1 and len(check)%2 == 1):
                    ans += k-1
            else:
                
                if check[0] == tmp[-1] and (len(tmp)%2 == 1 and len(check)%2 == 1):
                    ans += k-1

            ans += (tmp_num // 2) * k
            

print(ans)

Solution (Python):
s=input()
k=int(input())
n = len(s)
same = True
for i in range(1,len(s)):
    if s[i]!=s[i-1]:
        same = False
if same:
    print(len(s)*k//2)
    exit()

for i in range(1,len(s)):
    if s[i] != s[i-1]:
        before = i-1
        break
for i in range(len(s)-1,-1,-1):
    if s[i] != s[i-1]:
        after = i
        break

ans = (before+1)//2 
ans += (len(s)-after)//2
if s[0] == s[-1]:
    ans += (before + 1 + len(s) - after)//2 * (k-1)
else:
    ans += ((before+1)//2  + (len(s)-after)//2)*(k-1)
cnt = 1
for i in range(before+2, after):
    if s[i] != s[i-1]:
        ans += cnt //2 *k
        cnt = 1
    else:
        cnt +=1
ans += cnt //2 * k
print(ans)

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

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

first, last = s[0], s[-1]
first_flag = 1
a, b  = 0, 0
tmp = s[0]
tmp_cnt = 0
cnt = 0

for i in range(len(s)):
    c = s[i]
    if c == first and first_flag:  
        a += 1
    elif c != first:
        first_flag = 0
    
    if c == tmp:  
        tmp_cnt += 1
    else:
        cnt += (tmp_cnt // 2)
        tmp_cnt = 1
        tmp = c
        
    if i == len(s) -1:  
        b = tmp_cnt
        cnt += (tmp_cnt // 2)

if first != last:  
    ans = cnt * k
else:  
    ans = cnt * k - (a//2 + b//2 - (a+b)//2)*(k-1)

print(ans)

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

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

first, last = s[0], s[-1]
first_flag = 1
a, b  = 0, 0
tmp = s[0]
tmp_cnt = 0
cnt = 0

for i in range(len(s)):
    c = s[i]
    if c == first and first_flag:
        a += 1
    elif c != first:
        first_flag = 0
    
    if c == tmp:
        tmp_cnt += 1
    else:
        cnt += (tmp_cnt // 2)
        tmp_cnt = 1
        tmp = c
    if i == len(s) -1:
        b = tmp_cnt
        cnt += (tmp_cnt // 2)

if first != last:
    ans = cnt * k
else:
    ans = cnt * k - (a//2 + b//2 - (a+b)//2)*(k-1)

print(ans)

Solution (Python):
s = input()
k = int(input())
cnt = 0
if len(set(list(s))) == 1:
	cnt = k * len(s) // 2
	print(cnt)
	exit()

x, y = 0, 0
t = s + s
for i in range(len(s) - 1):
	if s[i] == s[i + 1]:
		x += 1
		s = s[:i+1] + '

for i in range(len(t) - 1):
	if t[i] == t[i + 1]:
		y += 1
		t = t[:i+1] + '

cnt = x + (k - 1) * (y - x)
print(cnt)

Solution (Python):
s=list(input())
a=int(input())

w = ""
c = 0
cnt = 0
for i in s:
    if w != i:
        
        cnt += c//2
        c = 1
        w = i
    else:
        c +=1
cnt += c//2


start = 0
start_w = s[0]
for i in s:
    if start_w != i:
        break
    else:
        start +=1

s.reverse()

end = 0
end_w = s[0]
for i in s:
    if end_w != i:
        break
    else:
        end +=1

same = len(list(set(s)))


if s[0] != s[-1]:
    print(cnt*a)
elif same ==1:
    print(len(s)*a//2)
else:
    print(cnt*a-(a-1)*(start//2+end//2-(start+end)//2))

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

if list(S).count(S[0])==len(S):
    print(len(S)*K//2)

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

    if S[0]==S[-1]:
        a=1
        i=0
        while i<len(S)-1:
            if S[i]==S[i+1]:
                a+=1
                i+=1
            else:
                break
        b=1
        i=len(S)-1
        while i>0:
            if S[i]==S[i-1]:
                b+=1
                i+=-1
            else:
                break

        S_count*=K
        S_count-=(K-1)*(a//2 + b//2 - (a+b)//2)

    else:
        S_count*=K
    print(S_count)

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

l = len(s)

ss = s + s
ll = len(ss)

single = 0
cnt = 0

for i in range(1, l):
    if s[i - 1] == s[i]:
        if cnt == 0:
            single += 1
            cnt = 1
        else:
            if cnt % 2 == 0:
                single += 1
                cnt += 1
            else:
                cnt += 1
    else:
        cnt = 0

double = 0
cnt = 0

for i in range(1, ll):
    if ss[i - 1] == ss[i]:
        if cnt == 0:
            double += 1
            cnt = 1
        else:
            if cnt % 2 == 0:
                double += 1
                cnt += 1
            else:
                cnt += 1
    else:
        cnt = 0

if len(set(s)) == 1:
    ans = double * (k // 2) + single * (k % 2)
elif single * 2 == double:
    ans = single * k
else:
    ans = single + (single + 1) * (k - 1)


print(ans)

Solution (Python):
s = list(input())

k = int(input())

n = len(s)

if(len(set(s)) == 1):
    print(n*k//2)
else:
    t = 0
    c = 1
    for i in range(n-1):
        if(s[i] == s[i+1]):
            c += 1
        else:
            t += c // 2
            c = 1
    t += c // 2
    if(s[0] != s[-1]):
        print(t*k)
    else:
        a = 0
        b = 0
        for i in range(n):
            if(s[i] == s[0]):a+=1
            else:break
        for i in range(n)[::-1]:
            if(s[i] == s[-1]):b+=1
            else:break
        print(t*k - (a//2 + b//2 - (a+b)//2) * (k-1))

Solution (Python):
s = list(input())
n = int(input())
ans = 0
if s[0] != s[-1]:
    cnt = 1
    tmp = ''
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    print(ans*n)
else:
    cnt = 1
    tmp = ''
    f = 0
    for i in range(len(s)):
        if i == 0:
            tmp = s[i]
            continue
        if tmp == s[i]:
            cnt += 1
        else:
            if f == 0:
                f = cnt
            ans += cnt // 2
            cnt = 1
            tmp = s[i]
    ans += cnt // 2
    if f == 0:
        print((cnt*n)//2)
    else:
        m = ans - f//2 - cnt//2
        ans = f // 2 + cnt // 2
        
        ans += m * n
        

        ans += (n-1) * ((f+cnt) // 2)
        
        print(ans)

Solution (Python):

"""
sをk回繰り返して得られる文字列tを、全て隣り合う文字が異なるようにする
為に必要な操作回数を求めよ
解法：
最初と最後だけ愚直にカウント
あとは中間部分を連結してそれをk-1回繰り返す
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
s = readline().rstrip().decode('utf-8')
k = int(readline())
first = 0
last = 0
l = 0
r = len(s)


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

if s[0] == s[-1]:
    res = s[0]
    for idx,i in enumerate(s):
        if i == res:
            first += 1
        else:
            l = idx
            break
    res = s[-1]
    for idx,i in enumerate(s[::-1]):
        if i == res:
            last += 1
        else:
            r = len(s)-idx
            break

count = 0
res = ""
for i in s[l:r]:
    if res == "":
        res = i
    elif res == i:
        count += 1
        res = ""
    else:
        res = i

print(first//2+last//2+(first+last)//2*(k-1)+count*k)

Solution (Python):
s=input()
k=int(input())
l=len(s)
now=1
seq=[]
re=False
all=False
for i in range(l):
    if s[i%l]==s[(i+1)%l]:
        if i==l-1:
            re=True
            if len(seq)==0:
                all=True
            else:
                first=seq[0]//2
                end=now//2
                seq[0]=(seq[0]+now)
        now+=1
    else:
        seq.append(now)
        now=1
if all:
    ans=len(s)*k//2
else:
    seq=[i//2 for i in seq]
    if re:
        ans=sum(seq)*k-seq[0]+first+end
    else:
        ans=sum(seq)*k

print(ans)

Solution (Python):
from itertools import groupby
s = input()
k = int(input())
ans = 0
count = []
for i, j in groupby(s):
    count.append(len(list(j)))
if len(set(s)) == 1:
    ans = (len(s)*k)//2
elif k > 1 and s[0] == s[-1]:
    f = count[0]
    e = count[-1]
    ans += f//2 + e//2 + (f+e)//2*(k-1)
    for n in count[1:-1]:
        ans += n//2 * k
else:
    for n in count:
        ans += n//2 * k
print(ans)

Solution (Python):
import itertools
def main():
  S = input()
  K = int(input())
  
  if len(set(S)) == 1:
    print(len(S) * K // 2)
    return
  
  cc = 0
  gg = itertools.groupby(S)
  for key, item in gg:
    cc += len(list(item))//2
  a = b = 0
  if S[0] == S[-1]:
    while a < len(S) and S[a] == S[0]:
      a += 1
    S = S[::-1]
    while b < len(S) and S[b] == S[0]:
      b += 1
  output = K * cc - (K-1) * (a//2 + b//2 - (a+b)//2)
  print(output)
  return

if __name__ == '__main__':
  main()

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


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

    N = len(S)
    if S == S[0] * N:
        ans = (N * K) // 2
    elif S[0] == S[-1]:
        a = 1
        for i in range(N):
            if S[i] == S[i+1]:
                a += 1
            else:
                break
        b = 1
        for i in reversed(range(N)):
            if S[i-1] == S[i]:
                b += 1
            else:
                break
        seq = 1
        ans = 0
        for i in range(a, N-b):
            if S[i] == S[i+1]:
                seq += 1
            elif seq > 1:
                ans += seq//2
                seq = 1
        if seq > 1:
            ans += seq//2
        ans *= K
        ans += a//2
        ans += (a+b)//2 * (K-1)
        ans += b//2
    else:
        seq = 1
        ans = 0
        for i in range(N-1):
            if S[i] == S[i+1]:
                seq += 1
            elif seq > 1:
                ans += seq//2
                seq = 1
        if seq > 1:
            ans += seq//2
        ans *= K

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
S = list(input())
n = len(S)
k = int(input())
  
if len(set(S)) == 1:
  print((n * k) // 2)
  exit()

res = 0
i = 0
while i < n - 1:
  if S[i] == S[i + 1]:
    res += 1
    i += 1
  i += 1

if S[0] != S[-1]:
  print(res * k)
else:
	i = 1
	while S[0] == S[i]:
		i += 1

	j = 1
	while S[-1] == S[-1 - j]:
		j += 1
	
	if i % 2 == 0 or j % 2 == 0:
		print(res * k)
	else:
		print(res * k + (k - 1))

Solution (Python):
S = list(input())
N = len(S)
K = int(input())
  
if len(set(S)) == 1:
  print((N*K)//2)
  exit()

ans = 0
i = 0
while i < N-1:
  if S[i] == S[i+1]:
    ans += 1
    i += 1
  i += 1

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

a = 1
while S[0] == S[a]:
  a += 1

b = 1
while S[N-1] == S[N-1-b]:
  b += 1
print(ans*K - (K-1)*(a//2 + b//2 - (a+b)//2))

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

prev = ''
cnt = 0
for s in S:
    if s == prev:
        cnt += 1
        prev = ''
    else:
        prev = s

b = 0
if K % 2 == 0:
    b += 1
if K > 2 and S[0] == S[-1]:
    mae = 1
    while mae < len(S) and S[mae] == S[0]:
        mae += 1
    if mae % 2 == 1 and mae != len(S):
        usiro = 1
        i = len(S) - 2
        while S[i] == S[-1]:
            usiro += 1
            i -= 1
        if usiro % 2 == 1:
            b = K // 2

if K % 2 == 0:
    res = cnt * (K // 2) + (b - 1)
else:
    res = cnt * (K // 2) + cnt // 2 + b
print(res)

Solution (Python):
s=str(input())
k=int(input())
ans=0
count=1
c1=0
c2=0

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

if s[0]==s[-1]:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
        else:
            if 2<=count:
                ans+=count//2
            count=1
    ans+=count//2
    i=0
    while s[i]==s[0]:
        c1+=1
        i+=1
    
    i=len(s)-1
    while s[i]==s[-1]:
        c2+=1
        i-=1
    
    ans*=k
    if c1%2==1 and c2%2==1:
        ans+=k-1

    print(ans)


else:
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            count+=1
        else:
            if 2<=count:
                ans+=count//2
            count=1
    ans+=count//2
    print(ans*k)

Solution (Python):
s = input()
k = int(input())
n = len(s)
c = 0
x = []
a = 1

for i in range(n):
    if i<n-1 and s[i]==s[i+1]:
        a += 1
    else:
        c += a//2
        x.append(a)
        a = 1

c2 = 0
if s[0]==s[-1]:
    c2 = x[0]//2+x[-1]//2-(x[0]+x[-1])//2

print(len(s)*k//2 if len(set(s))==1 else c*k-c2*(k-1))

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(s) + (k-1) * (n_change(2*s) - n_change(s)))

Solution (Python):
import math
import sys

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

if len(set(s)) == 1:
    print(len(s) * k // 2)
    sys.exit()
ss = s * 2
tmp, tmp2 = 0, 0
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
        s[i + 1] = '-1'
for i in range(len(s) * 2 - 1):
    if ss[i] == ss[i + 1]:
        tmp2 += 1
        ss[i + 1] = '-1'
print(tmp + (tmp2 - tmp) * (k - 1))

Solution (Python):
if __name__ == '__main__':
    s = list(input())
    ss = s + s
    k = int(input())
    if len(set(s)) == 1:
        print(len(s) * k // 2)
        exit(0)

    tmp, tmp2 = 0, 0
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            tmp += 1
            s[i + 1] = '-1'
    for i in range(len(s) * 2 - 1):
        if ss[i] == ss[i + 1]:
            tmp2 += 1
            ss[i + 1] = '-1'
    print(tmp + (tmp2 - tmp) * (k - 1))

Solution (Python):
import sys

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

flg = True
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        flg = False
        break

if flg:
    print(len(s) * k // 2)
    sys.exit()

ans = 0
skip = -1
for i in range(len(s) - 1):
    if i == skip:
        continue
    
    if s[i] == s[i + 1]:
        ans += 1
        skip = i + 1
        

ans *= k


if s[0] == s[-1]:
    cnt_h = 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt_h += 1
        else:
            break
    
    cnt_t = 1
    for i in reversed(range(1, len(s))):
        if s[i] == s[i - 1]:
            cnt_t += 1
        else:
            break
    
    cnt = cnt_h // 2 + cnt_t // 2
    cnt -= (cnt_h + cnt_t) // 2
    cnt *= k - 1
    ans -= cnt

print(ans)

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

if S[0] != S[-1]:
    ans = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S = S[:i + 1] + '*' + S[i + 2:]
            ans += 1
    print(ans * K)
else:
    
    k, l = 0, len(S) - 1
    while k < len(S) and S[k] == S[-1]:
        k += 1
    while l > -1 and S[l] == S[0]:
        l -= 1

    
    A = S[k:l + 1]
    ans = 0
    for i in range(len(A) - 1):
        if A[i + 1] == A[i]:
            A = A[:i + 1] + '*' + A[i + 2:]
            ans += 1
    ans *= K

    
    if l == -1 and k == len(S):
        
        print((len(S) * K) // 2)
    else:
        ans += (len(S) - l + k - 1) // 2 * (K - 1)
        ans += (len(S) - l - 1) // 2 + k // 2
        print(ans)

Solution (Python):
s=input()
k=int(input())
s0=s[0]
s1=s[-1]
cs=[]
p=s[0]
c=1
for i in range(1,len(s)):
      if p==s[i]:
            c+=1
      else:
            cs.append(c)
            p=s[i]
            c=1
cs.append(c)
if len(cs)==1:
      print((cs[0]*k)//2)
      exit()
ans=0
if s0!=s1:
      for csi in cs:
            ans+=csi//2
      ans*=k
      print(ans)
      exit()
else:
      for i in range(1,len(cs)-1):
            ans+=cs[i]//2
      ans*=k
      ans+=cs[0]//2+cs[-1]//2
      ans+=((cs[0]+cs[-1])//2)*(k-1)
      print(ans)

Solution (Python):
def cal(S):
    tmp, a, cnt = S[0], 1, 0
    flag = True
    for s in S[1:]:
        if S[0]!=s: flag=False
        if flag: a+=1
        if tmp[-1]==s:
            s = '*'
            cnt += 1
        tmp += s
    return a, cnt


S = input().replace('\n', '')
k = int(input())
ans = 0
if len(list(set(S)))==1:
    ans = len(S)*k//2
else:
    a, cnt = cal(S)
    b, _ = cal(S[::-1])
    ans = cnt*k
    if S[0]==S[-1]:
        ans -= ((a//2)+(b//2)-((a+b)//2))*(k-1)
print(ans)

Solution (Python):
def main():
    S = input()
    K = int(input())
    A = []
    last = S[0]
    count = 1
    for i in range(1, len(S)):
        s = S[i]
        if last == s:
            count += 1
        else:
            A.append({'key': last, 'count': count})
            count = 1
        last = s
    A.append({'key': last, 'count': count})
    
    if len(A) == 1:
        c = A[0]['count']
        l = c*K
        ans = l//2
        print(ans)
        return
    ans = 0
    if A[0]['key'] != A[-1]['key']:
        for a in A:
            c = a['count']
            ans += (c//2) * K
        print(ans)
        return
    for i in range(1, len(A) - 1):
        a = A[i]
        c = a['count']
        ans += (c//2) * K
    a0 = A[0]
    a1 = A[-1]
    c0 = a0['count']
    c1 = a1['count']
    ans += c0//2
    ans += c1//2
    ans += ((c0 + c1)//2) * (K - 1)
    print(ans)

if __name__ == '__main__':
    main()

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

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

def n_change(sss):
    s = sss.copy()
    x = 0
    for i in range(1,len(s)):
        if s[i] == s[i-1]:
            s[i] = "X"
            x += 1
    return x

print(n_change(S) + (K-1) * (n_change(2*S) - n_change(S)))

Solution (Python):
from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product








import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    S = list(input())
    K = int(input())
    l = []
    cnt = 0
    if len(S)==1:
        print(K//2)
    else:
        gr = groupby(S)
        for key, group in gr:
            l.append(list(group))
        if len(l)==1:
            print(len(S)*K//2)
        else:
            for i in range(len(l)):
                cnt += len(l[i]) // 2
                
            if l[0][0] == l[-1][0]:
                dev = len(l[0]) // 2 + len(l[-1]) // 2 - (len(l[-1]) + len(l[0])) // 2
            else:
                dev = 0
            print(cnt * K - dev * (K - 1))


resolve()

Solution (Python):


int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]


def solve():
    S = list(input())
    k = II()
    n = len(S)
    S2 = S[:]

    if len(set(S)) == 1:
        print(n * k // 2)
    else:
        cnt = 0
        for i in range(n-1):
            j = i + 1
            si, sj = S[i], S[j]
            if si == sj:
                S[j] = i
                cnt += 1
        if S[0] == S[-1]:
            moji = S[0]
            sl = 0
            for s in S2:
                if s == moji:
                    sl += 1
                else:
                    break
            sr = 0
            for s in S2[::-1]:
                if s == moji:
                    sr += 1
                else:
                    break

            print(cnt * k + ((sl+sr)//2 - sl//2 - sr//2) * (k - 1))
        else:
            print(cnt * k)


if __name__ == '__main__':
    solve()

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



def func(S):
    inner = 0
    S = list(S)
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            S[i + 1] = '
            inner += 1
    return inner

if len(set(list(S))) == 1:
    print(len(S) * K // 2)
else:
    inner = func(S)
    inter = func(S * 2) - func(S) * 2
    print(inner * K + inter * (K - 1))

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def run_length(s):
    ans=[]
    if len(s)==1:
        ans.append((s,1))
        return ans
    s_pre=s[0]
    cunt=1
    for i,ss in enumerate(s[1:],1):
        if ss==s_pre:
            cunt+=1
        else:
            ans.append((s_pre,cunt))
            cunt=1
            s_pre=ss
    ans.append((s_pre,cunt))
    return ans

def main():
    s=input()
    k=int(input())
    s_lis=run_length(s)
    ans=0    
    if len(s_lis)==1:
        print(k*s_lis[0][1]//2)
    else:
        for tup in s_lis:
            ans+=tup[1]//2  
        if s_lis[0][0]==s_lis[-1][0]:
            a=s_lis[0][1]
            b=s_lis[-1][1]
            print(ans*k-(a//2+b//2-(a+b)//2)*(k-1))
        else:
            print(ans*k)


if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def run_length(s):
    ans=[]
    if len(s)==1:
        ans.append((s,1))
        return ans
    s_pre=s[0]
    cunt=1
    for i,ss in enumerate(s[1:],1):
        if ss==s_pre:
            cunt+=1
        else:
            ans.append((s_pre,cunt))
            cunt=1
            s_pre=ss
    ans.append((s_pre,cunt))
    return ans

def main():
    s=input()
    k=int(input())
    s_lis=run_length(s)
    ans=0    
    if len(s_lis)==1:
        print(k*s_lis[0][1]//2)
    else:
        for tup in s_lis:
            ans+=tup[1]//2  
        if s_lis[0][0]==s_lis[-1][0]:
            a=s_lis[0][1]
            b=s_lis[-1][1]
            print(ans*k-(a//2+b//2-(a+b)//2)*(k-1))
        else:
            print(ans*k)


if __name__=='__main__':
    main()

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

def count_replace(l):
    ret = 0
    cnt = 1
    for i in range(len(l)-1):
        if l[i] == l[i+1]: 
            cnt += 1
        elif l[i] != l[i+1]:
            ret += cnt//2
            cnt = 1
    ret += cnt // 2 
    return ret

if len(set(s)) == 1: 
    ans = (len(s) * k) // 2
elif s[0] != s[-1]: 
    ans = count_replace(s)
    ans = k * ans
else: 
    
    cnt_pre = 1
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt_pre += 1
        else:
            break
    
    cnt_pos = 1
    for i in reversed(range(len(s))):
        if s[i] == s[i-1]:
            cnt_pos += 1
        else:
            break
    
    ans = cnt_pre//2 + cnt_pos//2 + (k-1) * ((cnt_pos+cnt_pre)//2)
    
    ret = count_replace(s[cnt_pre:-cnt_pos])
    ans += k * ret

print(ans)

Solution (Python):
s = input()
k = int(input())
li = list(s)
lee = len(s)

if len(list(set(li))) == 1:
    print((lee)*k//2)
    exit()


def motome(n,list):
    count = 0
    kaihi_flag = False
    li = list*n
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    
    
    return count

if k >= 5 and k%2 == 1:
    a = motome(3,li)
    b = motome(5,li)
    diff = b - a
    ans = b + (k-5)//2*diff
    print(ans)
elif k >=5 and k%2 == 0:
    a = motome(2,li)
    b = motome(4,li)
    diff = b - a
    ans = b + (k-4)//2*diff
    print(ans)
else:
    count = 0
    kaihi_flag = False
    li = li*k
    for i,l in enumerate(li):
        if i == 0:
            continue
        if l == li[i-1] and not kaihi_flag:
            count += 1
            kaihi_flag = True
        else:
            kaihi_flag = False
    
    
    print(count)

Solution (Python):
s = list(input())
n = len(s)
k = int(input())
ans = 0
if s.count(s[0]) == n:
    ans = (n * k) // 2
else:
    c = 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            c += 1
        else:
            ans += c // 2
            c = 1
    ans += c // 2
    ans *= k
    if s[0] == s[n - 1]:
        c1 = 0
        c2 = 0
        for i in range(n):
            if s[i] == s[0]:
                c1 += 1
            else:
                break
        for i in range(n - 1, -1, -1):
            if s[i] == s[n - 1]:
                c2 += 1
            else:
                break
        if not c1 // 2 + c2 // 2 == (c1 + c2) // 2:
            ans += k
            ans -= 1
print(ans)

Solution (Python):
s=input()
k=int(input())
if len(s)==1:
  print(k//2)
  exit()
c=1
ans=0
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    c+=1
  else:
    ans+=c//2
    c=1
ans+=c//2
ans*=k
cl=0
i=0
while  i<len(s) and s[-1]==s[i]:
  cl+=1
  i+=1
if i<len(s) and cl%2==1 and c%2==1:
  ans+=k-1
elif cl%2==1 and c%2==1:
  ans+=k//2
print(ans)

Solution (Python):
def main():
    s=input()
    k=int(input())
    if s.count(s[0])==len(s):
        print(len(s)*k//2)
    else:
        ans=0
        cnt=1
        res=[0,0]
        t=s+"X"
        flg=0
        for i in range(len(s)):
            if t[i]==t[i+1]:
                cnt+=1
            else:
                if flg==0:
                    res[0]=cnt
                    flg=1
                elif i==len(s)-1:
                    res[1]=cnt
                ans+=cnt//2
                cnt=1
        ans*=k
        if s[0]==s[-1] and res[0]%2==1 and res[1]%2==1:
            ans+=k-1
        print(ans)
if __name__=="__main__":
    main()

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

length = len(S)

num_fixed = 0
pre_char = ''
count = 0
for i in range(length):
    now_char = S[i]
    if pre_char != now_char:
        num_fixed += count//2
        count = 0
    count += 1
    pre_char = now_char
    
num_fixed += count//2



if S[0] == S[length - 1]:
    head_length = 1
    head_char = S[0]
    for i in range(1, length):
        now_char = S[i]
        if now_char != head_char:
            break
        head_length += 1
    tail_length = 1
    tail_char = S[length - 1]
    for i in range(1, length):
        now_char = S[length - 1 - i]
        if now_char != tail_char:
            break
        tail_length += 1
    if length == head_length == tail_length:
        ans = length * K // 2
    else:
        ans = K * num_fixed - (K - 1) * (head_length // 2 + tail_length // 2 - (head_length + tail_length) // 2)
else:
    ans = num_fixed * K

print(ans)

Solution (Python):
def f(s,n):
    res = 0
    flg = False
    for i in range(1,n):
        if flg:
            flg = False
            continue
        if S[i] == S[i-1]:
            flg = True
            res += 1
    return res
S = input()
K = int(input())
if len(set(list(S))) == 1:
    print(len(S)*K//2)
    exit()
ans = f(S,len(S))
S = S * 2
print(ans + (f(S,len(S)) - ans) * (K-1))

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

curr = S[0]
tmp = ""
T = []
for s in S:
    if s == curr:
        tmp += s
    else:
        T.append(tmp)
        curr = s
        tmp = s
T.append(tmp)

if len(T) == 1:
    res = (len(S) * K) // 2
    print(res)
    exit(0)

res = 0
t_first = T[0]
t_last = T[-1]

if t_first[0] != t_last[0]:
    res += (len(t_first) // 2) * K
    res += (len(t_last) // 2) * K
else:
    res += len(t_first) // 2
    res += len(t_last) // 2
    res += (len(t_first + t_last) // 2) * (K - 1)

for t in T[1:-1]:
    res += (len(t) // 2) * K

print(res)

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

if S.count(S[0])==N:
  ans= K*N//2
else:
  h=0
  for i in range(N):
    if S[i]==S[0]:
      h +=1
    else:
      break
  t=0
  for i in range(N-1,-1,-1):
    if S[i]==S[-1]:
      t +=1
    else:
      break
  ren=1
  cnt=0
  for i in range(h,N-t):
    if S[i]==S[i-1]:
      ren +=1
      if i==N-t-1:
        cnt += ren//2
    else:
      cnt += ren//2
      ren=1    
      
  if S[0]==S[-1]:
    
    ans= h//2 + (K-1)*((h+t)//2) + t//2 + K*cnt
  else:
    
    ans= (h//2 + cnt + t//2)*K
print(ans)

Solution (Python):
import sys
S=input()
k=int(input())
ans=0
flag=0
left=0
right=0
if S[0]==S[-1]:
    left=1
    for i in range(len(S)-1):
        if S[i+1]==S[0]:
            left+=1
        else:
            break
    if left==len(S):
        print(left*k//2)
        sys.exit()
    right=1
    for i in range(len(S)-1):
        if S[-i-2]==S[0]:
            right+=1
        else:
            break
    
now=""
for i in range(left,len(S)-right):
    if flag==1:
        flag=0
        now=S[i]
        continue
    if now==S[i]:
        ans+=1
        flag=1
    now=S[i]
ans*=k

if S[0]==S[-1]:
    ans+=(left+right)//2*(k-1)
    ans+=left//2+right//2

print(ans)