Problem:
 Score : 300 points    Problem Statement Given is a string S . Let T be the concatenation of K copies of S .
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.      Constraints  1 \leq |S| \leq 100  S consists of lowercase English letters.  1 \leq K \leq 10^9  K is an integer.         Input Input is given from Standard Input in the following format:  S  K       Output Print the minimum number of operations required.        Sample Input 1 issii
2      Sample Output 1 4  T is issiiissii .  For example, we can rewrite it into ispiqisyhi , and now any two adjacent characters are different.       Sample Input 2 qq
81      Sample Output 2 81       Sample Input 3 cooooooooonteeeeeeeeeest
999993333      Sample Output 3 8999939997

Solution (Python):
import sys
from collections import defaultdict

s = list(map(str, input().rstrip()))
k = int(input())
connection = False

if all([x == s[0] for x in s]):
    print(len(s) * k // 2)
    sys.exit()

cnt = 0
if s[0] == s[-1] and k != 1:
    connection = True
    cnt = 2
    start = 1
    goal = 1
    x = s[0]

    for i in s[1:]:
        if i == x:
            cnt += 1
            start += 1
        else:
            break

    for i in s[:-1][::-1]:
        if i == x:
            cnt += 1
            goal += 1
        else:
            break

d = defaultdict(int)

if connection:
    ss = s[:start]
    sg = s[-goal:]
    s = s[start:-goal]
    
prev = s[0]
tmp = 1
for i, x in enumerate(s[1:]):
    if prev == x and i == (len(s) - 2):
        tmp += 1
        d[tmp] += 1
    elif prev == x:
        tmp += 1
    else:
        d[tmp] += 1
        prev = x
        tmp = 1

if connection:
    d[cnt] += 1

ans = 0
for key, val in d.items():
    if key == 1:
        continue
    else:
        ans += key // 2 * val * k

if connection:
    ans -= cnt // 2
    ans += len(ss) // 2 + len(sg) // 2

print(ans)

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

    ans=0
    if len(cnt)==1:
        ans=cnt[0][1]*k//2
    elif cnt[0][0]!=cnt[-1][0]:
        for x,y in cnt:
            ans+=y//2
        ans*=k
    else:
        for x,y in cnt:
            ans+=y//2
        ans*=k
        if (cnt[0][1]+cnt[-1][1])%2==0:
            ans+=k-1
    print(ans)

else:
    print(k//2)

Solution (Python):
S=input()
K=int(input())
N=len(S)
count=1
countlist=[]
anssub=0
for i in range(N-1):
  if S[i]==S[i+1]:
    count+=1
  else: 
    countlist+=[count]
    count=1
if S[N-2]!=S[N-1]:
  count=1
countlist+=[count]  
for i in range(len(countlist)):
  if i!=0 and i!=len(countlist)-1:
    if countlist[i]!=1:
      anssub+=countlist[i]//2
a=countlist[0]+countlist[-1]
if S[0]==S[-1]:
  anssub2=a//2
else:
  anssub2=0
  if countlist[0]!=1:
    anssub2+=countlist[0]//2
  if countlist[-1]!=1:
    anssub2+=countlist[-1]//2
  
b=countlist[0]
c=countlist[-1]
anssub3=0
if b!=1:
  anssub3+=b//2
if c!=1:
  anssub3+=c//2
  
ans=anssub*K+anssub2*(K-1)+anssub3
if len(countlist)==1:
  ans=countlist[0]*K//2
print(ans)

Solution (Python):
S=input()
K=int(input())
N=len(S)
count=1
countlist=[]
anssub=0
for i in range(N-1):
  if S[i]==S[i+1]:
    count+=1
  else: 
    countlist+=[count]
    count=1
if S[N-2]!=S[N-1]:
  count=1
countlist+=[count]  
for i in range(len(countlist)):
  if i!=0 and i!=len(countlist)-1:
    if countlist[i]!=1:
      anssub+=countlist[i]//2
a=countlist[0]+countlist[-1]
anssub2=0
if S[0]==S[-1]:
  anssub2+=a//2
if S[0]!=S[-1]:
  if countlist[0]!=1:
    anssub2+=countlist[0]//2
  if countlist[-1]!=1:
    anssub2+=countlist[-1]//2
  
b=countlist[0]
c=countlist[-1]
anssub3=0
if b!=1:
  anssub3+=b//2
if c!=1:
  anssub3+=c//2
  
ans=anssub*K+anssub2*(K-1)+anssub3
if len(countlist)==1:
  ans=countlist[0]*K//2
print(ans)

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

c = s[0]
l = 1
a = []
for i in range(1, len(s)):
    if s[i] == c:
        l += 1
    else:
        a.append(l)
        c = s[i]
        l = 1
a.append(l)

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

print(ans)

Solution (Python):
s = input()
ss = s + s
sss = s + s + s
k = int(input())
n = 0
nn = 0
nnn = 0
s = list(s)
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    n += 1
    s[i] = '.'
ss = list(ss)
for i in range(1,len(ss)):
  if ss[i-1] == ss[i]:
    nn += 1
    ss[i] = '.'
sss = list(sss)
for i in range(1,len(sss)):
  if sss[i-1] == sss[i]:
    nnn += 1
    sss[i] = '.'
a = nn-n*2
if nnn == n*3+a*2:
  print(n*k+a*(k-1))
else:
  print((len(s)*k)//2)

Solution (Python):
s = input()
k = int(input())
def seqcnt(s):
    cnt=1
    ans=0
    bk=''
    for c in s+' ':
        if bk==c:
            cnt+=1
        else:
            ans+=(cnt)//2
            cnt=1
        bk=c
    return ans

cnt=seqcnt(s)
cnt2=seqcnt(s*2)    
if len(set(list(s)))==1:
    print(len(s)*k//2)
else:
    print(cnt+(cnt2-cnt)*(k-1))

Solution (Python):
A=list(input())
n=int(input())
tmp=0
if len(set(A)) == 1:
   print(len(A)*n//2)
   exit()
sum_n=0
tmp3=0
while A[0] == A[tmp3]:
   tmp3+=1
sum_n+=tmp3
tmp3=0
while A[-1] == A[-1-tmp3]:
   tmp3+=1
sum_n+=tmp3
for i in range(len(A)-1):
   if A[i] == A[i+1]:
      A[i+1]="1"
      tmp+=1
tmp2=0
if A[0] == A[-1]:
   tmp2=n-1
   if sum_n%2 == 1:
      tmp2-=n-1
print(tmp*n+tmp2)

Solution (Python):
from copy import deepcopy

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

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

n1 = 0
n2 = 0
s1 = deepcopy(s)
s2 = deepcopy(s) + deepcopy(s)

for i in range(n-1):
    if s1[i] == s1[i+1]:
        s1[i+1] = "?"
        n1 += 1

for i in range(2*n - 1):
    if s2[i] == s2[i+1]:
        s2[i+1] = "?"
        n2 += 1

d = n2 - n1

ans = n1 + d*(k-1)
print(ans)

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

L=len(S)

D=[0]*(2*L)

D[0]=1
n=0
for i in range(1,2*L):
    if S[(i-1)%L]==S[i%L]:
        D[i]=D[i-1]+1
        n+=1
    else:
        D[i]=1

X=[]
if D[L-1]==L:
        print((L*K)//2)
else:
    if S[0]==S[-1]:
        f,g=True,False

        i=0
        while f:
            g=g or (S[i]!=S[i+1])
            
            if D[i]>=D[i+1]:
                if g:
                    X.append(D[i])
                    if i>=L-1:
                        f=False
                        
            i+=1

        A=sum(map(lambda n:n//2,X[1:]))*(K-1)+sum(map(lambda n:n//2,X[1:-1]))+(X[0]//2)+(D[L-1]//2)
    
    else:
        for i in range(L):
            if D[i]>D[i+1]:
                X.append(D[i])


        A=sum(map(lambda n:n//2,X))*K

    print(A)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




S = input()
N = len(S)
K = ii()
cnt = 0

i = 1
flag = 0

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

while i < N:
    if S[i] == S[i-1]:
        cnt += 1
        if i + 1 < N and S[i] == S[i+1]:
            i += 1
        if i == N-1:
            flag = 1
    i += 1

if flag and S[0] == S[-1]:
    cnt += 1
    cnt_sub = 0
    S = S[1:] + S[0]
    i = 1
    flag = 0
    while i < N:
        if S[i] == S[i-1]:
            cnt_sub += 1
            if i + 1 < N and S[i] == S[i+1]:
                i += 1
        i += 1
    print(cnt + cnt_sub * (K-2) + cnt_sub - 1)

else:
    print(cnt * K)

Solution (Python):
def main():
    S = input()
    K = int(input())
    
    lengths = []
    s = S[0]
    prev = S[0]
    length = 1
    for s in S[1:]:
        if s == prev:
            length += 1
        else:
            lengths.append([prev, length])
            length = 1
        prev = s
    lengths.append([s, length])

    if len(lengths) == 1:
        ans = lengths[0][1] * K // 2
    else:
        if lengths[0][0] == lengths[-1][0]:
            joint_len = lengths[0][1] + lengths[-1][1]
            ans = lengths[0][1] // 2
            ans += sum([i[1]//2 for i in lengths[1:-1]]) * K
            ans += joint_len // 2 * (K-1)
            ans += lengths[-1][1] // 2
        else:
            ans = sum([i[1]//2 for i in lengths]) * K
    print(ans)
main()

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

N=len(S)


kosuu=[]
cnt=1
for i in range(N-1):
    if S[i]==S[i+1]:
        cnt+=1
        if i==N-2:
            kosuu.append(cnt)
    elif S[i]!=S[i+1]:
        kosuu.append(cnt)
        cnt=1

dammy=0
for i in range(len(kosuu)):
    dammy+=kosuu[i]//2


if K==1:
    out=dammy

elif S[0]!=S[-1]:
    out = dammy * K

elif len(S)==1:
    out=K//2

elif len(kosuu)==1:
    out=(kosuu[0]*K)//2

else:
    
    kotei = dammy - kosuu[0]//2 - kosuu[-1]//2
    aida = (kosuu[0] + kosuu[-1]) // 2
    
    out = kosuu[0]//2 + (kotei*K) + aida*(K-1) + kosuu[-1]//2

print(out)

Solution (Python):
s=input()
from itertools import groupby as gb
l=[(a,len(list(b))) for a,b in gb(s)]
k=int(input())
if s[-1]==s[0]:
    ans=sum(v//2 for kf,v in l[1:-1])
    if len(l)!=1:print(ans*k+l[0][1]//2+l[-1][1]//2+(l[-1][1]+l[0][1])//2*(k-1))
    else:print(l[0][1]*k//2)
else:
    print(sum(v//2 for kf,v in l)*k)

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

C0 = []
cnt = 1
for i in range(1,N):
    if S[i]==S[i-1]:
        cnt+=1
        if i==N-1:
            C0.append(cnt)
    else:
        C0.append(cnt)
        cnt = 1
out0 = 0
for m in C0:
    out0 += m//2

if S[0]!=S[-1]:
    out = out0*K
elif N==1:
    out = K//2
elif len(C0)==1:
    X = C0[0]*K
    out = X//2
else:
    X = (C0[0] + C0[-1])//2
    Y = C0[0]//2 + C0[-1]//2
    out = (out0-Y)*K
    out += X*(K-1)
    out += Y
print(out)

Solution (Python):
s = input()
k = int(input())
import collections
co = collections.Counter(list(s))
n = len(s)
if len(co.values())==1:
    print(n*k//2)
    exit()
prev = ""
cnt = 0
for i in range(n):
    if s[i] == prev:
        cnt+=1
        prev=""
    else:
        prev=s[i]
bf = 0
af = 0
if s[0] == s[-1]:
    for i in range(n):
        if s[i] ==s[0]:
            bf+=1
        else:break
    for i in range(n):
        if s[-i-1] ==s[-1]:
            af+=1
        else:break
cnt = cnt - (bf//2 + af//2)
print(cnt*k + (bf+af)//2 *(k-1) +bf//2 + af//2)

Solution (Python):
s = input()
k = int(input())
s_list = []
ans = 0

def rle(s):
    tmp, count = s[0], 1
    for i in range(1, len(s)):
        if tmp == s[i]:
            count += 1
        else:
            s_list.append(count)
            tmp = s[i]
            count = 1
    s_list.append(count)
    return

rle(s)

if len(s_list) >= 2:
    for i in range(len(s_list)):
        if s_list[i] >= 2:
            ans += s_list[i] // 2

    ans *= k

    if (s_list[0] + s_list[-1]) % 2 == 0 and s[0] == s[-1]:
        ans += k - 1

else:
    ans = len(s) * k // 2

print(ans)

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

Solution (Python):
s=input()
k=int(input())
n=len(s)
counter=1
allsame=0
samefirst=1
samelast=1
firstletter=''
lastletter='.'
for i in range(n):
  if s[i]!=s[0]:
    firstletter=s[i]
    samefirst=i
    break
  if i==n-1:
    allsame=1
for i in range(n):
  if s[n-i-1]!=s[n-1]:
    lastletter=s[n-i-1]
    samelast=i
    break
  if i==n-1:
    allsame=1
anstemp=0
previousletter=s[samefirst-1]
for i in range(samefirst,n-samelast+1):
  currentletter=s[i]
  if currentletter==previousletter:
    counter+=1
  else:
    anstemp+=counter//2
    counter=1
    previousletter=currentletter
if allsame==1:
  print((k*n)//2)
elif firstletter==lastletter:
  print(anstemp*k+samefirst//2+samelast//2+(k-1)*((samefirst+samelast)//2))
else:
  print((anstemp+samefirst//2+samelast//2)*k)

Solution (Python):
s = input()
k = int(input())
l = 1
ans = 0
c = []

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

if len(s) == l:
  ans = k*l//2
elif s[0] == s[-1] and l%2 == 1 and c[0]%2 == 1:
  ans += k-1
  
print(ans)

Solution (Python):


s = list(str(input()))
k = int(input())

count = [[s[0], 1]]


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


ans = 0
if len(count) == 1:
    ans += (count[0][1]*k)//2

elif count[0][0] != count[-1][0]:
    for i in range(0, len(count)):
        letter, num = count[i]
        ans += (num//2)*k
else:
    for i in range(1, len(count)-1):
        letter, num = count[i]
        ans += (num//2)*k

    letter, num = count[0][0], count[0][1]+count[-1][1]
    ans += (num//2)*(k-1)

    ans += count[0][1]//2
    ans += count[-1][1]//2

print(ans)

Solution (Python):
def main():
    S = input()
    K = int(input())
    if len(set(S)) == 1:
        print((len(S) * K)//2)
        return
    result = 0
    piyo = 0
    hoge = S[0]
    huga = 1
    first = 0
    for s in S[1:]:
        if s == hoge:
            huga += 1
            if huga % 2 == 0:
                piyo += 1
        else:
            if first == 0:
                first = huga
            hoge = s
            huga = 1
    if first == 0:
        first = huga
    if first % 2 == 1 and huga % 2 == 1 and S[0] == S[-1]:
        result += K - 1
    print(piyo * K + result)
main()

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

l = len(s)
cnt = 0
cnt_list = [0]

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


for i in range(l-1):
    if s[-1] == s[i]:
        cnt += 1
        cnt_list.append(cnt)
    else:
        break

L = len(cnt_list)

if len(set(s)) == 1:
    print(l*k//2)
    
elif k == 1:
    ans = 0
    for i in cnt_list[:l]:
        if i % 2 == 1:
            ans += 1
    print(ans)
    
else:
    cnt_ref = cnt_list[L-l:] 
    ans_ref = 0
    for i in cnt_ref:
        if i % 2 == 1:
            ans_ref += 1
    num_ref = (k*l-L) // l 
    
    
    amari = (k*l-L) % l
    ans_amari = 0
    for i in cnt_ref[:amari]:
        if i % 2 == 1:
            ans_amari += 1
    
    
    
    ans = 0
    for i in cnt_list:
        if i % 2 == 1:
            ans += 1
    
    
    ans += ans_amari + ans_ref * num_ref
    print(ans)

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

def f(s):
    prev = ''
    ct = 0
    for i in s:
        if i == prev:
            ct += 1
            prev = ''
        else:
            prev = i
    return ct
        

if len(set(s)) == 1:
    print(len(s) *n//2)
elif s[0]!=s[-1]:
    print(f(s) * n)
else:
    ans = f(s) + (f(s*2) - f(s)) * (n-1)
    print(ans)

Solution (Python):
S = input()
K = int(input())
S2 = S + S
ans = 0
s = S[0]
if len(set(S)) == 1:
    print(len(S) * K // 2)
else:
    for i in range(1, len(S2)):
        if s == S2[i]:
            ans += 1
            s = '1'
        else:
            s = S2[i]

    if ans % 2 == 0:
        ans = ans // 2 * K
    else:
        ans = (ans + 1) // 2 * K - 1

    print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() 
ni = lambda: int(readline().rstrip()) 
nm = lambda: map(int, readline().split()) 
nl = lambda: list(map(int, readline().split())) 

s = ns()
k = ni()
cnt = 1
li = []
prev = '@'
for c in s:
    if c == '@':
        cnt = 1
        prev = c
        continue
    elif prev == c:
        cnt += 1
    else:
        li.append(cnt)
        cnt = 1
        prev = c
else:
    li.append(cnt)
li = li[1:]

ans_ele = 0
ans = 0
if len(set(s)) == 1:
    ans = len(s) * k // 2
elif s[0] == s[-1] and (li[0] + li[-1]) % 2 == 0:

    for e in li:
        if e >= 2:
            ans_ele += e // 2
    ans = ans_ele * k + k - 1
else:
    for e in li:
        if e >= 2:
            ans_ele += e // 2
    ans = ans_ele * k
print(ans)

""" same
1 ~ 1 :  + (k - 1)
2 ~ 1 : X
1 ~ 2 : X
2 ~ 2 : X
 and len(li) != 1
"""

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

S2 = S*2

for i in range(1,len(S2)):
    if S2[i-1]== S2[i]:
        S2[i] = '@'
        
        if i>=len(S2)//2:
            ans+= K-1
        else:
            ans+=1
            
if len(set(S)) ==1 and len(S)%2 !=0:
   ans = len(S)*(K//2)+(K%2==1)*(len(S)//2)
print(ans)

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

if len(set(s)) == 1:
    print((len(s)*k)//2)
else:
    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):
from collections import Counter

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

ans = 0
s_d = s*2
for i in range(1,len(s_d)):
    if s_d[i-1]==s_d[i]:
        s_d[i] = '_'
        if i>=len(s_d)//2:
            ans+=k-1
        else:
            ans+=1
s_s = set(s)
if len(s_s)==1 and len(s)%2!=0:
    ans = len(s)*(k//2)+(k%2==1)*(len(s)//2)
print(ans)

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

S = S + S + S
ans_first = ans_odd = ans_even = 0
for i in range(1, N):
  pre_s = S[i-1]
  s = S[i]
  if s == pre_s:
    ans_first += 1
    S[i] = '

for i in range(N, 2*N):
  pre_s = S[i-1]
  s = S[i]
  if s == pre_s:
    ans_even += 1
    S[i] = '

for i in range(2*N, 3*N):
  pre_s = S[i-1]
  s = S[i]
  if s == pre_s:
    ans_odd += 1
    S[i] = '

k_even = K // 2
k_odd = K - k_even - 1
print(ans_first + k_odd*ans_odd + k_even*ans_even)

Solution (Python):
from collections import Counter

S1 = input()
K = int(input())
S1C = Counter(S1)
if len(S1C) == 1:
    print(len(S1) * K // 2)
    exit()

start = S1[0]
end = S1[-1]
now = S1[0]
a1 = 0
flag = False
for s in S1[1:]:
    if flag:
        flag = False
        now = s
        continue
    else:
        if s == now:
            a1 += 1
            flag = True
        now = s
S2 = S1 + S1

start = S2[0]
end = S2[-1]
now = S2[0]
a2 = 0
flag = False
for s in S2[1:]:
    if flag:
        flag = False
        now = s
        continue
    else:
        if s == now:
            a2 += 1
            flag = True
        now = s
b = a2 - 2 * a1

print(a1 * K + b * (K-1))

Solution (Python):
from itertools import groupby

def solve():
    S = input()
    K = int(input())
    a = [len(tuple(s)) for _, s in groupby(S)]
    
    if S[0] == S[-1] and K > 1:
        if len(a) == 1:
            return a[0] * K // 2
        else:
            ans = a[0] // 2 + a[-1] // 2 + (a[0]+a[-1]) // 2 * (K-1)
            ans += sum(n // 2 for n in a[1:-1]) * K
            return ans
    else:
        return sum(n // 2 for n in a) * K
    
print(solve())

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

num_s = len(list(s))
count = 0
temp_count = 1
count_1 = 0
count_2 = 0

if s[0] != s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      count += temp_count//2
      temp_count = 1
      
  ans = (count+temp_count//2)*k
  
elif s[0] == s[-1]:
  for i in range(1,num_s):
    if s[i-1] == s[i]:
      temp_count += 1
    elif s[i-1] != s[i]:
      if sign == 1:
        count_1 = temp_count
        sign = 0
      elif sign == 0:
        count += temp_count//2
     
      temp_count = 1

  count_2 = temp_count
  
  ans = count*k + ((count_1 + count_2)//2)*(k-1) + (count_1//2) + (count_2//2)

  if sign == 1:
    ans = (k*num_s)//2
    
if num_s == 1:
  ans = k//2
  
print(ans)

Solution (Python):
s=input()
k=int(input())
n=len(s)
ans=0
tmp=0
x=s[0]
ss=[]
for i in range(n):
  if x==s[i]:
    tmp+=1
  else:
    ss.append(tmp)
    tmp=1
    x=s[i]
if tmp>0:
  ss.append(tmp)
if len(ss)==1:
  print(n*k//2)
else:
  if s[0]==s[-1]:
    ans=(-(ss[0]//2)-(ss[-1]//2)+(ss[0]+ss[-1])//2)*(k-1)
  for i in ss:
    ans+=i//2*k
  print(ans)

Solution (Python):
def calc(s):
    n = len(s)
    seq_cnt = []
    cnt, before_c = 0, s[0]
    for c in s:
        if c != before_c:
            seq_cnt.append(cnt)
            cnt = 0
            before_c = c
        cnt += 1
    seq_cnt.append(cnt)
    ret = 0
    for cnt in seq_cnt:
        ret += cnt // 2
    return ret


def main():
    S = input()
    K = int(input())
    N = len(S)
    index = N
    for i, c in enumerate(S):
        if c != S[0]:
            index = i
            break
    if index == N:
        
        print(N * K // 2)
    else:
        
        print(calc(S[:index]) + (K - 1) * (calc(S[index:] + S[:index])) + calc(S[index:]))


if __name__ == '__main__':
    main()

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

def f(S):
    res = 0
    count = 1
    length = len(S)
    for i in range(1,length):
        if S[i] == S[i-1]:
            count += 1
        else:
            res += count // 2
            count = 1
    res += count // 2
    return res

res1 = f(s)
res2 = f(s+s)
res3 = f(s+s+s)

if res2 - res1 == res3 - res2:
    print(res1 + (res2-res1) * (k-1))
else:
    print(n * k // 2)

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

ans = 0
prev = S[0]
endchange = False
for i in range(1,N):
    if S[i] == prev:
        ans += 1
        prev = 0
        if i == N:
            endchange = True
    else:
        prev = S[i]

if all([s == S[0] for s in S]):
    print((N*K)//2)

elif K==1 or S[0] != S[-1]:
    print(ans * K)
else:
    front = 1
    while 1:
        if S[front] == S[0]:
            front += 1
        else:break
    back = -1
    while 1:
        if S[back] == S[0]:
            back -= 1
        else:break
    back = -1-back
    print(ans*K + ((front+back)//2 - front//2 - back//2) * (K-1))

Solution (Python):
import sys
s = input()
k = int(input())
data = []
start = s[0]
seq = 1
if len(set(list(s))) == 1:
    print(len(s)*k//2)
    sys.exit()
for i in s[1:]:
    if start == i:
        seq += 1
    else:
        data.append(seq)
        start = i
        seq = 1
else:
    data.append(seq)

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

Solution (Python):


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

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

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

ans = 0
if len(C) == 1:
    print((C[0][1]*K) // 2)
    exit()
else:
    for c in C:
        ans += c[1] // 2

ans *= K

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

Solution (Python):


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

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

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

ans = 0
if len(C) == 1:
    print((C[0][1]*K) // 2)
    exit()
else:
    for c in C:
        ans += c[1] // 2

ans *= K

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

Solution (Python):

import collections
import itertools as it
import math

import re
 
s = input()
k = int(input())




c = collections.Counter(s)

def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]

ans = 0

chains = list(nchars(s, 2))

if len(c) == 1:
    print(len(s) * k // 2)
else:
    
    
    for r in chains:
        ans += len(r) // 2

    if chains and chains[0] == chains[-1]:
        print(ans * k + (k - 1 if chains[0] == chains[-1] else 0))
    else:
        print(ans * k)

Solution (Python):

from itertools import*
s = input()
k = int(input())
t = [len(list(e)) for _, e in groupby(s)]
if len(set(s)) == 1:
    a = len(s) * k // 2
elif k > 1 and s[0] == s[-1]:
    p = sum(e // 2 for e in t[1:-1]) * k
    l, r = t[0], t[-1]
    q = (l + r) // 2 * ~-k + l // 2 + r // 2
    a = p + q
else:
    a = sum(e // 2 for e in t) * k
print(a)

Solution (Python):
S = [i for i in input()]
K = int(input())
ans = 0
s = S*2
for i in range(1,len(s)):
    if s[i-1] == s[i]:
        s[i] = "_"
        if i >= len(s)//2:
            ans += K-1
        else:
            ans += 1
kazu = set(S)
if len(kazu) == 1 and len(S) % 2 != 0:
  ans = len(S) * (K//2) + (K%2==1) * (len(S)//2)
print(ans)

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

ans = 0
lens = len(s)


def tansaku(moji, n, m, k):
	
	ansr = 0
	cntt = 1
	for i in range(n, m-1):
		if moji[i] == moji[i + 1]:
			cntt += 1
		else:
			ansr += (cntt // 2) * k
			cntt = 1
	ansr += (cntt // 2) * k
	return ansr

cnnt = 1
hentai = False
for i in range(lens-1):
	if s[i] == s[i+1]:
		cnnt += 1
if cnnt == lens:
	hentai = True

if not hentai:
	if k >= 2:
		zencnts = 0
		atocnts = 0
		if s[lens-1] == s[0]:
			for i in range(lens):
				if s[lens-1-i] == s[lens-1]:
					zencnts += 1
				else:
					break
			for i in range(lens):
				if s[i] == s[0]:
					atocnts += 1
				else:
					break
		news = s[atocnts:] + s[:atocnts]
		news1 = s[atocnts:]
		news2 = s[:atocnts]
		ans += tansaku(news, 0, lens, k-1)
		ans += tansaku(news1, 0, lens-atocnts, 1)
		ans += tansaku(news2, 0, atocnts, 1)
		print(ans)
	else:
		ans += tansaku(s, 0, lens, 1)
		print(ans)
else:
	print((lens*k)//2)

Solution (Python):
from collections import Counter
S=input()
K=int(input())
S_double = S*2
n = 0
if len(set(list(S)))==1:
    print(len(S)*K//2)
    exit()
while n< len(S_double)-1:
    if S_double[n] ==S_double[n+1]:
        S_double = S_double[:n+1]+' '+S_double[n+2:]
    n += 1
ans = Counter(S_double[:len(S)])[' '] + Counter(S_double[len(S):])[' ']*(K-1)
print(ans)

Solution (Python):
s = input()
k = int(input())
n = len(s)
ss = set(s)

ans = 0
if len(ss) == 1:
    print(k*n//2)
    
    exit()
if n == 1:
    print(n//2)
    exit()
streaks = []
cnt = 0
now = s[0]
for i in range(n):
    if now == s[i]:
        cnt += 1
    else:
        streaks.append(cnt)
        now = s[i]
        cnt = 1
streaks.append(cnt)

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

print(ans)

Solution (Python):
s = input()
k = int(input())
l=len(s)
s+='?'
ch = 1
ans=0
j=0

for i in range(l):
    if s[i]==s[i+1]:
        ch+=1
    else:
        ans+=ch//2
        if j==0:
            st=ch
        if s[i+1]=='?':
            gl=ch
        ch=1
        j=1
ans*=k
if st%2==1 and gl%2==1 and s[0]==s[l-1]:
    ans+=k-1

if st==l:
    ans=l*k//2

print(ans)

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

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

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

if S[0] != S[-1]:
    ans = 0
    tmp = ""
    for s in S:
        if s == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = s
    ans *= K
    print(ans)
    exit()
elif S[0] == S[-1]:
    A = 0
    B = 0
    for s in S:
        if s == S[0]:
            A+=1
        else:
            break
    for s in S[::-1]:
        if s == S[0]:
            B+=1
        else:
            break
    ans = 0
    tmp = ""
    for s in S:
        if s == tmp:
            ans += 1
            tmp = ""
            continue
        else:
            tmp = s
    ans *= K
    ans = ans - (A//2 + B//2)*(K-1) + (A+B)//2*(K-1)
    print(ans)
    exit()

Solution (Python):
s = list(input())
k = int(input())
ans = []
now = s[0]
cou = 0
if len(s) == 1:
    print(k//2)
    exit()

for i in range(len(s)):
    if now == s[i]:
        cou += 1
    elif now != s[i]:
        ans.append(cou)
        cou = 1
        now = s[i]
if cou > 0:
    ans.append(cou)
if ans[0] == len(s):
    print((ans[0]*k)//2)
    exit()
    
p = 0
for i in ans:
    p += (i//2)*k

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

print(p)

Solution (Python):


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))


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

def sub(s):
    if not s:
        return 0
    
    prev = s[0]
    counts = []
    count = 1
    for c in s[1:]:
        if c==prev:
            count += 1
        else:
            counts.append(count)
            count = 1
        prev = c
    if count>=2:
        counts.append(count)
    return sum([item//2 for item in counts])

if s[0]!=s[-1]:
    ans = k * sub(s)
elif all(s[0]==s[i] for i in range(1, len(s))):
    ans = (k * len(s)) // 2
else:
    i = 0
    while s[i]==s[-1]:
        i += 1
    ss = s[i:] + s[:i]
    ans = sub(s[:i]) + (k-1)*sub(ss) + sub(s[i:])
print(ans)

Solution (Python):
s = input()
k = int(input())
ren = []
n = 1
p = s[0]
for i in range(1,len(s)):
    if p == s[i]:
        n += 1
    else:
        ren.append(n)
        p = s[i]
        n = 1
ren.append(n)

ans = 0
if ren[0] == len(s):
    ans = (ren[0]*k)//2
elif s[0] == s[-1]:
    ans = ren[0]//2 + ren[-1]//2 - (ren[0]+ren[-1])//2
    ren[0] += ren[-1]
    ren[-1] = 0
    for i in ren:
        ans += (i//2)*k
else:
    for i in ren:
        ans += (i//2)*k

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

s = list(readline().rstrip().decode())
k = int(read())
if len(set(s)) == 1:
    print(len(s) * k // 2)
    exit()
ans = 0
memo = 0
flag = True
for i, (bf, af) in enumerate(zip(s, s[1:])):
    if s[0] == af and flag:
        memo += 1
    else:
        flag = False
    if bf == af:
        ans += 1
        s[i + 1] = '?'
ans *= k
if s[-1] == s[0] and memo % 2 == 0:
    ans += k - 1
print(ans)

Solution (Python):
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

import copy






INF = sys.maxsize


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





def main():
    s = list(input())
    k = ni()

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

    def changer(sss):
        ts = sss.copy()
        cnt = 0
        for i in range(1, len(ts)):
            if ts[i] == ts[i - 1]:
                ts[i] = "@"
                cnt += 1

        return cnt

    print(changer(s) + (k - 1) * (changer(s + s) - changer(s)))


if __name__ == '__main__':
    main()