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

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

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)
    
count *= int(K)
if len(L) == 1:
    if len(L[0]) == 1:
        count = int(int(K) / 2)
    elif len(L[0]) % 2 == 1:
        count += int(int(K) / 2)

elif L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + int(K) - 1
print(count)

Solution (Python):
def main():
    s = str(input())
    N = int(input())
    st = []
    moji = s[0]
    a = len(s)
    count = 1
    for i in range(a-1):
        if moji == s[i+1]:
            count += 1
        else:
            st.append(count)
            moji = s[i+1]
            count = 1
    st.append(count)
    ans = 0
    if len(st) == 1:
        ans = count*N//2
    else:
        for i in st:
            ans += i//2
        ans *= N
        if s[0] == s[-1]:
            ans += ((st[0]+st[-1])//2-st[0]//2-st[-1]//2)*(N-1)

    print(ans)

if __name__ == "__main__":
    main()

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

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

    for i in a:
        ans += (i//2)*K

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

    print(ans)

Solution (Python):
s = input()
S = list(s)
K = int(input())
cnt1 = 0
cnt2 = 0
if len(set(S)) == 1:
  print(len(S) * K // 2)
  exit()
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    S[i+1] = "
    cnt1 += 1
SS = list(s + s)
for i in range(len(SS)-1):
  if SS[i] == SS[i+1]:
    SS[i+1] = "
    cnt2 += 1
print(cnt1 + (cnt2 - cnt1) * (K - 1))

Solution (Python):
S = input()
K = int(input())
lenS = len(S)
ans = 0
if S.count(S[0]) == lenS:
    ans = (lenS*K)//2
else:
    if S[0] == S[lenS - 1] and lenS > 1:
        if S[0] == S[1]:
            ind = 1
            while(S[ind+1] == S[0]):
                ind += 1
            if ind % 2 == 1:
                S = "*"+S[1:]
                ans += K

        else:
            ans += K-1
            if S[lenS-1] == S[lenS-2]:
                ans += 1
            S = S[:lenS-1]+"*"

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

print(ans)

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

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

if len(sumarr) == 1:
    print(int(len(S) * K / 2))
    exit(0)


def to_cnt(num):
    if num < 2:
        return 0
    else:
        return int(num / 2)


ans = 0
if S[0] == S[len(S) - 1]:
    ans += to_cnt(sumarr[0])
    ans += to_cnt(sumarr[len(sumarr) - 1])
    connection = to_cnt(sumarr[0] + sumarr[len(sumarr) - 1])
    cnt = sum(map(to_cnt, sumarr[1: len(sumarr) - 1]))
    ans += cnt * K
    ans += connection * (K - 1)
else:
    ans += sum(map(to_cnt, sumarr)) * K

print(ans)

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

ans = 0
len_S = len(S)
a = b = 0
c = 1
same_letter = True
for i in range(len_S - 1):
    if S[i] == S[i + 1]:
        c += 1
    else:
        same_letter = False
        ans += c // 2
        if i == c - 1:
            a = c
        c = 1
b = c
ans += c // 2
if same_letter:
    ans = len_S * K // 2
else:
    ans *= K
    if S[0] == S[-1] and K >= 2 and (a % 2) * (b % 2) == 1:
        ans += K - 1
print(ans)

Solution (Python):
s,k=input(),int(input())
c = 0
r = 0
ss = 0
l = 1
for c1, c2 in zip(s, s[1:]):
    if c1 == c2:
        c += 1
    else:
        r += (c+1)//2
        if ss == 0:
            ss = c+1
        l = c+1
        c = 0
if c:
    r += (c+1)//2
    l = c+1
    if ss == 0:
        ss = c+1
r *= k
if len(s) == 1:
    r = k//2
elif ss == len(s):
    r = (k*ss)//2
elif s[0] == s[-1] and (ss*l) % 2:
    r+=(k-1)
print(r)

Solution (Python):



import sys


def main():
    S = input()
    K = int(input())
    ary = []
    ans = 0
    n = 1

    for i, s in enumerate(S[1:]):
        if s == S[i]:
            n += 1
        else:
            ary.append(n)
            n = 1
    if n != 1:
        ary.append(n)

    if S[0] == S[-1]:
        for num in ary[1:-1]:
            ans += num//2
        ans *= K
        if len(ary) == 1:
            ans += ary[0]*K//2
        elif len(ary) < 1:
            ans = K//2
        else:
            ans += (ary[0]+ary[-1])//2*(K-1)
            ans += ary[0]//2+ary[-1]//2
    else:
        for num in ary:
            ans += num//2
        ans *= K
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

s = input()
k = int(input())
s_sum = 0

s_f = 1
s_l = 1


if s[0] == s[-1]:
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                s_f += 1
            else:
                break
                
    
    for i in range(len(s)-1 , -1, -1):
        if i != len(s)-1:
            if s[i] == s[i+1]:
                s_l += 1
            else:
                break
                
    
                
                
    if s_f == len(s):
        print((k * len(s)) // 2)
        
    else:
        n = 1
        is_not_first = False
        for i in range(len(s)):
            if i != 0:
                if s[i] == s[i-1]:
                    n += 1
                else:
                    if not is_not_first:
                        is_not_first = True
                        n = 1
                    else:
                        if n != 1:
                            if n % 2 == 0:
                                s_sum += (n / 2)
                            else:
                                s_sum += ((n - 1)/ 2)
                            n = 1

        x1 = (s_f // 2 + s_l // 2) + s_sum * k
        x2 = ((s_f + s_l)//2) * (k-1)
        print(int(x1 + x2))
        
else:   
    n = 1
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                n += 1
            else:
                if n != 1:
                    if n % 2 == 0:
                        s_sum += (n / 2)
                    else:
                        s_sum += ((n - 1)/ 2)
                    n = 1
                    

        if i == (len(s) - 1):
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)
                    

    print(int(s_sum * k))

Solution (Python):
import re

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

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

con = list(nchars(S, 1))

if len(con)==1:
    print(int(len(S)*K/2))

else:
    cons = []
    for coni in con:
        cons.append(int(len(coni)/2))

    c1 = cons[0]

    c2 = 0
    for i in range(len(cons)):
        if i != 0 and i != len(cons)-1:
            c2 += cons[i]

    if con[0][0]==con[-1][0]:
        c3 = int(len(con[0]+con[-1])/2)
    else:
        c3 = int(len(con[0])/2) + int(len(con[-1])/2)

    c4 = int(cons[-1])

    print(int(c1 + c2*K + c3 *(K-1) + c4))

Solution (Python):


def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2


s1=input()
k=int(input())


s2=groupby(s1)

c=0
if len(s2)!=1:
    for i in range(len(s2)):
        if i!=0 and i!=len(s2)-1:
            c+=(s2[i][1]//2)*k

    if s2[0][0]==s2[-1][0]:
        c+=((s2[0][1]+s2[-1][1])//2)*(k-1)
        c+=(s2[0][1]//2+s2[-1][1]//2)
    else:
        c+=(s2[0][1]//2+s2[-1][1]//2)*k
else:
    c+=(s2[0][1]*k)//2

print(c)

Solution (Python):
s = input()
s = s + "1"
k = int(input())
ans = 0
lis = []
tem = 1
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tem += 1
  else:
    lis.append(tem)
    tem = 1
if s[0] == s[-2]:
  t = lis[0]+lis[-1]
  for i in range(1,len(lis)-1):
    if lis[i] % 2 == 0:
      ans += lis[i]//2
    else:
      ans += (lis[i]-1)//2
  ans*= k
  if t%2 == 0:
    ans += (k-1)*t//2
  else:
    ans += (k-1)*(t-1)//2
  if lis[0]%2 == 0:
    ans += lis[0]//2
  else:
    ans += (lis[0]-1)//2
  if lis[-1]%2 == 0:
    ans += lis[-1]//2
  else:
    ans += (lis[-1]-1)//2
else:
  for item in lis:
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans *= k
if len(lis) == 1:
  anse = (len(s)-1)*k
  if anse%2 == 0:
    ans = anse//2
  else:
    ans =(anse-1)//2
print(ans)

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-2<g[0])))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-1==g[0])))

Solution (Python):
import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
c3 = 0

for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
      c3 = l[i]//2

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

elif not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1) + c3)

Solution (Python):
import copy

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


def listCount(S,K):
    box = "" 
    count=0 

    for mozi in S:
        if box != mozi:
            box = mozi
        else:
            count += 1
            box=""
            
    return count
  

def listCount_dash(S,K):
    box =""
    S_origin = copy.deepcopy(S)
    
    
    for k in range(0,len(S)-1):
        
        if S[0] == S[len(S)-1]:
            S.append(S[0])
            S.pop(0)
        else:
            break
        
    return listCount(S,K)*(K-1) + listCount(S_origin,K)
  

def listCount_dou(S,K):
    S_origin = copy.deepcopy(S)
    S.extend(S)
    return listCount(S,K)*int(K/2) + listCount(S_origin,K)*(K%2)
  
flag = 1
box=S[0]
for mozi in S:
    if box != mozi:
        flag = 0
        break
    box = mozi

if flag==0 and S[0] != S[len(S)-1]:
    
    ans = listCount(S, K)*K
elif flag==0:
    
    ans = listCount_dash(S, K)
else:
    
    ans = listCount_dou(S,K)
    
print(ans)

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+(g[0]*g[-2]%2*s[0]==s[-2])*~-k//2**(len(s)-1==g[0]))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+(g[0]*g[-2]%2*s[0]==s[-2])*(~-k//2**(len(s)-1==g[0])))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-(g[0]*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-(g[0]%2*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-((s[0]==s[-2])*g[0]%2&g[-2]%2)*(-k//2**(len(s)-1==g[0])+1))

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

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

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

    if S[0] == S[-1]:
        a, i =1, 1
        while S[0]==S[i]:
            a += 1
            i += 1
        b, i =1, 1
        while S[-1]==S[-1- i]:
            b += 1
            i += 1
        ans = ans - (a//2 +b//2 -(a+b)//2)*(K-1)        
    

print(ans)

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2*(-len(s)==~g[0])or k-1))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2*(-len(s)==~g[0])or k-1))

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = input()
k = INT()
n = len(s)

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

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

if s[0]==s[-1]:
    a = l[0]
    b = l[-1]
    res = (a//2) + (b//2) - ((a+b)//2)
    print(ans-(res*(k-1)))
else:
    print(ans)

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




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

cnt = 0
continuous = 0
chr = S[0]
for i in range(1, len(S)):
    if chr == S[i]:
        cnt += 1
    if chr != S[i] or i == len(S) - 1:
        continuous += (cnt + 1) // 2
        cnt = 0
    chr = S[i]
chr = S[0]
left = 0
idx = 0
while idx < len(S) and S[idx] == chr:
    left += 1
    idx += 1
idx = -1
right = 0
while idx < len(S) and S[idx] == chr:
    right += 1
    idx -= 1
same = right + left
if continuous == 0 and S[0] == S[-1]:
    print(K - 1)
else:
    ans = K * continuous
    if right % 2 == 1 and left % 2 == 1:
        ans += K - 1
    print(ans)

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        print(n * k // 2)
        exit()

mode = 0
count = 0
ans = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        mode = 0
        ans += count // 2
        count = 0

if mode == 1:
    ans += count // 2

ans *= k

if s[0] == s[-1]:
    a = 1
    while s[a-1] == s[a]:
        a += 1
    b = n-1
    while s[b-1] == s[b]:
        b -= 1
    b = n - b
    ans -= ((a//2) + (b//2) - ((a+b) // 2))*(k-1)

print(ans)

Solution (Python):
def f(s, k):
    
    k = int(k)
    
    cnt = 0
    ss = s
    for i in range(len(ss)):
        if i == 0:
            continue

        if ss[i-1] == ss[i]:
            ss = ss[:i] + '\\' + ss[i+1:]
            cnt += 1
        
    cnt = cnt * k
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 1:
            cnt = cnt + (k//2)
    else:    
        cntchk1 = 0
        cntchk2 = 0
        if s[0] == s[len(s)-1]:
            for i in range(len(s)):
                if s[len(s)-1] == s[len(s)-1 - i]:
                    cntchk1 += 1


                if i < len(s)-1 and s[0] == s[i+1]:
                    cntchk2 += 1
                    
            if cntchk1 % 2 == 0 and cntchk1 % 2 == 0:
                cnt = cnt + (k -1)
                
    return str(cnt)


print(f(input(),input()))

Solution (Python):
import sys
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cnt==cntb==len(S)-1 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)

Solution (Python):
s = input()
k = int(input())
query = []
query2 = []
tmp = 1
if len(set(s)) == 1:
  print(len(s)*k//2)
  exit()

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tmp += 1
  else:
    query.append(tmp)
    tmp = 1
query.append(tmp)
tmp = 1
s2 = s+s
for i in range(len(s2)-1):
  if s2[i] == s2[i+1]:
    tmp += 1
  else:
    query2.append(tmp)
    tmp = 1
query2.append(tmp)
ans1 = 0
ans2 = 0
for i in query:
  ans1 += i//2
for i in query2:
  ans2 += i//2

if k== 1:
  print(ans1)
else:
  ans = ans1+(ans2-ans1)*(k-1)
  print(ans)

Solution (Python):


def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
pressed, cnt = press(S)
if len(cnt) == 1:
    ans = N*K // 2
elif S[0] != S[-1]:
    ans = 0
    for c in cnt:
        ans += (c // 2) * K
else:
    ans = (cnt[0] + cnt[-1])//2 * (K-1)+ cnt[0]//2 + cnt[-1]//2
    for i in range(1,len(cnt)-1):
        ans += (cnt[i] // 2) * K

print(ans)

Solution (Python):

def test():
    param = []
    val = ['issii', '2', '4']
    param.append(val)
    
    
    
    val = ['issii', '3', '6']
    param.append(val)
    
    
    
    val = ['qq', '81', '81']
    param.append(val)

    val = ['cooooooooonteeeeeeeeeest', '999993333', '8999939997']
    param.append(val)

    
    
    val = ['aaa', '4', '6']
    param.append(val)
    
    
    
    val = ['aaaa', '4', '8']
    param.append(val)
    
    
    
    val = ['aaaaa', '4', '10']
    param.append(val)
    
    
    
    val = ['i', '4', '2']
    param.append(val)
    
    
    
    val = ['isi', '4', '3']
    param.append(val)
    
    
    
    val = ['isssi', '3', '5']
    param.append(val)
    
    
    
    val = ['isssi', '3', '5']
    param.append(val)
    
    
    
    val = ['iissi', '3', '6']
    param.append(val)
    
    print(param)

    for i in range(len(param)):
        ret = f(param[i][0], param[i][1])
        if ret == param[i][2]:
            chk = 'o'
        else:
            chk = 'x'
        space = '  '
        print(chk + space + param[i][0] + space + param[i][1] + space + param[i][2] + space + 'test...' + ret)

def f(s, k):
    
    k = int(k)
    
    cnt = 0
    ss = s
    for i in range(len(ss)):
        if i == 0:
            continue

        if ss[i-1] == ss[i]:
            ss = ss[:i] + '\\' + ss[i+1:]
            cnt += 1
        
    cnt = cnt * k
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 1:
            cnt = cnt + (k//2)
    else:    
        cntchk1 = 0
        cntchk2 = 0
        if s[0] == s[len(s)-1]:
            for i in range(len(s)):
                if s[len(s)-1] == s[len(s)-1 - i]:
                    cntchk1 += 1


                if i < len(s)-1 and s[0] == s[i+1]:
                    cntchk2 += 1
                    
            if cntchk1 % 2 == 0 and cntchk1 % 2 == 0:
                cnt = cnt + (k -1)
                
    return str(cnt)


print(f(input(),input()))

Solution (Python):
from itertools import*
s,k=open(0)
k=int(k);x=y=0
g=[len(list(v))for _,v in groupby(s)]
for c in g:x+=c//2
if(s[0]==s[-2])*g[0]%2&g[-2]%2:y=k//2if-len(s)==~g[0]else k-1
print(x*k+y)

Solution (Python):
S = input()
K = int(input())
if len(set(S)) == 1:
    print(len(S) * K // 2)
    exit()
def get_kyotsu(st):
    kyotsu = []
    tmp = 1
    for s1, s2 in zip(st[:-1], st[1:]):
        if s1 == s2:
            tmp += 1
        if s1 != s2 and tmp != 1:
            kyotsu.append(tmp)
            tmp = 1
    if tmp != 1:
        kyotsu.append(tmp)
    res = 0
    for k in kyotsu:
        res += k//2
    return res

sento = S[0]
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        sento+=s2
    if s1 != s2:
        break
matsubi = S[-1]
for s1, s2 in zip(S[::-1][:-1], S[::-1][1:]):
    if s1 == s2:
        matsubi+=s2
    if s1 != s2:
        break

conn = matsubi + sento 

print(K * get_kyotsu(S) - (get_kyotsu(sento) + get_kyotsu(matsubi) - get_kyotsu(conn)) *(K-1))

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

    def f(s):
        prev = "A"
        cnt = 0
        for e in s:
            if e == prev:
                cnt += 1
                prev = "A"
            else:
                prev = e

        return cnt

    li = [f(s*i) for i in range(6)]

    if k < 6:
        ans = li[k]

    else:
        idx = 5 - (k + 1) % 2
        d = li[idx] - li[idx-2]
        ans = li[idx] + d * (k - idx) // 2
        
        

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def olcnt(s):
    i = 0
    lst = []
    while i < len(s):
        j = 1
        while i + j < len(s) and s[i] == s[i + j]:
            j += 1
        lst.append((s[i], j))
        i += j
    return lst

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

if len(set(s)) == 1:
    print(int(len(s)*k/2))
else:
    cnt1 = sum([int(x[1]/2) for x in olcnt(s)])
    cnt2 = sum([int(x[1]/2) for x in olcnt(s+s)])

    print(cnt1 + (cnt2-cnt1)*(k-1))

Solution (Python):
def f(s, k):
    n = len(s)
    if len(set(s)) == 1:
        return n * k // 2
    ans = 0

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

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

assert f("aba", 1) == 0
assert f("aba", 2) == 1 
assert f("aba", 4) == 3 
assert f("aaa", 4) == 6 
assert f("aaa", 3) == 4 
assert f("aaa", 5) == 7 
assert f("aa", 1) == 1 
assert f("aa", 2) == 2 
assert f("aa", 3) == 3 
assert f("aa", 4) == 4 
assert f("aaa", 1) == 1 
assert f("aaa", 2) == 3 
assert f("aaaa", 1) == 2 
assert f("aaaa", 2) == 4 
assert f("aaaa", 3) == 6 
assert f("qq", 81) == 81
assert f("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert f("issii", 2) == 4 

s = input()
k = int(input())
ans = f(s, k)
print(ans)

Solution (Python):
s = input()

k = int(input())

n = len(s)

res = 0

i = 0
l = 1

R = []

while i<n :
    if i==n-1 :
        R.append(l)
        break
    if s[i]==s[i+1] :
        l = l+1
    else :
        R.append(l)
        l = 1
    i = i+1

m = len(R)
for i in range(m) :
    if i==0 :
        if s[0]==s[n-1] :
            res += R[i]//2
        else :
            res += R[0]//2*k;
    elif i==m-1:
        if s[0]==s[n-1] :
            res += (R[m-1]+R[0])//2*(k-1)
            res += R[m-1]//2
        else :
            res += R[m-1]//2*k
    else :
        res += R[i]//2*k


if len(R)==1 :
    print(n*k//2)
else:
    print(res)

Solution (Python):
from itertools import*
s,k=open(0)
k,x=int(k),0
g=[len(list(v))for _,v in groupby(s)]
for c in g:x+=c//2
x*=k
if(s[0]==s[-2])*g[0]%2&g[-2]%2:x+=k//2if-len(s)==~g[0]else k-1
print(x)

Solution (Python):
from itertools import*
s,k=open(0)
k,x=int(k),0
g=[len(list(v))for _,v in groupby(s)]
for c in g:x+=c//2
x*=k
if(s[0]==s[-2])*g[0]%2&g[-2]%2:x+=k//2if-len(s)==~g[0]else k-1
print(x)

Solution (Python):
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
tmp = s[0]
cnt = 1
base = 0
is_first = True
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		if is_first:
			is_first = False
			first = cnt//2
		else:
			base += cnt//2
		tmp = s[i]
		cnt = 1

if s[0] != tmp:
	base += cnt//2
	print((first+base)*k)
else:
	i = 0
	last = cnt//2
	while s[i] == tmp:
		cnt += 1
		i += 1
	base += cnt//2
	
	print(first + base*k - cnt//2 + last)

Solution (Python):
s=input()
k=int(input())
ans=0
from itertools import*
g=[len(list(v))for k,v in groupby(s)]
for i in g:
    ans+=i//2
ans*=k
if s[0]==s[-1] and g[0]%2>0 and g[-1]%2>0:
    if len(s)==g[0]:
        ans+=k//2
    else:
        ans+=k-1
print(ans)

Solution (Python):

def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        same = 1
        for i in range(0, len(s)-1):
            if s[i] == s[i+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
        ans += replace_count(same) * k
        if s[0] == s[i]:
            a = 1
            b = 1
            a_idx = 0
            b_idx = -1
            while s[a_idx] == s[a_idx+1]:
                a += 1
                a_idx += 1
            while s[b_idx] == s[b_idx-1]:
                b += 1
                b_idx -= 1
            dup = (replace_count(a) + replace_count(b) - replace_count(a+b)) * (k-1)
            ans -= dup

    return ans


def replace_count(n):
    return n//2

main()

Solution (Python):
def count_difference(S):
    last = ''
    cnt = 0
    for s in S:
        if(s==last):
            cnt+=1
            last='0'
        else:
            last=s
    return cnt,last

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

cnt_diff = 0
last = ''
cnt_diff, last = count_difference(S)
if(len(S)==S.count(S[0])):
    print(len(S)*K//2)
elif(S[0]==S[-1] and last!='0'):
    cnt_diff2,_ = count_difference(S*2)
    if(cnt_diff2==cnt_diff*2):
        print(cnt_diff*K)
    else:
        print(cnt_diff+(cnt_diff+1)*(K-1))
else:
    print(cnt_diff*K)

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


K = int(K)
ans = 0
S = [c for c in S]

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

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

ans = 0
for i in cnt:
    ans += (i//2) * K

if S[0] == S[len(S)-1]:
    
    
    
    if cnt[0]%2 == cnt[-1]%2 ==1:
        ans += K - 1
print(ans)

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

s_list = list(S)

modified = "mdf"

def solve():
    
    if len(set(s_list)) == 1:
        return (len(s_list)*K)//2
    
    cnt = 1
    cnt_list = []
    for i in range(len(s_list)-1):
        if s_list[i] == s_list[i+1]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    
    
    
    
    cnt_list.append(cnt)
    
    
    head = cnt_list[0]
    tail = cnt_list[-1]
    body = cnt_list[1:-1]
    
    def body_change_cnt(body):
        cnt = 0
        for b in body:
            cnt += b//2
        return cnt
    
    if s_list[0] == s_list[-1]:
        head_tail = cnt_list[0] + cnt_list[-1]
        return head//2 + tail//2 + body_change_cnt(body)*K + (head_tail//2)*(K-1)
    else:
        return (head//2 + tail//2 + body_change_cnt(body)) * K

result = solve()

print(result)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)

def main():
    s = input()
    k = int(input())
    n = len(s)
    
    pri = 1
    suf = 0
    for i in range(n - 1):
        if s[i] != s[i + 1]:
            pri = i + 1
            break
    else:
        print(n * k // 2)
        exit()
    for i in range(n - 1, 0, -1):
        if s[i] != s[0]: break
        suf += 1
    pre = s[pri]
    mid = 0
    cnt = 1
    for i in range(pri + 1, n):
        if pre == s[i]:
            cnt += 1
        else:
            mid += cnt // 2
            cnt = 1
            pre = s[i]
    if s[0] != s[-1]:
        mid += cnt // 2
    
    print(mid * k + pri // 2 + suf // 2 + ((pri + suf) // 2) * (k - 1))

main()

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10**9
MOD = 10**9+7

S, K = input(), int(input())
N = len(S)
if S[0] == S[-1]:
    i = 1
    while i < N and S[i-1] == S[i]:
        i += 1
    j = -1
    while j > -N and S[j] == S[j-1]:
        j -= 1
    if N+j <= i:
        print(N*K//2)
    else:
        ans = (K-1)*((i-j)//2)+i//2+(-j)//2
        S = S[i:j]
        l, r = 0, 0
        n = len(S)
        lst = []
        while r < n:
            while r+1 < n and S[r] == S[r+1]:
                r += 1
            r += 1
            lst.append(r-l)
            l = r
        for x in lst:
            ans += K*(x//2)
        print(ans)
else:
    l, r = 0, 0
    lst = []
    while r < N:
        while r+1 < N and S[r] == S[r+1]:
            r += 1
        r += 1
        lst.append(r-l)
        l = r
    ans = 0
    for x in lst:
        ans += K*(x//2)
    print(ans)

Solution (Python):
s=list(input())
K=int(input())
import copy
S=copy.deepcopy(s)
F=0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        F=1
        break
if F==0:
    print((len(S)*K)//2)
elif K==1:
    ans=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans+=1
            S[i]="
    print(ans)
else:
    ans1=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans1+=1
            S[i]="
    ans2=0
    S2=s*2
    
    for i in range(1,len(S2)):
        if S2[i]==S2[i-1]:
            ans2+=1
            S2[i]="
    print(ans1+(ans2-ans1)*(K-1))

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

pre_c = ''
flg = 0
count = 1

f_c = S[0]
f_e = S[-1]
t = []

for c in S:
  if c == pre_c:
    count += 1
  elif pre_c == '':
    pass
  else:
    t.append(count)
    count = 1
  pre_c = c

t.append(count)
count_1 = 0
for i in t:
  count_1 += i // 2
  
if f_c == f_e and len(t) != 1:
  t[0] = t[0] + t[-1]
  t.pop(-1)

count_2 = 0
for i in t:
  count_2 += i // 2
  
if len(S) == 1:
  print(K // 2)
elif len(t) == 1:
  print(K * len(S) // 2)
else:
  print(count_1 + count_2 * (K - 1))

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

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