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):
def main():
    S = input()
    K = int(input())
    l = []
    t = 1
    m = S[0]
    if len(S) == 1:
        print(K//2)
        return
    if len(S) == 2:
        if S[0] == S[1]:
            print(K)
            return
        else:
            print(0)
            return
    if len(S) == 3:
        if S[0] == S[1] == S[2]:
            print(3*K//2)
            return
        elif S[0] == S[1] or S[1] == S[2]:
            print(K)
            return
        elif S[0] == S[2]:
            print(K-1)
            return
        else:
            print(0)
            return
    for v in S[1:]:
        if v == m:
            t += 1
        elif t != 1:
            l.append(t)
            t = 1
        m = v
    if t != 1:
        l.append(t)
    ls = 0
    lsm = 0
    lsu = 0
    if S[0] == S[-1]:
        if len(l) == 1 and l[0] == len(S):
            print(l[0] * K//2)
            return
        elif len(l) == 1:
            print(((l[0] + 1)//2) *(K-1) + (l[0] // 2))
            return
        if S[0] == S[1] == S[-2]:
            ls = l[-1] + l[0]
            lsm = l[0]
            lsu = l[-1]
            l = l[1:-1]
        elif S[-1] == S[-2]:
            ls = l[-1] + 1
            lsu = l[-1]
            l = l[:-1]
        elif S[0] == S[1]:
            ls = l[0] + 1
            lsm = l[0]
            l = l[1:]
        else:
            ls = 2
    r = 0
    for i in l:
        r += i // 2
    r *= K
    r += (K-1) * (ls // 2)
    r += (lsm  // 2) + (lsu // 2)
    print(r)
main()

Solution (Python):
def main():
    S = [i for i in input()]
    O = S[:]
    K = int(input())
    if len(S) == 1:
        return K // 2
    r = 0
    for i in range(1, len(S)):
        if S[i] == S[i - 1]:
            r += 1
            S[i] = S[i].upper()
    if S[-1] != S[0]:
        return r * K

    S = S + O
    p = 0
    for i in range(len(O), len(S)):
        if S[i] == S[i - 1]:
            p += 1
            S[i] = S[i].upper()
    if S[-1] == S[0]:
        return r + p * (K - 1)
    return (r + p) * (K // 2) + r * (K % 2)
   

print(main())

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

from collections import Counter

s = ns()
n = len(s)
s = s + "$"
k = ni()

c = []
cnt = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        cnt += 1
    elif cnt > 1:
        c.append(cnt)
        cnt = 1

if (len(c) == 1 and c[0] == n) or n == 1:
    print(n * k // 2)
elif s[0] != s[-2]:
    print(sum([x // 2 for x in c[:]]) * k)
else:
    a, b = 1, 1
    if s[0] == s[1]:
        a = c[0]
    if s[-2] == s[-3]:
        b = c[-1]
    print(sum([x // 2 for x in c[:]]) * k - (a // 2 + b // 2 - (a + b) // 2) * (k - 1))

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

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K // 2
elif len(set(S)) == 1:
    ans = len(S) * K // 2
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

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

def count(s):
    """何文字変更が必要か
    """
    a = []
    b = ''
    c = 1
    for si in s:
        if b == si:
            c += 1
        else:
            a.append(c)
            c = 1
        b = si
    a.append(c)

    r = 0
    for ai in a:
        r += ai // 2
    
    return r

if K == 1:
    ans = count(S)
elif len(S) == 1:
    ans = K // 2
elif len(set(S)) == 1:
    c = count(S*2)
    if K % 2 == 0:
        ans = c * (K//2)
    else:
        diff = count(S*3) - count(S*2)
        ans = c * (K//2) + diff
else:
    a1 = count(S*2)
    a2 = count(S*3)
    ans = a1 + (a2-a1) * (K-2)

print(ans)

Solution (Python):
S=input()
K=int(input())
h=1
b=0
l=[]
f=False
c1=0
c2=0
c3=0
c4=0
r=[]
v=[]
vf=False
vf1=False
vf2=False
for i in range(len(S)-1):
    if S[i]==S[i+1]:
        if i==0:
            c1=1
            c3=1
        if (i+1)==(len(S)-1):
            c2=1
            c4=1
        h+=1
        f=True
    else:
        f=False
    if not f:
        if h!=1:
            l.append(h)
            if c3==1:
                r.append(h)
                c3=0
            if c4==1:
                r.append(h)
                c4=0
        h=1

if f:
    l.append(h)
    
    if c4==1:
        r.append(h)
        c4=0

if S[0]==S[-1]:
    
    if c1==1 and c2==1:
        if len(l)==1:
            s=len(S)*K
            print(s//2)
            exit()
        else:
            v.append(l[0])
            v.append(l[-1])
            l[-1]+=l[0]
            del l[0]
            vf=True
    elif c1==1:
        
        v.append(l[0])
        
        l[0]+=1
        v.append(l[0])
        del l[0]
        vf1=True
    elif c2==1:
        
        
        v.append(l[-1])
        l[-1]+=1
        v.append(l[-1])
        del l[-1]
        vf2=True
    else:
        if len(S)==1:
            print(K//2)
            exit()
        print(K-1)
        exit()
h=0
if vf:
    h+=v[0]//2
    h+=v[-1]//2
    for i in range(len(l)):
        if i==(len(l)-1):
            h+=(l[i]//2)*(K-1)
        else:
            h+=(l[i]//2)*K
elif vf1:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
elif vf2:
    h+=(v[1]//2)*(K-1)
    h+=(v[0]//2)
    for i in range(len(l)):
        h+=(l[i]//2)*K
else:
    for i in range(len(l)):
        h+=(l[i]//2)*K


print(h)

Solution (Python):
s=input()
n=len(s)
k=int(input())
if k==1:
  if n==1:
    print(0)
  else:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt)
else:
  single=True
  for c in s:
    if c!=s[0]:
      single=False
      break
  if single:
    print(n*k//2)
  elif s[0]!=s[-1]:
    i=1
    cnt=0
    while i<n:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k)
  else:
    i=1
    while s[i]==s[0]:
      i+=1
    kk=i
    j=-1
    while s[j]==s[0]:
      j-=1
    num=i-j-1
    cnt=0
    i+=1
    while i<=n+j:
      if s[i-1]==s[i]:
        cnt+=1
        i+=1
      i+=1
    print(cnt*k+num//2*(k-1)+kk//2+(-j-1)//2)

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

if len(set(S)) == 1:
  print(len(S)*K//2)
  exit()
  
if S[0] != S[-1]:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1,len(S)):
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt//2)
      cnt = 1
      s = S[i]
  l.append(cnt//2)
  print(sum(l)*K)
else:
  cnt = 1
  s = S[0]
  l = []
  for i in range(1, len(S)):    
    if S[i] == s:
      cnt += 1
    else:
      l.append(cnt)
      cnt = 1
      s = S[i]
  l.append(cnt)
  num = l[0]+l[-1]
  ans = (num//2)*(K-1)
  for i in range(len(l)):
    if i == 0 or i == len(l)-1:
      if l[i] > 1:
        ans += l[i]//2
    else:
      ans += (l[i]//2)*K
  print(ans)

Solution (Python):
def runlen(a):
    n = len(a)
    result = []
    count = 1
    for i in range(n):
        if i == n - 1 or a[i] != a[i + 1]:
            result.append((count, a[i]))
            count = 1
        else:
            count += 1
    return result

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

rl = runlen(S)

if len(rl) == 1:
    result = (len(S) * K) // 2
else:
    result = rl[0][0] // 2 + rl[-1][0] // 2 + K * sum(map(lambda i: rl[i][0] // 2, range(1, len(rl) - 1)))
    if rl[0][1] == rl[-1][1]:
        result += (K - 1) * ((rl[0][0] + rl[-1][0]) // 2)
    else:
        result += (K - 1) * (rl[0][0] // 2 + rl[-1][0] // 2)

print(result)

Solution (Python):
S=input()
K=int(input())
if len(S)==1:
  print(K//2)
  exit()
if len(S)==2:
  if S[0]==S[1]:
    print(K)
  else:
    print(0)
  exit()
ans=0

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


if fro==0:
  print((K*cnt)//2)
  exit()
if S[0]==S[-1]:
  if fro%2==1 and cnt%2==1:
    ans+=K-1
print(ans)

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

    if s == s[0]*n:
        print( n*k//2 )
        return 0

    cnt, t = 0, 1
    for key, value in enumerate(s):
        if key == 0: continue
        if value == s[key-1]:
            t += 1
        else:
            cnt += t // 2
            t = 1
    cnt += t//2
    left, right = 0, len(s)-1
    a, b = 0, 0
    while s[left] == s[0]:
        left += 1
        a += 1
    while s[right] == s[-1]:
        right -= 1
        b += 1

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

main()

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

ans = 0

count = 1
past = ''

for i in s:
    if i == past:
        count += 1
    else:
        ans += count // 2
        count = 1
    past = i

if count == len(s):
    print((len(s)*n)//2)
    exit()

ans += count // 2
ans *= n

if s[0] == s[-1]:
    past = s[0]
    idx = 1
    begin = 1

    while idx < len(s) and past == s[idx] :
        begin += 1
        idx += 1
    past = s[-1]
    idx = -1
    end = 1

    while idx > 0 and past == s[idx] :
        end += 1
        idx -= 1

    if begin %2 != 0 and count %2 != 0:
        ans += n - 1

print(ans)

Solution (Python):

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


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


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

a=0
for i in t:
  a+=(i//2)*k


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

print(a)

Solution (Python):
import sys
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):
s = input()
k = int(input())

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

elif s[0] != s[-1]:
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k)
else:
    a,b = 0,0
    c = s[0]
    rev_s = s[::-1]
    for i in range(len(s)):
        if s[i]==c:
            a += 1
        else:
            break
    for i in range(len(s)):
        if rev_s[i]==c:
            b += 1
        else:
            break
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s = s[:i]+'.'+s[i+1:]
    print(s.count('.')*k - (a//2 + b//2 - (a+b)//2)*(k-1))

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

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

count_list.append(count)

if len(count_list) == 1:
    print((len(s) * repeat_num ) // 2)
else:
    if s[0] != s[-1]:
        single = sum(list(map(lambda x: x//2, count_list)))
        print(single * repeat_num)
    else:
        
        single = sum(list(map(lambda x: x//2, count_list[1:-1])))
        print(single * repeat_num
            + (count_list[0]) // 2
            + (count_list[-1]) // 2
            + (count_list[0] + count_list[-1]) // 2 * (repeat_num - 1))

Solution (Python):
def RanLengthEncoding(S):
    N = len(S)
    RLE = []
    now_char = S[0]
    now_cnt = 1
    for i in range(1, N):
        if now_char == S[i]:
            now_cnt += 1
        else:
            RLE.append([now_char, now_cnt])
            now_char = S[i]
            now_cnt = 1
    RLE.append([now_char, now_cnt])
    return RLE

S = input()
K = int(input())
rle = RanLengthEncoding(S)
if len(rle) == 1:
    ans = rle[0][1] * K // 2
    print(ans)
    exit()
cnt = 0
for i in range(1, len(rle)-1):
    cnt += rle[i][1] // 2

if rle[0][0] != rle[-1][0]:
    cnt += rle[0][1] // 2
    cnt += rle[-1][1] // 2
    ans = cnt * K
else:
    tmp = (rle[0][1] + rle[-1][1]) // 2
    ans = cnt * K
    ans += tmp * (K-1)
    ans += rle[0][1] // 2
    ans += rle[-1][1] // 2
print(ans)

Solution (Python):
from math import floor

def solve(s, k):
    counts = [0] * len(s)
    prev = ''
    index = -1
    for l in s:
        if prev == l:
            counts[index] += 1
        else:
            index += 1
            counts[index] = 1
        prev = l
    if index == 0:
        return floor(len(s) * k / 2)
    total = sum(floor(counts[i] / 2) * k for i in range(index + 1) if counts[i] > 1)
    if s[0] == s[-1] and counts[0] % 2 == 1 and counts[index] % 2 == 1:
        total += k - 1
    return total

_s = input()
_k = int(input())
print(solve(_s, _k))

Solution (Python):
from math import floor

def solve(s, k):
    counts = [0] * len(s)
    prev = ''
    index = -1
    for l in s:
        if prev == l:
            counts[index] += 1
        else:
            index += 1
            counts[index] = 1
        prev = l
    if index == 0:
        return floor(len(s) * k / 2)
    total = 0
    for i in range(index + 1):
        if counts[i] > 1:
            total += floor(counts[i] / 2) * k
    if s[0] == s[-1] and counts[0] % 2 == 1 and counts[index] % 2 == 1:
        if (counts[0] + counts[index]) % 2 == 0:
            total += k - 1
    return total

_s = input()
_k = int(input())
print(solve(_s, _k))

Solution (Python):
from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)

Solution (Python):
s = input()
k = int(input())
if s[0] != s[len(s)-1]:
  count = 0
  cur = '*'
  for i in range(len(s)):
    if cur == s[i]:
      count += 1
      cur = '*'
    else:
      cur = s[i]
  print(count*k)
else:
  flag = True
  for i in range(len(s)-1):
    if s[i] != s[i+1]:
      flag = False
      break
  if flag:
    print(len(s)*k//2)
  else:
    ans = 0
    t = s[0]
    mae = 0
    ushiro = len(s)-1
    for i in range(len(s)-2,-1,-1):
      if s[i] == t:
        ushiro = i
      else:
        break
    for j in range(1,len(s)):
      if s[j] == t:
        mae = j
      else:
        break
    count = 0
    cur = s[mae]
    for i in range(mae+1,ushiro):
      if cur == s[i]:
        count += 1
        cur = '*'
      else:
        cur = s[i]
    ans = count*k
    ans += ((len(s)-ushiro+mae+1)//2)*(k-1)+(mae+1)//2+(len(s)-ushiro)//2
    print(ans)

Solution (Python):
def solve(t):
    same=0
    count=0
    for i in range(len(t)):
        same+=1
        if i==len(t)-1 or t[i]!=t[i+1]:
            count+=same//2
            same=0
    return count

s=input()
k=int(input())
f=True
for i in range(len(s)-1):
    if s[i]!=s[i+1]:
        f=False
if f==True:
    print(len(s)*k//2)
else:
    st=s+s;
    scount=solve(s)
    stcount=solve(st)
    print(scount+(stcount-scount)*(k-1))

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    
    S = Scanner.string()
    K = Scanner.int()
    if len(Counter(S)) == 1:
        print(len(S) * K // 2)
        return
    ans = 0
    if S[0] == S[-1]:
        c1 = 1
        for i in range(1, len(S)):
            if S[i] == S[0]:
                c1 += 1
            else:
                break
        c2 = 1
        for j in reversed(range(len(S)-1)):
            if S[j] == S[-1]:
                c2 += 1
            else:
                break
        ans = c1 // 2
        ans += c2 // 2
        ans += (c1 + c2) // 2 * (K-1)
        c3 = 0
        c = ""
        for k in range(i, j + 1):
            if S[k] == c:
                c3 += 1
            else:
                ans += c3 // 2 * K
                c = S[k]
                c3 = 1
        ans += c3 // 2 * K
        print(ans)
        return
    else:
        c = "0"
        cnt = 0
        for i in range(len(S)):
            if c == S[i]:
                cnt += 1
            else:
                ans += cnt // 2
                c = S[i]
                cnt = 1
        ans += cnt // 2
        print(ans * K)
    return


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
k = int(input())
import math
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)
ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()

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

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()
print(ans)

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

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

ans=0
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)
ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()
    
    
l_length=1
last=s[0]
for i in range(1,len(s)):
    if s[i] == last:
        l_length+=1
    else:
        break
r_length=1
last=s[-1]
for i in range(len(s)-2,-1,-1):
    if s[i] == last:
        r_length+=1
    else:
        break

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()

print(ans)

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

if len(s) == 1:
    print(math.floor(k/2))
    exit()
if len(s) == 2:
    if s[0] == s[1]:
        print(k)
        exit()
    else:
        print(0)
        exit()
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()

ans=0
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)
ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()

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

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()

print(ans)

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = list(sr())
K = ir()

unit = 0
cur = ''
T = S.copy()
for i in range(len(T)):
    if T[i] == cur:
        T[i] = '_'
        unit += 1
    cur = T[i]

if len(set(S)) == 1:
    answer = len(S) * K // 2
elif S[0] != S[-1]:
    answer = unit * K
else:
    
    T = S.copy()
    head = 1; tail = 1
    for i in range(1, len(T)):
        if T[i] == T[0]:
            head += 1
        else:
            break
    for i in range(-2, -len(T)-1, -1):
        if T[i] == T[-1]:
            tail += 1
        else:
            break
    
    answer = unit * K - (head//2 + tail//2 - (head+tail)//2) * (K-1)

print(answer)

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = list(sr())
K = ir()

def count(s):
    t = s.copy()
    cur = ''
    ret = 0
    for i in range(len(t)):
        if t[i] == cur:
            t[i] = '_'
            ret += 1
        cur = t[i]
    
    return ret

one = count(S)
two = count(S + S)
diff = count(S * 3) - count(S)
if K&1:
    answer = one + (K//2) * diff
else:
    answer = two + ((K//2)-1) * diff

print(answer)

Solution (Python):
S = input()
K = int(input())
check = 0
ans = 0
St = []
for s in S:
    if s != check:
        check = s
    else:
        check = 0
        ans += 1
ans *= K
if S[0] == S[-1]:
    st = 1
    en = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            st += 1
        else:
            break
    for i in range(len(S)-1):
        if S[-1*(i+1)] == S[-1*(i+2)]:
            en += 1
        else:
            break
    if st % 2 == 1 and en % 2 == 1:
        ans += K - 1
if len(S) == 1:
    ans = K // 2
if len(set(list(S))) == 1:
    ans = len(S) * K // 2
print(ans)

Solution (Python):
from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  if len(s)%2==1 and k%2==1:
    print(len(s)//2 + len(s)*(k-1)//2 )
  else:
    print(len(s)*k//2)
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))

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

ans = 0
flag = False
before = ""
for i in range(len(s)):
    if s[i] == before:
        if flag:
            flag = False
        else:
            flag = True
            ans += 1
    else:
        flag = False
    before = s[i]

n = len(s)
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()
elif s[0] == s[-1]:
    a = 1
    b = 1
    for i in range(len(s)):
        if s[i] == s[i+1]:
            a += 1
        else:
            break
    for i in range(len(s)):
        if s[n-1-i] == s[n-2-i]:
            b += 1
        else:
            break
    if a % 2 == 1 and b % 2 == 1:
        print(ans*k + (k-1))
    else:
        print(ans*k)
else:
    print(ans * k)

Solution (Python):
S = input()
K = int(input())
l = []
cnt = 1
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
l.append(cnt)

if len(l) == 1:
    print(len(S) * K // 2)
else:
    if S[0] == S[-1]:
        ans = sum([x // 2 for x in l]) * K
        ans -= (l[0] // 2 + l[-1] // 2 - (l[0] + l[-1]) // 2) * (K - 1)
    else:
        ans = sum([x // 2 for x in l]) * K
    print(ans)

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

if len(set(s)) == 1:
    ans = len(s) * k //2
else:
    if s[0] != s[-1]:
        i = 0
        cnt = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt += 1
                i += 2
            else:
                i += 1
        ans = cnt * k
    else:
        i = 0
        cnt = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt += 1
                i += 2
            else:
                i += 1
        tmp = cnt * k

        cnt_s = 0
        i = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt_s += 1
                i += 2
            else:
                i += 1
        
        s_2 = s * 2
        cnt_s_2 = 0
        i = 0
        while i < len(s_2) - 1:
            if s_2[i] == s_2[i+1]:
                cnt_s_2 += 1
                i += 2
            else:
                i += 1

        diff = cnt_s_2 - 2 * cnt_s
        ans = tmp + diff * (k-1)



print(ans)

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

ans = 0
c = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        c +=1
    elif s[i] != s[i+1]:
        if c == 2:
            ans += 1
        else:
            ans += c // 2
        c = 1
if c == 2:
    ans += 1
else:
    ans += c // 2

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

elif s[0] != s[-1]:
    print(ans * k)
elif s[0] == s[-1]:
    a = 0
    for i in range(len(s)-1):
        if s[0] == s[i]:
            a +=1
        else:
            break

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

    print(ans * k - (a // 2 + b // 2 - (a+b) // 2) * (k - 1))

Solution (Python):
S = input()
K = int(input())
if len(set(S)) == 1:
    print(len(S) * K // 2)
    exit()

i = 0
cnt = 0
while i + 1 < len(S):
    if S[i] == S[i + 1]:
        cnt += 1
        i += 2
    else:
        i += 1

if S[0] != S[-1]:
    print(cnt * K)
else:
    i = 1
    a = 1
    prev = S[0]
    while i < len(S):
        if prev != S[i]:
            break
        else:
            a += 1
        i += 1
    prev = S[-1]
    b = 1
    i = len(S) - 2
    while 0 <= i:
        if prev != S[i]:
            break
        else:
            b += 1
        i -= 1
    print(cnt * K - (K - 1) * (a // 2 + b // 2 - (a + b) // 2))

Solution (Python):
def main():
    def f(s):
        n = len(s)

        ret = 0
        i = 0
        while i < n - 1:
            if s[i] == s[i + 1]:
                ret += 1
                i += 2
                continue
            i += 1

        return ret

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

    if k == 1:
        print(f(s))
        return

    if k <= 4:
        print(f(s * k))
        return

    d2 = f(s * 4) - f(s * 3)
    d1 = f(s * 3) - f(s * 2)

    q, r = divmod(k - 1, 2)
    ret = f(s * 1) + (d1 + d2) * q + d1 * r

    print(ret)

    
    


if __name__ == '__main__':
    main()

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

count = 0
change = False

for i in range(1, len(s)):
    if s[i-1] == s[i] and not change:
        count += 1
        change = True
    else:
        change = False

count2 = 0
change2 = False
for i in range(2, len(s)):
    if s[i-1] == s[i] and not change2:
        count2 += 1
        change2 = True
    else:
        change2 = False


if len(s) == 1:
    print(k//2)
elif s[0] == s[-1] and not change:
    if change2:
        print((count+count2+1)*(k//2)+count*(k%2))
    else:
        print(count+(count2+1)*(k-1))
else:
    print(count*k)

Solution (Python):
from itertools import groupby

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

N = len(S)

if len(set(list(S))) == 1:
    print(N * K // 2)
else:
    G = list(map(lambda g: len(list(g[1])), (groupby(S))))
    cnt = sum(map(lambda g: g // 2, G)) * K

    if S[0] != S[N - 1]:
        print(cnt)
    else:
        a = G[0]
        b = G[-1]
        print(cnt - (a // 2 + b // 2 - (a + b) // 2) * (K - 1))