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=int(input())

ck=0
ne=True

if len(set(s))==1:
    print((len(s)*k)//2)
    ne=False
elif s[0]==s[-1]:
    for i in range(len(s)):
       if s[i] != s[0]:
           s=s[i:]+s[:i]
           ck=i
           break

if ne:    
    
    ans=0
    
    M=[]
    now=s[0]
    cnt=1
    for c in s[1:]:
        if c==now:
            cnt+=1
        else:
            M.append(cnt)
            
            ans+=cnt//2
            cnt=1
            now=c
            
    M.append(cnt)
    ans+=cnt//2
    
    
    ans=ans*k
    
    
    
    if ck>0:
       for i in range(1,len(s)):
           if s[-i] != s[-1]:
               ck2=i-1
               break
       ans-=ck2//2
       ans+=(ck2-ck)//2+ck//2
    
    print(ans)

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

Solution (Python):
s=input()
k=int(input())
if len(s)==1:
  print(k//2)
else:
  ans=0
  chg=0
  for i in range(1,len(s)):
    if chg==0 and s[i-1]==s[i]:
      ans+=1
      chg=1
    else:
      chg=0
  ans2=1
  chg2=1
  for i in range(1,len(s)):
    if chg2==0 and s[i-1]==s[i]:
      ans2+=1
      chg2=1
    else:
      chg2=0
  if s[0]!=s[-1] or chg==1:
    print(k*ans)
  elif chg2==0:
    print((k-1)*ans2+ans)
  else:
    print(ans*((k+1)//2)+ans2*(k//2))

Solution (Python):




 
from collections import* 
from fractions import gcd
from functools import* 
from itertools import* 
from operator import mul,itemgetter
from bisect import* 
from heapq import* 
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)

 
def main():
    s=input()
    k=int(input())
    ans=0
    n=[len(list(v)) for k,v in groupby(s)]

    if s[0]!=s[-1]:
        y=[len(list(v))//2 for k,v in groupby(s)]
        print(sum(y)*k)
    else:
        x=[len(list(v))//2 for k,v in groupby(s)]

        if len(x)!=1:
            ans+=x[0]+x[-1]

            ans+=sum(x[1:-1])*k

            ans+=(k-1)*((n[0]+n[-1])//2)
            print(ans)
        else:
            print(len(s)*k//2)
        
    


if __name__ == '__main__':
    main()

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


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

pre = None
if K < 10:
    first_cnt = 0
    for s in S*K:
        if s == pre:
            first_cnt += 1
            pre = None
        else:
            pre = s
    print(first_cnt)
    exit()

pre = S[0]
first_cnt = 0

for s in S[1:]:
    if s == pre:
        first_cnt += 1
        pre = None
    else:
        pre = s

continuous_cnt = 0
for s in S:
    if s == pre:
        continuous_cnt += 1
        pre = None
    else:
        pre = s

continuous_cnt2 = 0
for s in S:
    if s == pre:
        continuous_cnt2 += 1
        pre = None
    else:
        pre = s

if continuous_cnt == continuous_cnt2:
    print(first_cnt+continuous_cnt*(K-1))
elif K % 2 == 1:
    print(first_cnt+(continuous_cnt+continuous_cnt2)*((K-1)//2))
else:
    print(first_cnt+continuous_cnt*((K-1)//2+1)+continuous_cnt2*((K-1)//2))

Solution (Python):
S = input()
K = int(input())
all_same = False
for i, (s, t) in enumerate(zip(S, S[1:])):
    if s != t:
        break
else:
    all_same = True

if all_same:
    ans = (K * len(S)) // 2
else:
    first, last = 0, 0
    if S[0] == S[-1]:
        for j, t in enumerate(reversed(S)):
            if t != S[0]: break
        first, last = i+1, j
        S = S[i+1:-j]        
    seq = []
    temp = 1
    for s, t in zip(S, S[1:]):
        if s == t: temp += 1
        else:
            if temp != 1: seq.append(temp)
            temp = 1
    if temp != 0:
        seq.append(temp)
    ans = first // 2 + last // 2 + ((first + last)//2) * (K-1)
    for se in seq:
        ans += (se // 2) * K
print(ans)

Solution (Python):
from itertools import groupby

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

if n > 1:
    a = [len(list(g)) for _, g in groupby(s)]

    if len(a) == 1:
        print(n * k // 2)
    else:
        answer = sum([i // 2 for i in a]) * k

        if s[0] == s[-1] and a[0] % 2 == 1 and a[-1] % 2 == 1:
            answer += k - 1            

        print(answer)
else:
    print(k // 2)

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

change = []
tmp = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    else:
        change.append(tmp)
        tmp = 1
change.append(tmp)



ret = 0
if len(change) >= 3:

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

print(ret)

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

s_pre = s[0]
cnt = 1
s2 = []
for x in s[1:]:
    if s_pre == x:
        cnt += 1
    else:
        s2 += [cnt]
        cnt = 1
        s_pre = x
s2 += [cnt]

def div2(x):
    return x//2

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

elif s[0] == s[-1]:
    print(s2[0]//2 + s2[-1]//2 + (s2[0]+s2[-1])//2*(k-1) + sum(map(div2, s2[1:-1]))*k)

else:
    print(sum(map(div2, s2))*k)

Solution (Python):
import math
import sys
import itertools
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

s = S()
K = I()
N = len(s)

start = s[0]
left = 0
right = 0
f = 0
for i in range(N):
  if f == 0:
    if s[i] == start:
      left = i
    else:
      f = 1
if f == 0:
  ans = N*K//2
  print(ans)
else:
  if s[0] == s[-1]:
    f = 0
    for i in range(N-1,-1,-1):
      if f == 0:
        if s[i] == start:
          right = i
        else:
          f = 1
    left = left +1

    
    
    
    ans = ((left + (N-right))//2)*(K-1) + left//2  + (N-right)//2
  else:
    ans = 0
    left = 0
    right = N
  tmp = 0
  t = ""
  for i in range(left,right):
    if t == s[i]:
      tmp += 1
    else:
      t = s[i]
      tmp = 1
    if tmp%2 == 0:
      ans += K
  print(ans)

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

Count = []
c = []
pre = ''
for s in S:
  if s != pre:
    if len(c) != 0:
      Count.append(c)
    c = [s,1]
    pre = s
  else:
    c[1] += 1
if len(c) != 0:
  Count.append(c)

if len(Count) == 1:
  ans = Count[0][1]*K//2
else:
  ans = 0
  for i in range(len(Count)):
    ans += Count[i][1]//2
  ans *= K
  if Count[0][0] == Count[-1][0] and Count[0][1] % 2 == 1 and Count[-1][1] % 2 == 1:
    ans += K-1
print(ans)

Solution (Python):
S = list(input())
K = int(input())
R = []
hoge = 1
for i in range(1,len(S)):
    if S[i] == S[i-1]:
        hoge += 1
    else:
        R.append(hoge)
        hoge = 1
R.append(hoge)
ans = 0
if len(R) == 1:
    print((R[0]*K)//2)
    exit()
if S[0] == S[-1]:
    ans += (K-1)*((R[0]+R[-1])//2) + R[0]//2 + R[-1]//2
    R[0],R[-1] = 0,0
for i in R:
    ans += (i//2)*K 
print(ans)

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

    prev = s[0]
    prev_idx = 0
    idx = 1

    acm = []
    while idx < len(s):
        if s[idx] != prev:
            acm.append(idx - prev_idx)
            prev_idx = idx
            prev = s[idx]
        idx += 1

    acm.append(idx - prev_idx)

    bl = True
    for i in range(len(s) - 1):
        bl = bl and (s[i] == s[i+1])

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

    elif s[0] == s[-1]:
        ad = acm[0] + acm[-1]
        acm2 = acm[1:-1] + [ad]
        tmp = 0
        for e in acm2:
            tmp += e // 2
        tmp2 = 0
        for e in acm:
            tmp2 += e // 2
        ans = tmp * (k - 1) + tmp2

    else:
        tmp = 0
        for e in acm:
            tmp += e // 2
        ans = tmp * k

    print(ans)


if __name__ == "__main__":
    main()

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

S = readline().rstrip().decode('utf-8')
K = int(read())



def F(S):
    arr = list(S)
    prev = 'A' 
    cnt = 0
    for x in S:
        if x == prev:
            cnt += 1
            prev = 'A'
        else:
            prev = x
    return cnt

arr = [F(S*i) for i in range(10)]

if K < 10:
    answer = arr[K]
else:
    if K&1:
        d = arr[9] - arr[7]
        answer = arr[9] + d * ((K-9)//2)
    else:
        d = arr[8] - arr[6]
        answer = arr[8] + d * ((K-8)//2)
print(answer)

Solution (Python):
import sys
input=sys.stdin.readline
s=input().rstrip()
k=int(input())
n=len(s)
if s[n-1]==s[0]:
    x=s[0]
    a=0
    while a<n:
        if s[a]==x:
            a+=1
        else:
            break
    b=0
    while b<n:
        if s[n-1-b]==x:
            b+=1
        else:
            break
    if a==n:
        ans=(n*k)//2
        print(ans)
    else:
        ans=0
        p='!'
        l=0
        for i in range(a,n-b):
            if s[i]==p:
                l+=1
            else:
                ans+=l//2
                l=1
            p=s[i]
        ans+=l//2
        ans=ans*k+a//2+b//2+((a+b)//2)*(k-1)
        print(ans)
else:
    ans=0
    p='!'
    l=0
    for i in range(n):
        if s[i]==p:
            l+=1
        else:
            ans+=l//2
            l=1
        p=s[i]
    ans+=l//2
    print(ans*k)

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


if s.count(s[0]) == len(s):
    print(len(s)*K//2)
elif len(s) >= 3:
    ans1 = 0
    ans2 = 0
    use = [True]*len(s)
    use2 = [True]*len(s)
    for i in range(len(s)-1):
        if use[i+1] and use[i]:
            if s[i+1] == s[i]:
                ans1 += 1
                use[i+1] = False

    if use[-1] and use2[0]:
        if s[-1] == s[0]:
            ans2 += 1
            use2[0] = False

    for i in range(len(s)-1):
        if use2[i+1] and use2[i]:
            if s[i+1] == s[i]:
                ans2 += 1
                use2[i+1] = False

    print(ans1+ans2*(K-1))

else:
    if len(s) == 1:
        print(K//2)
    else:
        if s[0] == s[1]:
            print(K)
        else:
            print(0)

Solution (Python):




def main():
  s = input().strip()
  k = int(input())
  mode = s[0] == s[-1]

  pre = 1
  idx = 1

  while idx < len(s) and s[idx] == s[0]:
    pre += 1
    idx += 1

  s = s[idx:]

  if len(s) == 0:
    print(pre * k // 2)
    return

  post = 1
  idx = -2

  while idx >= -len(s) and s[idx] == s[-1]:
    post += 1
    idx -= 1

  s = s[:idx + 1]

  required = 0
  count = 1

  for i in range(1, len(s)) :
    if s[i - 1] == s[i]:
      count += 1
    else:
      required += count // 2
      count = 1

  required += count // 2
  required *= k
  required += pre // 2
  required += post // 2

  if mode:
    required += ((pre + post) // 2) * (k - 1)
  else:
    required += (pre // 2 + post // 2) * (k - 1)

  print(required)


if __name__ == '__main__':
  main()

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

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

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

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


f = 0
count = 0
for i in range(1, len(s)):
    if f == 1:
        f = 0
        continue

    if s[i-1] == s[i]:
        f = 1
        count += 1


if (re_same + same) % 2 == 0 and s[0] == s[len(s)-1]:
    print(count*k + k - 1)
else:
    print(count*k)

Solution (Python):




from __future__ import print_function, division, unicode_literals

__metaclass__ = type

try:
    input = raw_input
    range = xrange
except NameError:
    pass


import sys












def f(k):
    return k // 2


def rle(S):
    res = []
    last = None
    for i, c in enumerate(S):
        if c == last:
            res[-1] = (last, res[-1][1] + 1)
        else:
            res.append((c, 1))
            last = c
    return res


def solve(S, K):
    N = len(S)
    x = S[0]
    i = 0
    j = N
    while i < N and S[i] == x:
        i += 1
    if i == N:
        return f(N * K)
    while j > 0 and S[j - 1] == x:
        j -= 1
    if j == N or K == 1:
        res = rle(S)
        return K * sum(f(k) for _, k in rle(S))
    res = rle(S)
    return f(i) + f(N - j) + (K - 1) * f(i + N - j)\
        + K * sum(f(k) for _, k in res[1:-1])


def main():
    S = input()
    K = int(input())
    print(solve(S, K))


if __name__ == "__main__":
    main()

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

N = len(S)
first = S[0]
last = S[-1]
now = 1
ing = 0
a = []
cnt = 1
flag = 0
if N > 1:
    while now < N:
        if S[now] == S[now-1]:
            ing = 1
            cnt += 1
            now += 1
            continue
        else:
            if ing ==1:
                a.append(cnt)
                cnt = 1
                now +=1
                ing = 0
            else:
                now +=1
                cnt = 1
                ing = 0
    if ing ==1:
        a.append(cnt)

    output = 0
    for j in range(len(a)):
        output += a[j]//2

    if first==last:
        if S[0]==S[1]:
            if S[-1]==S[-2]:
                X =a[0]
                Y =a[-1]
            else:
                X = a[0]
                Y = 1
        else:
            if S[-1]==S[-2]:
                X = 1
                Y =a[-1]
            else:
                X = 1
                Y = 1

        if (X==1)and(Y==1):
            output += 1
            flag = 1
        elif X==1:
            if Y%2==1:
                output+=1
                flag = 1
        elif Y==1:
            if X%2==1:
                output+=1
                flag = 1
        else:
            if (X%2==1)and(Y%2==1):
                output += 1
                flag = 1

    output = output * K - flag
    if (len(a)==1)and(a[0]==N):
        output = (K*N)//2
elif N == 1:
    output = K//2

print(output)

Solution (Python):

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

t1 = 0
t2 = 0
if s[0] == s[-1]:
    for i in range(len(s)):
        if s[i] == s[0]:
            t1 += 1
        else:
            break
    s = s[::-1]
    for i in range(len(s)):
        if s[i] == s[0]:
            t2 += 1
        else:
            break
    if len(set(s)) == 1:
        t = len(s)
    s = s[::-1]
    t = t1 + t2

t = t1 + t2
ss = t//2
ans = 0
tmp = 0

for i in range(t1, len(s)-t2):
    
    
    if i == t1:
        tmp = 1
    elif t1 < i < len(s)-t2-1:
        if s[i] == s[i-1]:
            tmp += 1
            
        else:
            ans += tmp//2
            tmp = 1
    else:
        if s[i] == s[i-1]:
            tmp += 1
            ans += tmp //2
        else:
            ans += tmp //2
            tmp = 1

if len(set(s)) == 1:
    anser = len(s) * k //2
    print(anser)
else:
    anser = ss*(k-1) + ans * k + t1//2 + t2//2
    print(anser)

Solution (Python):
l=list(input())
k=int(input())
n=len(l)
if n==1:
    if k%2==0:
        print(k//2)
    else:
        print((k-1)//2)
    exit()
al=[]
su=[]
f=1
for i in range(n-1):
    if l[i]!=l[i+1]:
        al.append(l[i])
        su.append(f)
        f=1
    else:
        f+=1
if l[-2]==l[-1]:
    al.append(l[-1])
    su.append(f)
else:
    al.append(l[-1])
    su.append(1)
    
if len(al)==1:
    j=su[0]*k
    if j%2==0:
        print(j//2)
    else:
        print((j-1)//2)
    exit()

if al[0]!=al[-1]:
    ans=0
    for i in su:
        if i%2==0:
            ans+=i//2
        else:
            ans+=(i-1)//2
    print(k*ans)
else:
    ans=0
    if su[0]%2==0:
        ans+=su[0]//2
    else:
        ans+=(su[0]-1)//2
    tmp=0
    for i in range(len(al)-2):
        if su[i+1]%2==0:
            tmp+=su[i+1]//2
        else:
            tmp+=(su[i+1]-1)//2
    ans+=tmp*k
    j=su[0]+su[-1]
    if j%2==0:
        ans+=(j//2)*(k-1)
    else:
        ans+=((j-1)//2)*(k-1)
    if su[-1]%2==0:
        ans+=su[-1]//2
    else:
        ans+=(su[-1]-1)//2
    print(ans)

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

ans = 0
def aa(s):
    ansaa=0
    a = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            a += 1
        else:
            if a > 0:
                ansaa += (a+1)//2
                a = 0
    ansaa += (a+1)//2    
    return ansaa




l = -1
r = 0
if s[0] == s[-1]:
    ss = s[0]
    while l >= -n and s[l] == ss:
        l -= 1
    while r < n and s[r] == ss:
        r += 1
    
    if l == -n-1:
        ans = (n*k)//2
    else:
        ans += ((r-l-1)//2)*(k-1)
        
        ans += aa(s[r:n+l+1])*k
        ans += aa(s[:r])
        ans += aa(s[n+l+1:])
else:
    ans += aa(s)*k

print(ans)

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

lst = []
ch = ''
cnt = 0
for c in s:
    if c == ch:
        cnt += 1
    else :
        lst.append([ch, cnt])
        ch = c
        cnt = 1
lst.append([ch, cnt])
ans = 0
if len(lst) == 2:
    print((lst[1][1] * k) // 2)
    exit()
for ch, cnt in lst:
    ans += cnt // 2

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

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

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

Solution (Python):
import sys

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

if len(S) == 1:
    print(K//2)
    sys.exit()
if len(S) == 2:
    if S[0] == S[1]:
        print(K)
    else:
        print('0')
    sys.exit()

olds = S[0]
l = 1
c = 0
first = 1
for i in range(1,len(S)):

    if olds == S[i]:
        l += 1
    else:
        if first:
            firstL = l
            first = 0
        c += l//2
        l = 1
    olds = S[i]
    
    if i == len(S)-1:
        if first:
            print(len(S)*K//2)
            sys.exit()
        lastL = l
        c += l//2

if S[0] == S[-1] and firstL%2 == 1 and lastL%2 == 1:
    print((c+1)*K-1)
else:
    print(c*K)

Solution (Python):
import sys
s=input()
k=int(input())
f=0
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        f=1
        break
if f==0:
    print(len(s)*k//2)
    sys.exit()
if k>=3:
    ss=s+s+s
else:
    ss=s*k
flag=[0]*len(ss)
for i in range(1,len(ss)):
    if ss[i-1]==ss[i] and flag[i-1]==0:
        flag[i]=1
        continue
if k>=3:
    count=sum(flag[0:len(s)])+sum(flag[len(s):2*len(s)])*(k-2)+sum(flag[2*len(s):3*len(s)])
else:
    count=sum(flag)
print(count)

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
if N == 1:
    print(K // 2)
else:
    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, collections, copy

def solve():
    S = list(input())
    S2 = copy.deepcopy(S)
    K = int(input())
    lens = len(S)
    head_change = 0
    for i in range(1, lens):
        if S[i] == S[i - 1]:
            head_change += 1
            S[i] = "Changed"
    
    inner_change = 1
    S2[0] = "Changed"
    for i in range(1, lens):
        if S2[i] == S2[i-1]:
            inner_change += 1
            S2[i] = "Changed"

    if lens == 1: print(K // 2)
    elif S[lens-1] != "Changed" and S[0] == S[lens - 1]:
        if S2[lens-1] != "Changed":
            print(head_change + inner_change * (K - 1))
        else:
            print((head_change + inner_change) * (K // 2) + head_change * (K % 2))
    else:
        print(head_change * K)
    

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
S = input()
K = int(input())
ns = []
c = S[0]
n = 1
for i in range(1, len(S)):
  if c == S[i]:
    n += 1
  else:
    ns.append(n)
    n = 1
  c = S[i]
ns.append(n)

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

Solution (Python):


from collections import Counter
def main():
    S = input()
    K = int( input())
    CS = Counter(S)
    if CS[ S[0]] == len(S):
        print( len(S)*K//2)
        return
    N = len(S)

    for i in range(N-1):
        if S[i] != S[i+1]:
            check = i+1
            break

    T = S[check:] + S[:check]
    ans = 0
    now = S[0]
    cnt = 1

    for t in S[1:check]:
        if now == t:
            cnt += 1
            continue
        ans += cnt//2
        cnt = 1
        now = t
    ans += cnt//2

    cnt = 1
    now = S[check]
    for t in S[check+1:]:
        if now == t:
            cnt += 1
            continue
        ans += cnt//2
        cnt = 1
        now = t
    ans += cnt//2
    
    cnt = 1
    now = T[0]
    cc = 0

    for t in T[1:]:
        if t == now:
            cnt += 1
            continue
        cc += cnt//2
        cnt = 1
        now = t
    cc += cnt //2
    ans += cc*(K-1)
    
    print(ans)
        
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import groupby
    s=input()
    k=int(input())
    t=groupby(s)
    S=[]
    for tup in t: S.append((tup[0],len(list(tup[1]))))
    if len(S)==1:
        print(len(s)*k//2)
        return
    if s[0]==s[-1]:
        p=0 
        for a,b in S[1:-1]:
            p+=b//2
        q=(S[0][1]+S[-1][1])//2
        ans=p*k+q*(k-1)
        ans+=S[0][1]//2+S[-1][1]//2
        print(ans)
    else:
        p=0
        for a,b in S:
            p+=b//2
        ans=p*k
        print(ans)
resolve()

Solution (Python):


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

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

    R = []
    lastLetter = None
    cnt = 0

    for c in S:
        if lastLetter == c:
            cnt += 1
        else:
            lastLetter = c
            if cnt > 0:
                R.append(cnt)

            cnt = 1
    
    R.append(cnt)

    if len(R) == 1:
        print(len(S)*K//2)
        return

    S = 1 if len(R) > 1 and S[0] == S[-1] and R[0]%2 == 1 and R[-1]%2 == 1 else 0

    x = sum(r//2 for r in R)*K + (K-1)*S

    print(x)




if __name__ == '__main__':
    main()

Solution (Python):
S = input()
K = int(input())
pre_s = ''
cnt = 1
ans = 0
flag = False
for s in S:
    if s == pre_s:
        cnt += 1
    else:
        ans += cnt // 2
        pre_s = s
        cnt = 1
ans += cnt // 2

if S[0] == S[-1]:
    pre_s = S[0]
    cnt = 1
    for s in S[1:]:
        if s == pre_s:
            cnt += 1
        else:
            break
    end_cnt = 1
    for s in reversed(S[:-1]):
        if s == pre_s:
            end_cnt += 1
        else:
            break
    if (end_cnt + cnt) % 2 == 0 and end_cnt % 2 == 1:
        ans += 1
        flag = True
        
if len(set(S)) == 1 and len(S) % 2 == 1:
    print((len(S)//2)*(K//2+K%2) + (len(S)//2+1)*(K//2))
elif flag:
    print(ans*K-1)
else:
    print(ans*K)

Solution (Python):

import sys
INF = float("inf")


def Run_length_encoding(S: str):
    """AAABBC -> [(A, 3), (B, 2), (C, 1)]
    """
    ans = []
    curr = S[0]
    counter = 0
    for c in S:
        if c == curr:
            counter += 1
        else:
            ans.append((curr, counter))
            curr = c
            counter = 1
    ans.append((curr, counter))
    return ans


def solve(S: str, K: int):

    rle = Run_length_encoding(S)
    fchr, fcnt = rle[0]
    lchr, lcnt = rle[-1]
    if len(rle) == 1:
        print(fcnt*K//2)
        return

    ans = 0
    for c, cnt in rle:
        ans += cnt//2
    ans *= K

    if K > 1 and fchr == lchr:
        ans -= K*(fcnt//2)
        ans -= K*(lcnt//2)
        ans += (K-1)*((fcnt+lcnt)//2)
        ans += fcnt//2
        ans += lcnt//2

    print(ans)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    K = int(next(tokens))  
    solve(S, K)


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, K):
    ans = 0
    S = [c for c in S]
    if K < 4:
        S = S * K
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                ans += 1
                S[i+1] = '-'
        return ans
    
    N = len(S)
    S = S * (4 if K % 2 == 0 else 5)
    a = [0] * (4 if K % 2 == 0 else 5)
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            a[(i+1)//N] += 1
            S[i+1] = '-'

    o = math.ceil((K-2) / 2)
    e = (K-2) // 2
    return a[0] + a[-1] + a[1] * o + a[2] * e


def main():
    S = read_str()
    K = read_int()
    print(slv(S, K))


if __name__ == '__main__':
    main()

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

ls = len(S)

moreWriteNeeded = False

if S[0] == S[ls-1]:

    i = 1
    while i < ls and S[i] == S[0]:
        i += 1

    if i == ls:
        print((ls * K) // 2)
        exit()

    if i % 2 == 1:
        i = 1
        while S[ls - i - 1] == S[0]:
            i += 1

        if i % 2 == 1:
            moreWriteNeeded = True

write = 0

last = "Z"
for c in S:
    if last == c:
        last = "Z"
        write += 1
    else:
        last = c

ans = K * write if not moreWriteNeeded else K * write + K - 1

print(ans)

Solution (Python):

from fractions import gcd

from math import factorial as f

from math import ceil,floor,sqrt

from sys import exit

from copy import deepcopy
import numpy as np

import bisect
import re


ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))



def main():
    s = input()
    k = ii()
    n = len(s)

    s = list(s)
    ss = deepcopy(s)

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



    sc = deepcopy(s)

    cnt = 0
    for i in range(1,n):
        if sc[i] == sc[i-1]:
            sc[i] = '0'
            cnt+=1
    
    if sc[n-1] == sc[n-2] or sc[0] == sc[n-1]:
        flag = True
    else:
        flag = False

    ans = cnt*k+(flag*(k-1))

    cnt = 1
    s[0] = '0'
    for i in range(1,n):
        if s[i] == s[i-1]:
            s[i] = '0'
            cnt+=1
    
    if s[n-1] == s[n-2] or s[0] == s[n-1]:
        flag = True
    else:
        flag = False

    ans2 = cnt*k+(flag*k)



    print(min(ans,ans2))





main()

Solution (Python):
import sys, math; readline = sys.stdin.readline; from collections import deque;

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

a = []

_c = None
_count = 0
for c in S:
    if c == _c:
        a[-1] += 1
    else:
        a.append(1)
    _c = c
total = 0

if len(a) >= 2:
    for x in a[1:-1]:
        total += x // 2 * K

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

print(total)

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