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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = S()
    k = I()
    l = len(s)
    a = []
    t = s[0]
    c = 1
    for u in s[1:]:
        if t == u:
            c += 1
        else:
            a.append(c)
            t = u
            c = 1
    if len(a) == 0:
        return l * k // 2
    r = 0
    if s[0] == s[-1]:
        for b in a:
            r += b // 2
        r += c // 2
        a[0] += c
        k -= 1
    else:
        a.append(c)
    o = 0
    for b in a:
        o += b // 2

    return r + o * k


print(main())

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

def solve(s):
	tmp = ""
	streak = 1
	cnt = 0
	for i in range(len(s)):
		if s[i] == tmp:
			streak += 1
		else:
			cnt += streak // 2
			tmp = s[i]
			streak = 1
	cnt += streak // 2
	return cnt
	

if len(list(set(list(map(str,s))))) == 1:
	ans = len(s)*k//2
elif s[0] == s[-1]:
	key_c = s[0]
	pre_ind = 0
	epi_ind = 0

	for i in range(len(s)):
		if s[i] != key_c:
			pre_ind = i-1
			break
	for i in range(len(s)):
		if s[-(i+1)] != key_c:
			epi_ind = len(s)-i
			break

	cnt_mid = solve(s[pre_ind+1:epi_ind])
	cnt_left = (pre_ind+1)//2
	cnt_right = (len(s)-epi_ind)//2
	cnt_conection = (pre_ind+1+len(s)-epi_ind)//2

	ans = cnt_left + cnt_mid*k + cnt_conection*(k-1) + cnt_right
	

elif s[0] != s[-1]:
	cnt = solve(s)
	ans = cnt * k

print(ans)

Solution (Python):
def is_all(t):
    for i in range(len(t)-1):
        if t[i]!=t[i+1]:
            return False
    return True

s=input()
n=len(s)
k=int(input())
cor=[1]
ans=0
c=1
if is_all(s):
    print(n*k//2)
    exit()
i=0
while i<n-1:
    if s[i]==s[i+1]:
        cor[0]+=1
        i+=1
    else:
        i+=1
        break
while i<n-1:
    if s[i]==s[i+1]:
        cor.append(1)
        while i<n-1:
            if s[i]==s[i+1]:
                cor[-1]+=1
                i+=1
            else:
                break
    i+=1

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

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

if len(set(S)) == 1:
    print(len(S)*k//2)
else:
    ans = 0
    cnt = 1
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            cnt += 1
        else:
            ans += cnt//2
            cnt = 1
    if cnt > 1:
        ans += cnt//2
    ans = ans * k
    if S[0] == S[-1]:
        f, b = 0, 0
        for s in S:
            if s == S[0]:
                f += 1
            else:
                break
        for s in S[::-1]:
            if s == S[0]:
                b += 1
            else:
                break
        ans -= (f//2 + b//2 - (f+b)//2) * (k-1)
    print(ans)

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




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




if s[0] != s[-1]:
    cnt = 0
    bf_same = False
    tmp = 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            if tmp > 1:
                cnt += (tmp // 2) * k
                tmp = 1
    else:
        if tmp > 1:
            cnt += (tmp // 2) * k
        print(cnt)
    exit()






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


bt = 1
bt_idx = -1
for i in range(len(s) - 1, 0, -1):
    if s[i] == s[i - 1]:
        bt += 1
        bt_idx = i - 1
    else:
        break


cnt = 0
bf_same = False
tmp = 1
for i in range(tp_idx + 1, bt_idx - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    else:
        if tmp > 1:
            cnt += (tmp // 2) * k
            tmp = 1
else:
    if tmp > 1:
        cnt += (tmp // 2) * k



ans = cnt + tp // 2 + bt // 2 + ((tp + bt) // 2) * (k - 1)
print(ans)

Solution (Python):
s = input()
n = len(s)
flg = 0
ans1 = 0
ans2 = 0
for i in range(1,n):
  if s[i] == s[i-1] and flg == 0:
    flg = 1
    ans1 += 1
  else:
    flg = 0
s = s*2
n *= 2
flg = 0
for i in range(1,n):
  if s[i] == s[i-1] and flg == 0:
    flg = 1
    ans2 += 1
  else:
    flg = 0
k = int(input())
if len(set(list(s))) == 1:
  print(n*k//4)
elif ans2>ans1*2:
  print((k//2)*ans2+(k%2)*ans1+(k-1)//2)
else:
  print((k//2)*ans2+(k%2)*ans1)

Solution (Python):
from sys import stdin
import sys
import numpy as np
import collections


def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def rip(sep = ''):
    if sep == '' :
        return map(int, input().split()) 
    else: return map(int, input().split(sep))
def ria(sep = ''): 
    return list(rip(sep))
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()

def main():
    S = rs()
    K = ri()
    N = len(S)
    key = []
    cnt = []
    key.append(S[0])
    cnt.append(1)
    for i in range(1,N):
        if S[i] == key[len(key) - 1]:
            cnt[len(cnt) - 1] += 1
        else:
            key.append(S[i])
            cnt.append(1)

    if len(cnt) == 1:
        tot = N * K
        print(tot // 2)
        sys.exit(0)

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






if __name__ == "__main__":
    main()

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

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

count = 0
flag = True
for i in range(1, len(s)):
    if not flag:
        flag = True
        continue
    if s[i] == s[i-1]:
        count += 1
        flag = False

count2 = 0
flag = True
s = s*2
for i in range(1, len(s)):
    if not flag:
        flag = True
        continue
    if s[i] == s[i-1]:
        count2 += 1
        flag = False

print(count * k + (count2-count*2)*(k-1))

Solution (Python):
s=input()
k=int(input())
n=len(s)
cnt=1
ans=0
l=[]
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
  else:
    l.append(cnt)
    cnt=1
l.append(cnt)
if len(l)==1:
  print(k*sum(l)//2)
else:
  if s[0]!=s[n-1]:
    for i in range(len(l)):
      ans+=k*(l[i]//2)
  else:
    for i in range(1,len(l)-1):
      ans+=k*(l[i]//2)
    ans+=(k-1)*((l[0]+l[len(l)-1])//2)
    ans+=l[0]//2+l[len(l)-1]//2
  print(ans)

Solution (Python):
from itertools import groupby

def count(s):
    l = []
    c = 0
    for key,value in groupby(s):
        l.append(list(value))
    for i in l:
        c += len(i)//2
    return c
s = list(input())
k =int(input())
if k <= 10**5:
    print(count(s*k))
else:
    d2 = count(s*3)-count(s*2)
    d3 = count(s*4)-count(s*3)
    if d2 != d3:
        if k%2 == 1:
            print(d2*((k-2)//2+1) + count(s*2) + d3*((k-2)//2))
        else:
            print(d2*((k-2)//2) + count(s*2) + d3((k-2)//2))
    else:
        print(d2*(k-2) + count(s*2))

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

sames = 1
ans = 0
prev = 'prev'


for s in S:
	if prev == s:
		sames+=1
	else:
		if sames > 1:
			ans+=(sames//2)
			sames = 1
	prev = s
ans+=(sames//2)


add_f = 0
add_b = 0
if S[0] == S[len(S)-1]:
	for s in S:
		if s == S[0]:
			add_f+=1
		else:
			break
	for sr in reversed(S):
		if sr == S[len(S)-1]:
			add_b+=1
		else:
			break
add = add_f+add_b


if len(set(list(S))) == 1:
	print(K*len(S)//2)
else:
	print(ans*K + (add//2)*(K-1) - (add_f//2)*(K-1) - (add_b//2)*(K-1))

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

sames = 1
ans = 0
prev = 'prev'

for s in S:
	if prev == s:
		sames+=1
	else:
		if sames > 1:
			ans+=(sames//2)
			sames = 1
	prev = s
ans+=(sames//2)

add_f = 0
add_b = 0
if S[0] == S[len(S)-1]:
	for s in S:
		if s == S[0]:
			add_f+=1
		else:
			break
	for sr in reversed(S):
		if sr == S[len(S)-1]:
			add_b+=1
		else:
			break
add = add_f+add_b

if len(set(list(S))) == 1:
	print(K*len(S)//2)
else:
	print(ans*K + (add//2)*(K-1) - (add_f//2)*(K-1) - (add_b//2)*(K-1))

Solution (Python):
s=list(input())
k=int(input())
l=[]
p=1
for i in range(len(s)-1):

    if s[i]==s[i+1]:
        p+=1
    else:
        l.append(p)
        p=1
l.append(p)
m=0
if len(l)==1:
    print((l[0]*k)//2)

else:
    if s[len(s)-1]==s[0]:
        for i in range(1,len(l)-1):
            m+=k*(l[i]//2)
        m+=(k-1)*((l[0]+l[len(l)-1])//2)
        m+=l[0]//2+l[len(l)-1]//2
        print(m)

    else:
        for i in range(len(l)):
            m+=k*(l[i]//2)
        print(m)

Solution (Python):
s, k = input(), int(input())
r = 0
f = True
fir = 0
las = 0

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

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

i = 1
tmp = 1
while i < len(s):
    if s[i] == s[i - 1]:
        tmp += 1
    else:
        f = False
        r += tmp // 2
        tmp = 1
    i += 1
r += tmp // 2

if f:
    print(len(s) * k // 2)
else:
    print(r * k - (k - 1) * (fir // 2 + las // 2 - (fir + las) // 2))

Solution (Python):
s=input()
k=int(input())
if len(s)==1:
	print(k//2)
	exit()
cnt=0
same_length=1
first=1
for i in range(len(s)-1):
 		 if s[i]==s[i+1]:
 		 	same_length+=1
 		 	if cnt==0:first+=1
 		 elif s[i]!=s[i+1]:
 		 	if cnt==0:s_left=first
 		 	cnt+=int(same_length//2)
 		 	same_length=1
if same_length==len(s):
	print(len(s)*k//2)
	exit()
s_right=same_length
cnt+=int(same_length//2)
if s[0]==s[-1] and s_left%2==1 and s_right%2==1:
	print(cnt*k+k-1)
else:
	print(cnt*k)

Solution (Python):
from itertools import groupby
S = input()
K = int(input())
A = list(map(lambda x: (x[0], len(list(x[1]))), groupby(S)))
a1 = A[0]
a2 = A[-1]
if len(A) == 1:
    print(len(S) * K // 2)
elif a1[0] != a2[0]:
    print(K * sum(x // 2 for _, x in A))
else:
    print(K * sum(x // 2 for _, x in A) - (K - 1) * (a1[1] // 2 + a2[1] // 2 - (a1[1] + a2[1]) // 2))

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

def merge(A, B):
    res = [[inf]*26 for _ in range(26)]
    for i in range(26):
        for j1 in range(26):
            for j2 in range(26):
                if j1 == j2:
                    continue
                for k in range(26):
                    res[i][k] = min(res[i][k], A[i][j1]+B[j2][k])
    return res
S = list(map(lambda x: ord(x) - 97, readline().strip()))
K = int(readline())
inf = 10**18+13
dp = [None]*26

for st in range(26):
    Dp = [inf]*26
    if st == S[0]:
        Dp[st] = 0
    else:
        Dp[st] = 1
    for s in S[1:]:
        Dp2 = [inf]*26
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                if s != i:
                    Dp2[i] = min(Dp2[i], Dp[j] + 1)
                else:
                    Dp2[i] = min(Dp2[i], Dp[j])
        Dp = Dp2[:]
    dp[st] = Dp[:]

stack = [dp]

for _ in range(32):
    stack.append(merge(stack[-1], stack[-1]))

E = [[0]*26 for _ in range(26)]
for i in range(32):
    if (1<<i) & K:
        E = merge(E, stack[i])
print(min(min(d) for d in E))

Solution (Python):
S = list(input().rstrip("\n"))
K = int(input())


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

from itertools import groupby


init = 0
for _,group in groupby(S):
    tmp = sum(1 for _ in group)
    if tmp >= 2:
        init += tmp // 2

if K == 1:
    print(init)
    exit()


com_diff = 0
for _ ,group in groupby(S+S):
    tmp = sum(1 for _ in group)
    if tmp >= 2:
        com_diff += tmp // 2
com_diff -= init

print(init + (K-1)*com_diff)

Solution (Python):
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif len(list(set(a)))==1:
  print((len(a)*k)//2)
else:  
  tmp=a[0]
  count1=0
  count2=0
  l=0
  while a[l]==tmp:
    count1+=1
    l+=1
  j=-1
  while a[j]==tmp:
    count2+=1
    j-=1
    
  for i in range(len(a)-1):
    if a[i]==a[i+1]:
      a[i+1]='1'
      ans+=1
  if a[0]==a[-1]:
    if count1%2==0:
      print(ans*k)
    else:
      print(ans*k+k-1)
  else:
    print(ans*k)

Solution (Python):

S = input()
K = int(input())
if len(list(set(S))) == 1:
    print(len(S) * K // 2)
    exit()
l = []
SS = S * 2
s = SS[0]
cnt = 1
for i in range(1, len(SS)):
    if s != SS[i]:
        l.append(cnt)
        s = SS[i]
        cnt = 1
    else:
        cnt += 1
l.append(cnt)
ans = 0
if S[0] == S[-1]:
    for i in range(len(l)//2 + 1):
        
        
        if i == 0:
            ans += l[i] // 2
        elif i == len(l) // 2:
            ans += l[i] // 2 * (K-1)
        else:
            ans += l[i] // 2 * K
    ans += l[-1] // 2
    
else:
    
    for i in range(len(l)//2):
        ans += l[i] // 2 * K
print(ans)

Solution (Python):
s = input()
k = int(input())
l = []
cnt = 0
for i in range(len(s)-1):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0
else:
  cnt += 1
  l.append(cnt)

ans = 0
if len(set(list(s))) == 1:
  print(len(s)*k//2)
else:
  for i in range(len(l)):
    ans += l[i] // 2
  if s[0] != s[-1]:
    print(ans*k)
  else:
    fans = (k - 1) * ((l[0] + l[-1]) // 2) + ans
    ans = 0
    for i in range(1,len(l)-1):
      ans += l[i] // 2
    fans += (k - 1) * ans
    print(fans)

Solution (Python):
S = list(input())
K = int(input())
cnt = 0
start = S[0]
for i in range(len(S)):
    if start != S[i]:
        break
    if i == len(S) - 1:
        print(len(S) * K // 2)
        exit()
ans = 0
now = start
for i in range(len(S)):
    if i == len(S) - 1:
        if now == S[i]:
            cnt += 1
        ans += cnt // 2
    else:
        if S[i] != now :
            ans += cnt // 2
            cnt = 1
            now = S[i]
        else:
            cnt += 1

end = S[-1]
start_cnt = 0
end_cnt = 0
for c in S:
    if c == start:
        start_cnt += 1
    else:
        break
for i in range(len(S) - 1 , - 1, -1):
    if S[i] == end:
        end_cnt += 1
    else:
        break
add = 0
if end_cnt % 2 == 1 and start_cnt % 2 == 1 and start == end:
    add = (K - 1)
print(ans * K + add)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = readline().rstrip()
K = int(read())

def solve_small(S,K):
    T = S * K
    prev = 0
    cnt = 0
    for x in T:
        if x != prev:
            prev = x
            continue
        else:
            prev = 0
            cnt += 1
    return cnt

A = [solve_small(S,k) for k in [0,1,2,3,4,5]]

if K <= 5:
    answer = A[K]
else:
    if K&1:
        d = A[5] - A[3]
        answer = A[5] + (K-5)//2 * d
    else:
        d = A[4] - A[2]
        answer = A[4] + (K-4)//2 * d

print(answer)

Solution (Python):
from itertools import groupby
S = input()
k = int(input())

seq = [(s, len(list(k))) for s, k in groupby(S)]

if len(seq) == 1:
    ans = (k * len(S)) // 2
else:
    if seq[0][0] != seq[-1][0]:
        ans = k*sum(a[1] // 2 for a in seq)
    else:
        left = seq[0][1]
        right = seq[-1][1]
        ans = k*sum(a[1] // 2 for a in seq[1:-1])
        ans += ((left + right) // 2) * (k - 1)
        ans += left // 2 + right // 2

print(ans)

Solution (Python):
def counter(s):
    res, cnt = 0, 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
        else:
            res += cnt // 2
            cnt = 1
    res += cnt // 2
    return res


def solve():
    S = list(input())
    K = int(input())
    
    N = len(S)
    if len(set(S)) == 1:
        print(N * K // 2)
        exit()
    
    a = counter(S)
    b = counter(S + S)
    print(a + (K - 1) * (b - a))


if __name__ == '__main__':
    solve()

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):
from itertools import groupby
S = list(input())
K = int(input())
newS = list([list(g) for k, g in groupby(S)])
change = 0
for i in range(len(newS)):
    change += len(newS[i])//2
chain = 0
if len(newS) == 1:
    print(K*len(S)//2)
else:
    if len(newS) > 2 and newS[0] == newS[len(newS)-1]:
        chain = 1
    print(change*K+chain*(K-1))

Solution (Python):
import re
from collections import Counter
s = input()
k = int(input())
len_s = len(s)
res = re.findall(r"(.)(\1{0,})", s)
res = [i[0] + i[1] for i in res]

INF = 10**18
best = INF
ct = Counter(s)

if len(res) == 1:
    print(len_s * k // 2)
else:
    score = 0
    if res[0][0] == res[-1][0]:
        score += len(res[0] + res[-1]) // 2 * (k-1) + len(res[0]) // 2 + len(res[-1]) // 2
        res.pop(0)
        res.pop(-1)
    for r in res:
        score += k * (len(r) // 2)
    print(score)

Solution (Python):
def count(st):
    n=len(st)
    tmp=1
    res=[]
    for i in range(n-1):
        if st[i]==st[i+1]:
            tmp+=1
        else:
            res.append(tmp)
            tmp=1
    res.append(tmp)
    return res
S=input()
K=int(input())
T=S*3
N=len(S)
if N==1:
    print(K//2)
    exit()
if K<=2:
    Y=count(S*K)
    ans=0
    for i in Y:
        ans+=i//2
    print(ans)
    exit()
if S.count(S[0])==N:
    print((N*K)//2)
    exit()
X=[]
if S[0]==S[-1]:
    for j in range(1,N):
        if S[j-1]!=S[j]:
            break
    tmp=1
    for i in range(j,j+N-1):
        if T[i]==T[i+1]:
            tmp+=1
        else:
            X.append(tmp)
            tmp=1
    X.append(tmp)
else:
    X=count(S)

cnt=0
for i in X:
    cnt+=i//2
Y=count(T)
cnt2=0
for i in Y:
    cnt2+=i//2
print(cnt*(K-3)+cnt2)

Solution (Python):
def solve(s, k):
    n = len(s)
    s = list(s)
    if len(set(s)) == 1:
        return n*k//2
    a = 0
    for i in range(n):
        if s[0] == s[i]:
            a += 1
        else:
            break
    b = 0
    for i in range(n):
        if s[-1] == s[-1-i]:
            b += 1
        else:
            break
    c = 0
    for i in range(a, n-b-1):
        if s[i] == s[i+1]:
            c += 1
            s[i+1] = "."
    res = a//2 + b//2 + c*k
    if s[0] == s[-1]:
        res += ((a+b)//2) * (k-1)
    else:
        res += (a//2 + b//2) * (k-1)
    return res

s = input()
k = int(input())
print(solve(s, k))

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

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

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

if S[0] == S[-1]:
  ans += ((dup[0]+dup[-1])//2 - (dup[0]//2+dup[-1]//2))*(K-1)

print(ans)

Solution (Python):

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

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

dup = [1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    dup[-1]+=1
  else:
    dup.append(1)
    
ans = 0
for i in dup:
  ans += (i//2)*k

if s[0]==s[-1]:
  ans += ((dup[0]+dup[-1])//2 - (dup[0]//2+dup[-1]//2))*(k-1)

print(ans)

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

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

t=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    t[-1]+=1
  else:
    t.append(1)
    
ans=0
for i in t:
  ans+=(i//2)*k

if s[0]==s[-1]:
  ans+= ((t[0]+t[-1])//2 - (t[0]//2+t[-1]//2))*(k-1)

print(ans)

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

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


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


a = count(S)
b = count(S + S)
print(a + (K - 1) * (b - a))

Solution (Python):
from itertools import groupby

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

s = [ len(list(j)) for i, j in groupby(S)]
ans = 0
for i in s:
    if i >= 2:
        ans += i // 2
ans *= K
if S[0] == S[-1] and s[0] % 2 == s[-1] % 2 == 1:
    ans += K - 1

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

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())
R = int(input())
lengths = []
same = False
All = False
odd = False
sl = 1
Sum = 0
if S[0] == S[len(S)-1]:
  same = True
S.append('0')
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    sl += 1
  else:
    lengths.append(sl)
    sl = 1
if len(lengths) == 1:
  All = True
if lengths[0]%2 == 1 and lengths[len(lengths)-1]%2 == 1:
  odd = True
if same:
  if All:
    Sum += (lengths[0]*R)//2
  else:
    temp = lengths[0]
    lengths[0] = 0
    lengths[len(lengths)-1] += temp
    if odd:
      for l in range(len(lengths)):
        Sum += (lengths[l]//2)*R
      Sum -= 1
    else:
      for g in range(len(lengths)):
        Sum += (lengths[g]//2)*R
else:
  for j in range(len(lengths)):
    Sum += (lengths[j]//2)*R
print(Sum)

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

S = S + S + S
N = len(S)

a = 0
b = 0
c = 0
for i in range(1, N) : 
    if S[i] == S[i-1] :
        if i < N // 3 :
            a += 1
        elif i < N // 3 * 2 :
            b += 1
        else :
            c += 1
        S[i] = '
        
ret = a + K // 2 * b + (K - K // 2 - 1) * c
print(ret)

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

import itertools
if len(set(S))==1:
  print((len(S)*K)//2)
elif S[0]!=S[-1]:
  ans=0
  for key,g in itertools.groupby(S):
    ans+=len(list(g))//2
  print(ans*K)
else:
  first=0
  last=0
  ans=0
  isfirst=True
  for key,g in itertools.groupby(S):
    num=len(list(g))
    if isfirst:
      first=num
      isfirst=False
    else:
      last=num
      ans+=num//2
  ans-=num//2
  print(ans*K+((first+last)//2)*(K-1)+first//2+last//2)

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

    li = []
    same_str_num = 1
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            same_str_num += 1
        else:
            li.append(same_str_num)
            same_str_num = 1
    li.append(same_str_num)

    if len(li) == 1:
        print(li[0]*k//2)
        return

    if s[0] == s[-1]:
        ans = li[0]//2 + li[-1]//2
        ans += ((li.pop(0) + li.pop())//2) * (k-1)
    else:
        ans = 0
    for i in li:
        ans += (i//2)*k
    print(ans)

if __name__ == '__main__':
    main()

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

    li = []
    same_str_num = 1
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            same_str_num += 1
        else:
            li.append(same_str_num)
            same_str_num = 1
    li.append(same_str_num)

    if len(li) == 1:
        print(li[0]*k//2)
        return

    if s[0] == s[-1]:
        ans = li[0]//2 + li[-1]//2
        ans += ((li.pop(0) + li.pop())//2) * (k-1)
    else:
        ans = 0
    for i in li:
        ans += (i//2)*k
    print(ans)






if __name__ == '__main__':
    main()

Solution (Python):
s=input()
k=int(input())
t=list(s)
ans=0

if(len(set(list(s)))==1):
    ans=(len(s)*k)//2
elif(s[0] != s[-1]):
    for i in range(len(t)-1):
        if(t[i] == t[i+1]):
            ans+=1
            t[i+1]="*"
    ans*=k
else:
    s_first=s[0]
    first=0
    end=len(s)-1

    while(s[first] == s_first):
        first+=1

    while(s[end] == s_first):
        end-=1

    len_first=first
    len_end=len(s[end+1:])

    rest_s=list(s[first:end+1])
    rest_ans=0
    for i in range(len(rest_s)-1):
        if(rest_s[i] == rest_s[i+1]):
            rest_ans+=1
            rest_s[i+1]="*"

    ans+=len_first//2
    ans+=rest_ans*k
    ans+=((len_first+len_end)//2)*(k-1)
    ans+=len_end//2
print(ans)

Solution (Python):
s=str(input())
k=int(input())
li=[1]
ans=0
if len(set(s))==1:
    print(len(s)*k//2)
    exit()
for i in range(1,len(s)):
    if s[i]==s[i-1]:
        li[-1]+=1
    else:
        li.append(1)

for j in range(len(li)):
    ans+=int(li[j]//2)*k

if s[0]==s[-1] and li[0]%2==li[-1]%2==1:
    ans+=k-1

print(ans)

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

tmp = '0'
count = 0
for x in s:
	if tmp == x:
		count += 1
		tmp = '0'
	else:
		tmp = x
if tmp == s[0]:
	start_count = 0
	for x in s:
		if tmp != x:
			break
		start_count += 1
	if start_count == len(s):
		print((len(s)*k)//2)
	else:
		print((count+start_count%2)*k-start_count%2)
else:
	print(count*k)

Solution (Python):
import math
S = list(input())
T = int(input())
prev = S[0]
same_cnt = 0
renzoku = []
for i in range(len(S)):
    if S[i] == prev:
        same_cnt += 1
        if i == len(S)-1:
            renzoku.append(same_cnt)
    else:
        prev = S[i]
        renzoku.append(same_cnt)
        same_cnt = 1
        if i == len(S)-1:
            renzoku.append(same_cnt)

if S[0] != S[-1]:
    print(T*sum(list(map(lambda x: math.floor(x/2),renzoku))))
else:
    if len(renzoku) != 1:
        linked_renzoku = renzoku[:]
        tail = linked_renzoku[-1]
        linked_renzoku[0] += tail
        linked_renzoku.pop()
        
        print((T-1)*sum(list(map(lambda x: math.floor(x/2),linked_renzoku))) + sum(list(map(lambda x: math.floor(x/2),renzoku))))
    else:
        print(math.floor((T*len(S))/2))

Solution (Python):
s=input()
k=int(input())
if len(set(list(s)))==1:
    print((len(s)*k)//2)
    exit()
t=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:
      t[-1]+=1
  else:
      t.append(1)
a=0
for i in t:
    a+=(i//2)*k
if s[0]==s[-1]:
  if t[0]%2==t[-1]%2==1:
      a+=k-1
print(a)

Solution (Python):


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

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

if flag and K>1:
    S*=2
    for i in range(len(S)-1):
        if S[i+1]==S[i]:
            S[i+1]="?"
    temp=S.count("?")
    print(K//2*temp+(temp//2 if K%2==1 else 0))
    exit()

if K==1:
    for i in range(len(S)-1):
        if S[i+1]==S[i]:
            S[i+1]="?"
    print(S.count("?"))
    exit()

n=len(S)
S*=4
N=len(S)-1

for i in range(N):
    if S[i+1]==S[i]:
        S[i+1]="?"

l=[S[:n],S[n:2*n],S[2*n:3*n],S[3*n:]]

ans=[0]*4
for i in range(4):
    ans[i]=l[i].count("?")

print(ans[0]+ans[3]+ans[1]*(-(-(K-2)//2))+ans[2]*((K-2)//2))

Solution (Python):





from math import floor

s = input()
k = int(input())
if len(set(list(s))) == 1:
    print(floor((len(s) * k) / 2))
    exit()

if s[0] == s[-1]:
    
    
    pre, suf = 0, 0
    for c in s:
        if c == s[0]:
            pre += 1
        else:
            break
    for c in s[::-1]:
        if c == s[-1]:
            suf += 1
        else:
            break
    
    idx = 0
    cc = 0
    t = s[pre : len(s) - suf]
    while idx < len(t):
        c = 0
        tmp = idx
        while tmp < len(t) and t[tmp] == t[idx]:
            tmp += 1
            c += 1
        if c > 1:
            cc += floor(c / 2)
        idx = tmp
    ans = floor(pre / 2) + cc * k + floor((pre + suf) / 2) * (k - 1) + floor(suf / 2)
    print(ans)
else:
    idx = 0
    cc = 0
    t = s[:]
    while idx < len(t):
        c = 0
        tmp = idx
        while tmp < len(t) and t[tmp] == t[idx]:
            tmp += 1
            c += 1
        if c > 1:
            cc += floor(c / 2)
        idx = tmp
    ans = cc * k
    print(ans)

Solution (Python):

import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

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

    count = 0
    same = 1
    flag = False 
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if not flag: 
                first_same = same
                flag = True 
                
                
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            
            count += same // 2 + 1
            
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import sys

def main():
    s, k = sys.stdin.read().split()
    k = int(k)

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

    count = 0
    same = 1
    flag = False
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            same += 1
        else:
            if not flag: 
                first_same = same
                flag = True 
                
            count += same // 2
            same = 1
    
    bl = False
    if s[-1] == s[0]:
        if same % 2 == 1 and first_same % 2 == 1:
            
            count += same // 2 + 1
            
            bl = True
        else:
            count += same // 2
    else:
        count += same // 2

    ans = count * k
    if bl:
        ans -= 1

    print(ans)

if __name__ == '__main__':
    main()