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 = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k / 2
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()


if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left // 2) + (mid // 2) * (k - 1) + (right // 2)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0

    print ans
    exit()
else:
    ans = 0
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    ans += (sz // 2)
    sz = 0

    ans *= k

    print ans
    exit()

Solution (Python):
from copy import deepcopy
s = list(input())
K = int(input())
dummy = "A"
S = deepcopy(s)
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    replace_cnt = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = dummy
            replace_cnt += 1
    if S[0] != S[-1]:
        print(replace_cnt*K)
    else:
        S = s+s
        replace_cnt_two = 0
        for i in range(1, len(S)):
            if S[i] == S[i-1]:
                S[i] = dummy
                replace_cnt_two += 1
        diff = replace_cnt_two - replace_cnt
        print(diff*(K-1)+replace_cnt)

Solution (Python):
S = input() + 'Z'
K = int(input())

n = 0
x = S[0]
T = []

for s in S:
    if s == x:
        n += 1
    else:
        x = s
        if n != 0:
            T.append(n)
            n = 1

if len(T) == 1:
    ans = (T[0]*K) // 2
elif  K==1 or S[0]!=S[-2]:
    ans = K * sum([t//2 for t in T])
else:
    ans = K * sum([t//2 for t in T[1:-1]]) + T[0]//2 + T[-1]//2 + (K-1)*((T[0]+T[-1])//2)
    
print(ans)

Solution (Python):
import collections
import sys

input = sys.stdin.readline


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

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

        k1 = self.connection_and_disconnection(S)
        k2 = self.connection_and_disconnection(S * 2)

        ans = k1 + (k2 - k1) * (K - 1)
        print(ans)

    def connection_and_disconnection(self, S: str):
        S = S + '$'
        cont_same_letter = 1
        need_to_change = 0
        for i in range(len(S) - 1):
            if S[i] == S[i + 1]:
                cont_same_letter += 1
            else:
                need_to_change += cont_same_letter // 2
                cont_same_letter = 1
        return need_to_change



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

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


lis = []
i = 0
cnt = 1

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

res = 0

if len(lis) == 1:
    res += (lis[0]*k)//2
else:
    if s[0] == s[len(s)-1]:
        for a in lis[1:-1]:
            res += ((a)//2)*k
        res += ((lis[0])//2)*1
        res += ((lis[-1])//2)*1
        res += ((lis[0]+lis[-1])//2)*(k-1)
    else:
        for a in lis:
            res += ((a)//2)*k

print(res)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

S = input()
K = int(input())
ele, num = get_sequence(S)
if len(ele) == 1:
    print(K*len(S)//2)
else:
    if ele[0]==ele[-1]:
        ans = num[0]//2 + num[-1]//2
        ans += sum(map(lambda x: x//2, num[1:-1]))*K
        ans += (num[0]+num[-1])//2 * (K-1)
    else:
        ans = sum(map(lambda x: x//2, num))*K
    print(ans)

Solution (Python):
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

S = input()
K = int(input())
ele, num = get_sequence(S)
if len(ele) == 1:
    print(K*len(S)//2)
elif len(ele) == 2:
    print((num[0]//2 + num[1]//2)*K)
else:
    if ele[0]==ele[-1]:
        ans = num[0]//2 + num[-1]//2
        ans += sum(map(lambda x: x//2, num[1:-1]))*K
        ans += (num[0]+num[-1])//2 * (K-1)
    else:
        ans = sum(map(lambda x: x//2, num))*K
    print(ans)

Solution (Python):

def solve():
    ans=0;f=0
    s=input();n=len(s);k=int(input())
    if len(s)==1:
        return k//2
    for i in range(n-1):
        if s[i]!=s[i+1]:
            break
    else:return (n*k)//2
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2
        ans+=count//2
    else:
        ans+=count//2*k
        
        
    i=j
    while i<n-1:
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        ans+=count//2*(k)
    return ans

print(solve())

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

from collections import Counter
l=Counter(s)
if len(l)==1:
    print(len(s)*k//2)
    exit()

f=1
ans=0
for i in range(len(s)-1):
    if f and s[i]==s[i+1]:
        ans+=1
        f=0
    else:
        f=1

if s[0]!=s[-1]:
    print(ans*k)
else:
    ans2=0
    ss=s+s
    f=1
    for i in range(2*len(s)-1):
        if f and ss[i]==ss[i+1]:
            ans2+=1
            f=0
        else:
            f=1
    print(ans+(ans2-ans)*(k-1))

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

from collections import Counter
l=Counter(s)
if len(l)==1:
    print(len(s)*k//2)
    exit()

f=1
ans=0
for i in range(len(s)-1):
    if f and s[i]==s[i+1]:
        ans+=1
        f=0
    else:
        f=1

if s[0]!=s[-1]:
    print(ans*k)
else:
    ans2=0
    ss=s+s
    f=1
    for i in range(2*len(s)-1):
        if f and ss[i]==ss[i+1]:
            ans2+=1
            f=0
        else:
            f=1
    print(ans+(ans2-ans)*(k-1))

Solution (Python):
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += int(a[i]/2)
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k/2))
else:
    for i in range(1,d-1):
        c += int(a[i]/2)
    c=c*k
    c += (k-1)*int((a[0]+a[d-1])/2) + int(a[0]/2) + int(a[d-1]/2)
    print(int(c))

Solution (Python):
def opeCounter(S, K):
    T = S * K
    c = 0
    i = 1
    while i < len(T):
        if T[i-1] == T[i]:
            c += 1
            i += 1
        i += 1
    return c

def agc039a(S, K):
    s1 = opeCounter(S, 1)
    s2 = opeCounter(S, 2)
    s3 = opeCounter(S, 3)
    d1 = s2 - s1
    d2 = s3 - s2

    ans = 0
    if d1 == d2: ans = s1 + d1 * (K-1)
    else:
        if(K%2 != 0): ans = s1 + (d1+d2) * ((K+2-1)//2-1) 
        else: ans = s2 + (d1+d2) * (K//2-1)
    return ans

S = input()
K = int(input())
print(agc039a(S, K))

Solution (Python):
from itertools import groupby

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

Solution (Python):
from itertools import groupby

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

Solution (Python):
from itertools import groupby
from functools import reduce
from operator import xor

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

Solution (Python):
from itertools import groupby

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

Solution (Python):
S = input()
N = int(input())
T = [S*i + "
F = [0]*4
for i in range(len(T)) :
    t = T[i]
    acc = 0
    c = 1
    for j in range(1,len(t)) :
        if t[j] == t[j-1] :
            c += 1
        else :
            acc += c//2
            c = 1
    F[i] = acc

if N <= 4 :
    ans = F[N-1]
elif N % 2 == 1 :
    d = F[2] - F[0]
    ans = F[2] + d * ((N-3)//2)
else :
    d = F[3] - F[1]
    ans = F[3] + d * ((N-4)//2)

print(ans)

Solution (Python):
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print(k*n//2)
else:
    ans1 = 0
    i = 0
    while i+1<n:
        if s[i%n]==s[(i+1)%n]:
            i+=1
            ans1+=1
        i+=1
    ans2 = 0
    i = 0
    while i+1<2*n:
        if s[i%n]==s[(i+1)%n]:
           i+=1
           ans2+=1
        i+=1
    print(ans1+(ans2-ans1)*(k-1))

Solution (Python):
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print(k*n//2)
else:
    ans1 = 0
    i = 0
    while i+1<n:
        if s[i%n]==s[(i+1)%n]:
            i+=1
            ans1+=1
        i+=1
    ans2 = 0
    i = 0
    while i+1<2*n:
        if s[i%n]==s[(i+1)%n]:
           i+=1
           ans2+=1
        i+=1
    print(ans1+(ans2-ans1)*(k-1))

Solution (Python):
def main():

    S = input()
    K = int(input())
    count = []

    ct = 1
    for i in range(1, len(S)):
        if(S[i] == S[i-1]): ct += 1
        else:
            count.append(ct)
            ct = 1
    count.append(ct)

    v = 0
    for c in count:
        v += c // 2

    if(len(count) == 1):
        return K*len(S) // 2
    elif S[0] == S[-1]:
        return K*v + (K-1)*((count[0]+count[-1])//2 - count[0]//2 - count[-1]//2)
    else:
        return K*v

if __name__ == '__main__':
    print(main())

Solution (Python):
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0

if all([S[i] == S[0] for i in range(1,len(S))]):
    print((len(S)*K)//2)
    exit()
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1

print(tmp+(tmp2-tmp)*(K-1))

Solution (Python):
import re

def main(s):
    
    n = 2
    
    reg = re.compile("(.)\\1{%d,}" %(n-1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]


if __name__ == "__main__":
    s = input()
    K = int(input())

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

    t = list(main(s))

    
    if len(t) == 1 and len(t[0]) == len(s):
        print((len(s) * K) // 2)
        exit()

    
    ans1 = 0
    if len(s) >= 3 and s[0] == s[-1]:
        if len(t) >= 2 and t[0][0] == s[0] and t[-1][0] == s[-1]:
            ans1 = len(t[0]) // 2 + len(t[-1]) // 2 - ((len(t[0]) + len(t[-1])) // 2)
            ans1 *= K-1
        
        elif len(t) >= 1 and t[0][0] != s[0]:
            ans1 = len(t[-1]) // 2 - ((1 + len(t[-1])) // 2)
            ans1 *= K-1
        
        elif len(t) >= 1 and t[-1][0] != s[-1]:
            ans1 = len(t[0]) // 2 - ((len(t[0] + 1)) // 2)
            ans1 *= K-1
        
        else:
            ans1 = -1 * (K-1)

    
    ans2 = 0
    for i in t:
        ans2 += len(i) // 2
    ans2 *= K

    ans = -ans1 + ans2
    print(ans)

Solution (Python):
def calc(S, T):
    U = [(S[0], 1)]
    un = 0
    for i in range(1, len(S)):
        if U[un][0] == S[i]:
            U[un] = (U[un][0], U[un][1] + 1)
        else:
            U += [(S[i], 1)]
            un += 1
    if un == 0:
        return (U[0][1] * T) // 2
    else:
        if U[0][0] != U[un][0]:
            return sum([u[1] // 2 for u in U]) * T
        else:
            return U[0][1] // 2 + U[un][1] // 2 \
                + (U[0][1] + U[un][1]) // 2 * (T - 1) \
                + sum([u[1] // 2 for u in U[1:un]]) * T


S = input()
T = int(input())
print(calc(S, T))

Solution (Python):
a = [input() for i in range(2)]

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

if ren == []:
  ren = [1]
  
ans = 0
if len(ren) == 1:
  ans = ren[0] * iter // 2
elif text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Solution (Python):
import copy

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

def count(L):
    cnt = 0
    for i in range(len(L)-1):
        if L[i] == L[i+1]:
            L[i+1] = "1"
            cnt += 1
    return cnt

T = copy.copy(S)
T.insert(0, T[len(T)-1])
cnt_T = count(T)

if T[-1] != "1":
    ans = count(S) + (cnt_T * (K-1))
else:
    TT = S * 2
    cnt_TT = count(TT)
    if K % 2 == 0:
        ans = cnt_TT * (K // 2)
    else:
        ans = (cnt_TT * (K // 2)) + count(S)

print(ans)

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string



def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7



show_flg=False
show_flg=True

def naive(b,k):
    a=b*k
    n=len(a)
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
            
    return t

b=StoI()
k=I()

if len(b)*k<10:
    print(naive(b,k))
    exit()

odd=naive(b,3)-naive(b,1)
even=naive(b,4)-naive(b,2)
if k%2==1:
    ans=naive(b,1)+(k//2)*odd
else:
    ans=naive(b,2)+(k//2)*even-even
print(ans)

Solution (Python):
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string



def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7



show_flg=False
show_flg=True

def naive(b,k):
    a=b*k
    n=len(a)
    
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
            
    return t


b=StoI()
a=b
k=I()
n=len(a)
p=a[0]
t=0

if len(b)*k<10:
    print(naive(b,k))
    exit()


for i in range(n-1):
    if p==a[i+1]:
        t+=1
        p=-1
    else:
        p=a[i+1]

odd=naive(b,3)-naive(b,1)
even=naive(b,4)-naive(b,2)
if k%2==1:
    ans=naive(b,1)+(k//2)*odd
else:


    ans=naive(b,2)+(k//2)*even-even
print(ans)

Solution (Python):
S,K=open(0);K=int(K);s=1;l=[]
for c,d in zip(S,S[1:]):
 if c==d:s+=1
 else:l+=[s];s=1
if len(l)==1:print(l[0]*K//2)
elif S[0]==S[-2]:print(K*sum(i//2for i in l[1:-1]+[l[0]+l[-1]])-(l[0]&l[-1]&1))
else:print(K*sum(i//2for i in l))

Solution (Python):
import sys

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

flag = 0
c = S[0]
for i in range(len(S) - 1):
    if c != S[i + 1]:
      flag = 1

if flag == 0:
  print(len(S) * K // 2)
  sys.exit()

countA = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA += 1
        S[i + 1] = 'A'
 

countB = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB += 1
        T[i + 1] = 'A'
tmp = countB - (2*countA)
print((countA * K) + (tmp * (K - 1)))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    s = S()
    k = II()
    ans = 0
    lis = []
    i = 0
    tmp = 1
    while len(s) > i + 1:
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            lis.append((s[i], tmp))
            tmp = 1
        i += 1
    lis.append((s[i], tmp))
    if lis[0][0] == lis[-1][0]:
        if len(lis) == 1:
            ans += k * lis[0][1] // 2
            print(ans)
            return
        ans -= (lis[0][1] // 2 + lis[-1][1] // 2) * (k - 1)
        ans += (lis[0][1] + lis[-1][1]) // 2 * (k - 1)
    for _, b in lis:
        ans += b // 2 * k
    print(ans)
    

    return


def B():
    return


def C():
    return


def D():
    return


def E():
    return


def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    A()

Solution (Python):






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

def monomer(A): 
    N=0
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]="?"
            N+=1
    return N

n=monomer(S) 
if S[0]!=S[-1]: 
    print(n*K)


else:
    WS=list(s*2) 
    WS.insert(0,WS[-1]) 
    wn=monomer(WS)
    SS=list(s)
    SS.insert(0,SS[-1])
    nn=monomer(SS)
    print(n+wn*((K-1)//2)+nn*((K-1)%2))

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

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

count = 1
i = 0
sum = 0
while(1):
    if S[i] == S[i+1]:
        count += 1
        i += 1
    else:
        sum += count//2
        count = 1
        i += 1
    if i == len(S)-1:
        break

sum += count//2 




j = 0 
a = 1 
while(1):
    if S[j] == S[j+1]:
        a += 1
        j += 1
    else:
        break
    if j == len(S) -1:
        break



m = len(S)-1 
b = 1 
while(1):
    if S[m] == S[m-1]:
        m -= 1
        b += 1
    else:
        break
    if m == 0:
        break




if a == len(S): 
    print((len(S)* K)//2)
elif S[len(S)-1] == S[0]:
    print( sum * K + ((a+b)//2 - a//2 - b //2 ) * ( K -1))
else:
    print( sum * K)

Solution (Python):
a = lambda x: x//2

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

    l = len(S)
    hoge = [1]+[0]*(l-1)
    for i in range(1,l):
        if S[i] == S[i-1]:
            hoge[i] = hoge[i-1] + 1
            hoge[i-1] = 0
        else:
            hoge[i] = 1
    e = list(filter(lambda x: x,hoge))

    if len(e) == 1:
        return a(e[0]*K)
    elif S[0] == S[-1]:
        beg = e[0]
        end = e[-1]
        e = e[1:-1]
        return a(beg) + sum(map(a,e))*K + a(beg+end)*(K-1) + a(end)
    else:
        return sum(map(a,e))*K

print(main())

Solution (Python):
s = str(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):
def count(S):
    ans = 0
    i = 1
    while i < len(S):
        if S[i-1] == S[i]:
            i += 1
            ans += 1
        i += 1
    return ans

def first_last_count(S):
    first = 1
    c = S[0]
    for i in range(1, len(S)):
        if S[i] == c:
            first += 1
        else:
            break

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

    return (first//2) + (last//2) - ((first+last)//2)

def is_same(S):
    c = S[0]
    for i in range(1, len(S)):
        if S[i] != c:
            return False
    return True

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

    if is_same(S):
        print((len(S) * K) // 2)
        exit()

    ans = count(S)
    ans *= K
    if S[-1] == S[0]:
        ans -= first_last_count(S) * (K-1)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
S=input()
S_=S+"A"
K=int(input())
i=0
cnt=0
lst=[]
while i<len(S_)-1:
    if not S_[i] == S_[i+1]:
        cnt+=1
        lst.append(cnt)
        cnt=0
        i+=1
    else:
        i+=1
        cnt+=1
if len(lst)==1:
    print((lst[0]*K)//2)
elif S[0]!=S[-1]:
    ans=0
    for j in lst:
        ans+=j//2
    print(ans*K)
else:
    ans_1=0
    ans_2=0
    for k in range(len(lst)):
        ans_1+=lst[k]//2
    for k in range(len(lst)):
        if k==0:
            ans_2+=lst[k]//2
        elif k==len(lst)-1:
            ans_2+=lst[k]//2
            ans_2+=(lst[k]+lst[0])//2
        else:
            ans_2+=(lst[k]//2)*2
    print(ans_1+(ans_2-ans_1)*(K-1))

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

def input(): return sys.stdin.readline()[:-1]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def MATINT(h): return [list(map(int, input().split())) for _ in range(h)]
def MATSTR(h): return [input() for _ in range(h)]
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()
K = INT()

N = len(S)
numlist = []
count = 1

for i in range(1, N):
    if S[i] == S[i - 1]:
        count += 1
    else:
        numlist.append(count)
        count = 1
numlist.append(count)
if len(numlist) == 1:
    ans = (N * K) // 2
elif S[0] == S[-1]:
    center = numlist[0] + numlist[-1]
    ans = (center // 2) * (K - 1)
    ans += numlist[0] // 2
    ans += numlist[-1] // 2
    for i in range(1, len(numlist)-1):
        ans += (numlist[i] // 2) * K
else:
    ans = 0
    for i in range(len(numlist)):
        ans += (numlist[i] // 2) * K
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import Counter















def f(s):
    n = len(s)
    i = 0
    ans = 0
    while i < n-1:
        c = 0
        while i < n-1 and s[i] == s[i+1]:
            c += 1
            i += 1
        ans += (c+1) // 2
        i += 1
    return ans

def main():
    s = input().strip()
    k = int(input().strip())
    n = len(s)
    if len(Counter(s)) == 1:
        print(n * k // 2)
        return
    ini = f(s)
    dif = f(s*2) - ini
    print(ini + dif * (k-1))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import Counter

def f(s):
    n = len(s)
    c = 0
    ans = 0
    for i in range(n-1):
        if s[i] == s[i+1]:
            c += 1
            if i == n-2:
                ans += (c+1) // 2
        else:
            ans += (c+1) // 2
            c = 0
    return ans

def main():
    s = input().strip()
    k = int(input().strip())
    n = len(s)
    if len(Counter(s)) == 1:
        print(n * k // 2)
        return
    ini = f(s)
    dif = f(s*2) - ini
    print(ini + dif * (k-1))

if __name__ == '__main__':
    main()

Solution (Python):
s=list(input())
k=int(input())
ss=s*2
a,b=0,0
i=1
while i<len(s):
    if s[i]==s[i-1]:
        i+=2
        a+=1
    else:
        i+=1
i=1        
while i<len(ss):
    if ss[i]==ss[i-1]:
        i+=2
        b+=1
    else:
        i+=1    
if len(set(s))==1:
    print(len(s)*k//2)
else:
    print(a+(b-a)*(k-1))

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

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

left = 0
right = 1

while left < N and S[left] == S[0]:
    left += 1
while right <= N and S[-right] == S[-1]:
    right += 1
right -= 1

change = 0
isChanged = False
T = list(S[left: N - right])

for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

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

print(ans)

Solution (Python):
import sys
s = input()
k = int(input())
ans = []
seqlen = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        seqlen += 1
    else:
        ans.append(seqlen)
        seqlen = 1
ans.append(seqlen)
if len(ans) == 1:
    print(len(s)*k//2)
    sys.exit()
key = 0
key0 = 0
key1 = 0
if s[-1] == s[0]:
    key0, key1 = ans[0], ans[-1]
    ans[0] = ans[0]+ans[-1]
    ans.pop()
    key = 1
for i in range(len(ans)):
    ans[i] = ans[i] // 2
answer = sum(ans)
if key0:
    print(answer*k+key0//2+key1//2-(key0+key1)//2)
else:
    print(answer*k)

Solution (Python):
def d_c(s):
  count = 0
  x = ''
  for i in range(len(s)):
    if x == s[0]:
      count += 1
      x = ''
    else:
      x = s[0]
    s = s[1:]
  return count
 
def num_con(s):
  x = s[0]
  s = s[1:]
  count = 1
  for i in range(len(s)):
    if s[0] == x:
      count += 1
      x = s[0]
      s = s[1:]
    else:
      break
  return count
      
    
s = input()
k = int(input())
 
if len(s)==1:
  print(k//2)
elif num_con(s)==len(s):
  print((len(s)*k)//2)
elif s[0]==s[-1]:
  f = num_con(s)
  m = num_con(s[::-1])
  dis = (f//2 + m//2 - (f+m)//2)*(k-1)
  print(d_c(s)*k-dis)
else:
  print(d_c(s)*k)

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

chrs = []
prev = 0
cnt = 0
for c in s:
    if c==prev: cnt+=1
    else: chrs.append(cnt);cnt=1
    prev = c
chrs.append(cnt)

ans = 0
for i in chrs: ans += i//2
ans *= k
if s[0]==s[-1]: ans += (chrs[1]%2 and chrs[-1]%2) * (k-1)

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

print(ans)

Solution (Python):
S = input()
K = int(input())
cnt = 0
ind_f = 0
ind_b = 0

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

if S[0] == S[-1]:
    _ = S[0]
    for i, c in enumerate(S):
        if _ != c:
            ind_f = i
            cnt = i
            break
    for i, c in enumerate(S[::-1]):
        if _ != c:
            ind_b = i
            cnt += i
            break
cnt = (cnt // 2) * (K - 1) 
cnt += ind_f // 2 + ind_b // 2
prev = ''
cnt2 = 0
if ind_f > 0:
    S = S[ind_f:-ind_b]
for c in S:
    if prev == c:
        cnt2 += 1
        prev = '0'
    else:
        prev = c
cnt += cnt2 * K    
print(cnt)

Solution (Python):
from sys import stdin

S = stdin.readline().rstrip()
K = int(stdin.readline().rstrip())
ans_odd = 0
ans_even = 0
changed_odd = [False] * len(S)
changed_even = [False] * len(S)

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

cnt = 1
ans = 0
begin = 0
end = 0

start = 0
for i in range(1, len(S)):
    if S[i - 1] != S[i]:
        if start == 0:
            begin += cnt
        else:
            if cnt >= 2:
                ans += cnt // 2
        cnt = 1
        start = i
    else:
        cnt += 1
    if i == len(S) - 1:
        if start == 0:
            print((cnt * K) // 2)
            exit()
        end += cnt
        break

if S[0] != S[-1]:
    print(ans * K + (begin // 2) * K + (end // 2) * K)
else:
    print(ans * K + begin // 2 + end // 2 + ((begin + end) // 2) * (K - 1))

Solution (Python):
S = input()
K = int(input())
li = []
num = 1


for i in range(len(S)-1): 
    if S[i] == S[i+1]:
        num += 1
    else:
        li.append(num)
        num = 1
li.append(num)



g = 0
for l in li:
    g += (l // 2)



tmp = 0
result = 0
if len(li) == 1:
    result = (li[0] * K) // 2
else:
    tmp = sum(j // 2 for j in li)
    if S[0] != S[-1]:
        result = tmp * K
    else:
        if li[0] % 2 == li[-1] % 2 == 1: 
            result = tmp * K + (1 * (K-1))
        else:
            result = tmp * K

print(result)

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


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

focus_c = S[0]
consecutive_s = S[0]
focus_s_array = []
for s in S[1:]:
    if s == focus_c:
        consecutive_s += s
    else:
        focus_s_array.append(consecutive_s)
        focus_c = s
        consecutive_s = s
else:
    focus_s_array.append(consecutive_s)


cost = 0
for fsa in focus_s_array:
    cost += len(fsa) // 2
ans = cost * K

if (focus_s_array[0][0] == focus_s_array[-1][0]) and ((len(focus_s_array[0]) + len(focus_s_array[-1])) % 2 == 0):
    ans += K - 1

print(ans)