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

Solution (Python):


import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        
        
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    s = lines[0]
    k = int(lines[1])

    return (s, k)


def solve(s, k):

    compress = []
    previous_c = None
    cnt = 1
    for c in s:
        if c == previous_c:
            cnt = cnt + 1
        else:
            if previous_c:
                compress.append((previous_c, cnt))
                cnt = 1
        previous_c = c
    compress.append((previous_c, cnt))
    if debug:
        log("compress=%s" % compress)

    if len(compress) == 1:
        return compress[0][1] * k // 2
    elif len(compress) == 2:
        return (compress[0][1] // 2 + compress[1][1] // 2) * k
        
    head = compress[0]
    if len(compress) > 1:
        body = compress[1:-1]
    else:
        body = []
    tail = compress[-1]
    if debug:
        log("head=%s" % (head,))
        log("body=%s" % (body,))
        log("tail=%s" % (tail,))
    
    body_count = 0 
    for b in body:
        body_count = body_count + b[1] // 2

    total_replace_count = 0
    if head[0] == tail[0]:
        length = head[1] + tail[1]
        replace_count = length // 2 * (k-1)
        total_replace_count = total_replace_count + head[1] // 2 + replace_count + tail[1] // 2
        total_replace_count = total_replace_count + body_count * k
    else:
        total_replace_count = total_replace_count + (head[1] // 2 + body_count + tail[1] // 2) * k

    return total_replace_count


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Solution (Python):

import math


def solve(*args: str) -> str:
    s = args[0]
    k = int(args[1])

    if all([s[0] == c for c in s]):
        return str(int(len(s) * k / 2))

    H = s[0]

    ret = 0
    h = 0
    t = 0
    if H == s[-1] and 1 < k:
        h = t = 1
        while H == s[h] and h < len(s):
            h += 1

        while H == s[-t-1] and t < len(s):
            t += 1
        ret += int((h + t) / 2) * (k - 1) + int(h/2) + int(t/2)

    ret += count_adjacent_dup(s[h:len(s)-t]) * k

    return str(ret)


def count_adjacent_dup(s: str) -> int:
    ret = 0
    cnt = 0

    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            cnt += 1
        else:
            ret += math.ceil(cnt/2)
            cnt = 0

    if 0 < cnt:
        ret += math.ceil(cnt/2)

    return ret


if __name__ == "__main__":
    print(solve(*[input() for _ in range(2)]))

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


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


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


mod = 1000000007
inf = float('INF')


def A():
    S = input()

    K = II()

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

    i = 0
    ans = 0
    A = 1
    p = 0
    z=0
    z2=0
    while(A > 0):
        count = 0
        while(S[i] == S[i+1]):
            if i == 0 :
                p = 1
            if S[i+2] == '\n':
                p=2
            count += 1
            i += 1
        if p == 1 :
            z=count+1
        if p==2:
            z2=count+1

        if(S[i] != S[i+1]):
            i += 1
        if count == len(S)-2:
            print(((len(S)-1)*K)//2)
            return

        if count > 0:
            ans += (count+1)//2

        if S[-1] == S[i]:
            A = -1
        p=0
    if K >= 2 and S[-2] == S[0] and S[-3] != S[-2] and S[0] != S[1]:
        ans += 1
        print((ans*K)-1)
    elif K >= 2 and S[-2] == S[0] and (z % 2==1 and z2%2==1):
        ans += 1
        print((ans*K)-1)
    else:
        print(ans*K)

    return




def B():
    N, K = map(int, input().split())
    h = LI()
    count = 0

    for i in h:
        if i >= K:
            count += 1
        else:
            continue
    print(count)
    return




def C():
    N = II()
    A = LI()
    J = [0] * N
    for i, n in enumerate(A):
        J[n-1] = i+1
    for s in J:
        print(s, end=" ")

    return




def D():
    A, B = map(int, input().split())
    g = gcd(A, B)
    
    
    sq = int(sqrt(g)) + 2
    ans = 1

    for i in range(2, sq):
        if g % i == 0:
            ans += 1
        while g % i == 0:
            g /= i

            
        if g == 1:
            break
        if i == sq - 1:
            
            ans += 1

    print(ans)

    return




def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return




def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    



def solution(S):
    
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass



if __name__ == '__main__':
    A()

Solution (Python):
s=input()
k=int(input())
ans=0
cnt=1
add=False
lead=1
if len(s)==1:
  print(k//2)
  exit()
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    lead+=1
  else:
    break
if len(s)%2 != 0 and s.count(s[0])==len(s):
  print((len(s)*k)//2)
  exit()
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    cnt+=1
  else:
    ans+=cnt//2
    cnt=1
  if s[0] == s[-1] and lead%2!=0 and cnt%2==1 and i == (len(s)-1):
    ans+=cnt//2
    add=True
  elif i == (len(s)-1):
    ans+=cnt//2
if add:
  print((ans*k)+1*(k-1))
else:
  print(ans*k)

Solution (Python):
s=input()
k=int(input())
code_list = 'abcdefghijklmnopqrstuvwxyz'

def c1(s):
  count1=0
  S = list(s)
  l_s=len(S)
  for i in range(1,l_s):
    
    if S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count1+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count1+=1
          break
  return count1,S[-1]

def c2(s,last):
  S=list(s)
  count2=0
  l_s=len(S)
  for i in range(0,l_s):
    
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count2+=1
            break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count2+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1] and j !=S[0]:
          S[i]=j
          count2+=1
          break
  return count2,S[-1]

def c3(s,last):
  S=list(s)
  count3=0
  l_s=len(S)
  for i in range(0,l_s):
    
    if i==0:
      if S[0]==last:
        for j in code_list:
          if j != S[i+1] and j !=last:
            S[i]=j
            count3+=1
            break
    elif S[i]==S[i-1] and i<l_s-1:
      for j in code_list:
        if j != S[i+1] and j !=S[i-1]:
          S[i]=j
          count3+=1
          break
    elif S[i]==S[i-1]:
      for j in code_list:
        if j != S[i-1]:
          S[i]=j
          count3+=1
          break
  return count3

if len(s)==1:
  print(k//2)
elif k==1:
  c,_=c1(s)
  print(c)
elif k==2:
  count1,last=c1(s)
  count3=c3(s,last)
  print(count1+count3)
else:
  if k%2==0:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    count22,last22=c2(s,last21)
    count3=c3(s,last22)
    
    print(count1+count21*((k-2)//2)+count22*((k-2)//2)+count3)
  else:
    count1,last=c1(s)
    count21,last21=c2(s,last)
    if k>3:
      count22,last22=c2(s,last21)
    else:
      count22=0
    count3=c3(s,last21)
    
    print(count1+count21*((k-2)//2+1)+count22*((k-2)//2)+count3)

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

def solve():
    global S,K
    if len(set(list(S)))==1:
        return len(S)*K//2

    initchar=S[0]
    initn=0
    for ss in S:
        if initchar==ss:
            initn+=1
        else:
            break
    count=0
    i=1
    while i<len(S):
        if S[i]==S[i-1]:
            count+=1
            S[i]="."
        i+=1
    
    a=(S[0]==S[-1])
    if initn%2==0:
        a=0


    if len(S)==1:
        return (K*len(S))//2
    else:
        return(count*K+a*(K-1))


print(int(solve()))

Solution (Python):
S = list(input()) * 2
K = int(input())
Len_S2 = len(S)
Len_S1 = Len_S2 // 2
count1 = 0
count2 = 0
tmp = all(c == S[0] for c in S[:Len_S1:])
for i in range(Len_S2-1):
    if S[i] == S[i+1]:
        if i < Len_S1 - 1:
            count1 += 1
        if i < Len_S2:
            count2 += 1
        S[i+1] = "1"
diff = count2 - count1
if tmp and K >= 3 and Len_S1 % 2 != 0:
    print((count1 + (diff - 1) * (K-1)) + (1 * (K // 2)))
else:
    print(count1 + diff * (K-1))

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

if len(S) == 1:
    print(K//2)
    exit()
L = []
LS = []
ans = 0
streak = 1
s = ''

for i in range(len(S)-1):
    if i == 0:
        s = S[0]
        streak = 1
    
    if s == '':
        if S[i+1:i+2] == S[i:i+1]:
            s = S[i:i+1]
            streak += 1
    else:
        if S[i+1:i+2] == s:
            streak += 1
        else:
            L.append(streak)
            LS.append(s)
            s = ''
            streak = 1
    
    if i == len(S)-2:
        L.append(streak)
        LS.append(S[-1])
        
        
        
        



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

if LS[0] != LS[-1]:
    for l in L:
        
        ans += l//2
    ans = K*ans
elif LS[0] == LS[-1]:
    for i in range(1,len(L)-1):
        ans += L[i]//2
    ans = K*ans + L[0]//2 + (K-1)*((L[0]+L[-1])//2) + L[-1]//2
print(ans)

Solution (Python):
s = input()
k = int(input())
s = list(s)
ind = 0
anss = [[] for i in range(100)]
for i in range(len(s)-1):
  anss[ind].append(s[i])
  if s[i] != s[i+1] and i != len(s)-2:
    ind += 1
  elif i == len(s)-2:
    ind += 1


for i in range(100-ind):
  anss.remove([])

f = 0
pf = 0
if len(anss) == 0:
  anss.append([])
if s[0] == s[len(s)-1] and s[len(s)-2] == s[len(s)-1] and len(anss)>1 and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0] += anss[len(anss)-1]
  anss.remove(anss[len(anss)-1])
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    f = 1
elif s[0] == s[len(s)-1] and k > 1:
  if len(anss[0]) % 2 == 1:
    pf = 1
  anss[0].append(s[len(s)-1])
  if len(anss[0]) % 2 == 0 and pf == 1 and len(anss) > 1:
    
    f = 1
elif s[len(s)-2] == s[len(s)-1] and k > 1:
  anss[len(anss)-1].append(s[len(s)-1])
else:
  anss.append([s[len(s)-1]])
pans = 0

for i in anss:
  pans += len(i) // 2

if len(anss) == 1 and len(anss[0]) % 2 == 1:
  print((len(s)*k)//2)
elif f == 1:
  print(pans*k-1)
else:
  print(pans*k)

Solution (Python):


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

n=len(s)


skip=0
if n == 1: 
    print((k-1) // 2 + (k-1) % 2 )

else:
    flag=0
    for i in range(1,n):
        
       if s[i] != s[i-1]:
           flag =1
           break
    
    if flag == 0: 
        print((k*n-1) // 2 + (k*n-1) % 2 )
    else:
        p=0
        q=0
        if s[0] == s[n-1]:
            l=1 
            
            for i in range(1,n):
                
                if s[i] == s[i-1]:
                    p +=1
                else:
                    break
            if l < n:
                for i in range(n-2,0,-1):
                    if s[i] == s[i+1]:
                        q +=1
                    else:
                        break
            l = 1 + p + q
            
            op = ((l // 2) + (l % 2)) * (k-1)
            op += ((p // 2) + (p % 2)) 
            op += ((q // 2) + (q % 2))
        else: 
            op = 0     
        
        
        
        if s[0] == s[n-1]:
            s2 = s[p+1:n-1-q] 
        else:
            s2 = s
        
        n=len(s2)
                    
        
        l = 0
        for i in range(1,n):
            
            if s2[i-1] != s2[i]:
                op += (l // 2 + l % 2) * k
                l = 0 
            else:
                l += 1 
        if l != 0:
            op += (l // 2 + l % 2) * k
        
                
        print(op)

Solution (Python):
S=input()
K=int(input())
T=S+S
if len(set(S))!=1:
  A=[]
  D=1
  for i in range(len(S)-1):
    if S[i]==S[i+1]:
      D+=1
    else:
      A.append(D)
      D=1
  A.append(D)
  B=[i for i in A if not i==1]
  C=[(i//2) for i in B]
  Scount=(sum(C))

  E=[]
  H=1
  for j in range(len(T)-1):
    if T[j]==T[j+1]:
      H+=1
    else:
      E.append(H)
      H=1
  E.append(H)
  F=[j for j in E if not j==1]
  G=[(j//2) for j in F]
  Tcount=(sum(G))
  ans=(Tcount-Scount)*(K-1)+Scount
  print(ans)
else:
  print(K*len(S)//2)

Solution (Python):
s = input()
k = int(input())
l = len(s)
ans = 0
if k <= 3: s = s*k
else:
    if k % 2 == 0: s = s*4
    else: s = s*5
a = [0]*len(s)
for i in range(len(s)):
    if i > 0 and s[i] == s[(i-1)]: a[i] += 2
    if i < len(s)-1 and s[i] == s[(i+1)]: a[i] += 1
for i in range(len(s)):
    if a[i] == 3:
        a[i] = -1
        if a[i+1] >= 2: a[i+1] -= 2
        if a[i-1] == 1: a[i-1] = 0

for i in range(len(s)-1):
    if a[i] == 1 and a[i+1] == 2:
        a[i] = -1
        a[i+1] = 0

if k <= 3: 
    ans = len([i for i in range(len(s)) if a[i] == -1])
else:
    if k % 2 == 0:
        ans += len([i for i in range(l) if a[i] == -1])
        ans += len([i for i in range(3*l,4*l) if a[i] == -1])
        ans += len([i for i in range(l, 3*l) if a[i] == -1]) * (k//2-1)
    else:
        ans += len([i for i in range(l) if a[i] == -1])
        ans += len([i for i in range(4*l,5*l) if a[i] == -1])
        ans += len([i for i in range(l, 2*l) if a[i] == -1]) * (k//2)
        ans += len([i for i in range(2*l, 3*l) if a[i] == -1]) * (k//2-1)
print(ans)

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


def c(ss, k):
    s = list(ss)
    s *= k
    n = len(s)
    ret = 0
    for i in range(n - 2):
        if s[i] == s[i + 1] == s[i + 2]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            s[i + 1] = 'AB' [(i + 1) % 2]
            ret += 1
    return ret


x1 = c(S, 1)
x2 = c(S, 2)
x3 = c(S, 3)
x4 = c(S, 4)
x5 = c(S, 5)
if K < 100:
    print(c(S, K))
elif K % 2:
    K //= 2
    K -= 1
    print(x3 + K * (x5 - x3))
else:
    K //= 2
    K -= 1
    print(x2 + K * (x4 - x2))

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

s = [v for v in S]

block = []
count = 1

if len(s) == 1:
    if K == 1:
        print(0)
        exit()
    else:
        if K%2 == 1:
            print((K-1)//2)
            exit()
        else:
            print(K//2)
            exit()

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

z = len(block)
t = 0
d = 0

if not z == 1:
    for j in range(len(block)):
        x = len(block[j])
        if x == 1:
            continue
        else:
            if x%2 == 0:
                t += x//2
            elif x%2 == 1:
                t += x - (x+1)//2
    
    if block[0][0] == block[-1][-1]:
        joint = block[0] + block[-1]
        y = len(joint)
        if y%2 == 1:
            d = 0
        else:
            d = 1
else:
    p = len(block[0])
    if p%2 == 0:
        t = p//2
    else:
        t = (p-1)//2
        if K%2 == 1:
            ans = t + p*(K-1)//2                       
            print(ans)
            exit()
        else:
            ans = p + p*(K-2)//2
            print(ans)
            exit()

ans = t*K + d*(K-1)
print(ans)

Solution (Python):
import sys

S=input()
K=int(input())
result=0

a=S[0]
renzoku=0
sumInS=0
for s in S:
    if a==s:
        renzoku=renzoku+1
    else:
        sumInS=sumInS+renzoku//2
        renzoku=1
    a=s
sumInS=sumInS+renzoku//2
result = sumInS*K

if S[0]==S[-1]:
    rst=0
    for s in S:
        if s==S[0]:
            rst=rst+1
        else:
            break
    ren=0
    for s in S[::-1]:
        if s==S[-1]:
            ren=ren+1
        else:
            break
    if rst%2==1 and ren%2==1:
        result=result+(K-1)

if renzoku==len(S):
    total=len(S)*K
    result=total//2

print(result)

Solution (Python):
s = list(input().strip())
t = int(input())
ans = 0
change = False
last = False

if len(s) == 1:
    print(t // 2)
    exit()
before = s[0]
count = 1
first = True
for i in range(1, len(s)):
    if before == s[i]:
        count += 1
    else:
        before = s[i]
        
        if first:
            first = False
            if count % 2 == 1:
                first_odd = True
            else:
                first_odd = False
        ans += count // 2
        count = 1

if first:
    print(count*t // 2)
    exit()

ans += count // 2

if count % 2 == 1:
    last_odd = True
else:
    last_odd = False


if s[0] == s[-1] and first_odd and last_odd:
    print(ans + (ans + 1) * (t - 1))
else:
    print(ans * t)

Solution (Python):
import collections
S =[i for i in input()]
K = int(input())
a = 1
b = []
c = []
SS = len(S)
result = 0
if S[0] != S[-1]:
  try:
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  print(result*K)
elif len(collections.Counter(S)) == 1:
  print(len(S)*K//2)
else:
  result2 = 0
  for j in range(len(S)):
    if S[j] == S[j + 1]:
      a += 1
    else:
      c.append(a)
      a = 1
      break
  S2 = S[c[0]:]
  try:
    for j in range(len(S2)):
      if S2[j] == S2[j + 1]:
        a += 1
      else:
        c.append(a)
        a = 1
  except:
    c.append(a)
  S = S+S
  S = S[c[0]:-(SS-c[0])]
  try:
    a = 1
    for j in range(len(S)):
      if S[j] == S[j+1]:
        a += 1
      else:
        b.append(a)
        a = 1
  except:
    b.append(a)
  for k in b:
    result += k//2
  for l in c:
    result2 += l//2
  print(result*(K-1)+result2)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    s = list(input().rstrip())
    k = int(input())


    if len(s) == 1:
        print(k//2)
    else:
        sa = s[:]

        c1 = 0
        c2 = 0

        for i in range(len(s) - 1):
            if sa[i] == sa[i+1]:
                c1 += 1
                sa[i+1] = '1'


        if sa[0] == sa[-1]:
            s[0] = '2'
            c2 += 1

            for i in range(len(s) - 1):
                if s[i] == s[i+1]:
                    c2 += 1
                    s[i+1] = '2'


            if s[-1] == sa[0]:
                print(c1 + c2*(k-1))
            elif s[-1] != sa[0]:
                if k % 2 == 0:
                    x = k // 2
                    print(c1*x + c2*x)
                else:
                    x = k // 2
                    print(c1*(x+1) + c2*x)
        else:
            print(c1*k)

if __name__ == '__main__':
    main()

Solution (Python):
S, K = open(0).read().split()

cGroups = []
tempList = []
for i, c in zip(range(len(S)), S):
    tempList.append(c)
    if (i == len(S) - 1) or c != S[i + 1]:
        cGroups.append(tempList)
        tempList = []

singleResult = 0
for g in cGroups:
    if len(g) > 1:
        singleResult += len(g) // 2

result = singleResult * int(K)

if cGroups[0][0] == cGroups[-1][0] and len(cGroups[0]) % 2 == 1 and len(cGroups[-1]) % 2 == 1:
    if len(cGroups) == 1:
        result += int(K) // 2
    else:
        result += int(K) - 1

print(result)

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

l=len(s)
ans=0

if K==1:
  Ls=list(s)
  for n in range(1,l):
    if Ls[n]==Ls[n-1]:
      ans+=1
      Ls[n]=str(n)
elif K==2:
  Ls=list(s+s)
  for n in range(1,2*l):
    if Ls[n]==Ls[n-1]:
      ans+=1
      Ls[n]=str(n)
else:
  ansA=0
  ansB=0
  ansC=0
  Ls=list(s+s+s)
  for n in range(1,l):
    if Ls[n]==Ls[n-1]:
      ansA+=1
      Ls[n]=str(n)
  for n in range(l,2*l):
    if Ls[n]==Ls[n-1]:
      ansB+=1
      Ls[n]=str(n)
  for n in range(2*l,3*l):
    if Ls[n]==Ls[n-1]:
      ansC+=1
      Ls[n]=str(n)
  if Ls[0]==Ls[l]:
    ans=ansA*K
  elif Ls[0]!=Ls[l] and Ls[0]==Ls[2*l]:
    if K%2==0:
      ans=(ansA+ansB)*(K//2)
    else:
      ans=(ansA+ansB)*(K//2)+ansA
  else:
    ans=ansA+ansB*(K-1)
print(ans)

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

tail = s[0]
plus = 0
flg = False
if tail == s[-1]:
  tmp_cnt1 = 0
  tmp_cnt2 = 0
  for i in s:
    if tail == i:
      tmp_cnt1 += 1
    else:
      break
  for i in s[::-1]:
    if tail == i:
      tmp_cnt2 += 1
    else:
      break
  if (tmp_cnt1 % 2) == 1 and (tmp_cnt2%2)==1:
    plus = 1
  if tmp_cnt1 == len(s):
    flg = True

cnt=0
if len(s)==1:
  ret = k//2
else:
  tmp=s[0]
  tmp_cnt=0
  for i in s[1:]:
    if tmp==i:
      tmp_cnt+=1
    else:
      cnt+=(tmp_cnt+1)//2
      tmp_cnt=0
    tmp=i
  cnt+=(tmp_cnt+1)//2
  ret=cnt*k
  if plus == 1:
    ret += k-1
  if flg:
    ret = len(s)*k//2
print(ret)

Solution (Python):
import copy

S=input()
K=int(input())
S=list(S)
n=len(S)
ans=0
j=1

def allsame(str):
    flag = True
    for i in range(len(str)-1):
        if str[i+1]!=str[i]:
            flag=False
            break;
    return flag

if S[0]!=S[n-1]:
    for i in range(n):
        if i==n-1:
            ans += j//2
            break
        if S[i+1]==S[i]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = ans*K
elif allsame(S):
    ans = n*K//2
else:
    i=0
    l=1
    for i in range(n):
        if S[i+1]==S[i]:
            l += 1
        else:
            tempans =l//2
            break;
    T=copy.copy(S)
    T.reverse()
    k=1
    for i in range(n):
        if T[i+1]==T[i]:
            k += 1
        else:
            tempans +=k//2
            break;
        
    for i in range(n-l-k):
        if i==n-l-k-1:
            ans += j//2
            break;
        if S[i+l+1]==S[i+l]:
            j += 1
        else:
            ans += j//2
            j=1
    ans = (ans+(l+k)//2)*K
    ans = ans + tempans -(k+l)//2
    
print(ans)

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

if not(1 <= len(S) and len(S) <= 100):
    exit()
if not (S.isalpha() and S.islower()):
    exit()
if not float(K).is_integer():
    exit()
if not (1 <= int(K) and int(K) <= 10 ** 9):
    exit()

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

S = input()
K = int(input())
l = len(S)

if l==1 :
  print(int(K/2))
  exit()
  
s0 = ""
c = 0
d = 1
idx = 0
for s in S:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1
  s0 = s1

  idx = idx+1

c0 = c

if K==1:
  print(c0)
  exit()

s0 = ""
c = 0
d = 1
idx=0
for s in S*2:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l*2-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1

  s0 = s1
  idx = idx+1


c1 = c-c0
if K==2:
  print(c)
  exit()


s0 = ""
c = 0
d = 1
idx=0
for s in S*3:
  s1 = s
  if s0==s1:
    d += 1 
    if idx==l*3-1:
      c += int(d/2)
  else:
    c += int(d/2)
    d = 1

  s0 = s1
  idx = idx+1

if K==3:
  print(c)
  exit()

c2 = c-c1-c0
  
print(c1*math.ceil((K-1)/2)+c2*math.floor((K-1)/2)+c0)

Solution (Python):
def main():
    S = input()
    K = int(input())
    count = 0
    same = True
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            continue
        else:
            same = False
            break
    if same:
        count += int((len(S) * K) / 2)
    else:
        i = 0
        while i < len(S):
            c = 1
            flag = False
            if i < len(S) - 1:
                flag = True
                while S[i] == S[i+1]:
                    c += 1
                    i += 1
                    if i + 1 >= len(S):
                        flag = False
                        break
            i += 1
            if c % 2 == 0:
                count += K * int(c / 2)
            else:
                count += K * int((c - 1) / 2)
            if not flag:
                u = 0
                a = 0
                while S[u] == S[-1]:
                    u += 1
                    a += 1
                if c % 2 == 0 and a % 2 == 0:
                    continue
                elif c % 2 == 1 and a % 2 == 0:
                    continue
                elif c % 2 == 0 and a % 2 == 1:
                    continue
                else:
                    count += K - 1
            
    print(count)

if __name__ == "__main__":
    main()

Solution (Python):
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print((n*k)//2)
else:
    arr = []
    cnt = 0
    last = s[0]
    for i in s + '0':
        if i != last:
            arr.append(cnt)
            cnt = 1
            last = i
        else:
            cnt += 1
    ans = 0
    if s[0] == s[-1]:
        ans += (arr[0] + arr[-1])//2*(k-1) + arr[0]//2 + arr[-1]//2
        for i in arr[1:-1]:
            ans += (i//2)*k
    else:
        for i in arr:
            ans += (i//2)*k
    print(ans)

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

cnt = 0
ans = 0

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

if S[0] == S[len(S)-1]:
    fcnt = 0
    for i in range(len(S) - 1):
        if S[i] == S[i + 1]:
            fcnt += 1
        else:
            break
    bcnt = 0
    for i in range(len(S)-1, 0, -1):
        if S[i] == S[i - 1]:
            bcnt += 1
        else:
            break
    if fcnt%2 == 0 and bcnt%2 == 0:
        if fcnt == len(S)-1:
            print((ans+ans+1)*N//2)
        else:
            print((ans+1)*N-1)
    else:
        print(ans*N)
else:
    print(ans*N)

Solution (Python):
t=str(input())
k=int(input())
L = len(t)
tmp = t[0]
n = 0 
flag = 0
for i in range(1,L):
  if flag==1:
    flag=0
  elif t[i]==tmp:
    n+=1
    flag=1  
  tmp=t[i]
ans=k*n
if L>2:
  if t[0]==t[-1] and flag==0:
    u = t[0]+t
    tmp = u[0]
    m = 0 
    flag = 0
    for i in range(1,L+1):
      if flag==1:
        flag=0
      elif u[i]==tmp:
        m+=1
        flag=1  
      tmp=u[i]
    if m!=n: 
      ans+=k-1

if t.count(t[0])==L:
  ans=(k*L)//2
print(ans)

Solution (Python):


S = input()
K = int(input().split()[0])




count_seq_start = 1  
count_seq_end = 1  


for i in range(1, len(S)):
    if S[i] == S[0]:
        count_seq_start += 1
    else:
        break


count_seq_end = 1  

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

unit_c = 0
count_seq = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        count_seq += 1
    else:
        
        if count_seq % 2 == 0:
            unit_c += count_seq // 2
        else:
            unit_c += (count_seq - 1) // 2
        count_seq = 1




if count_seq % 2 == 0:
    unit_c += count_seq // 2
else:
    unit_c += (count_seq - 1) // 2

c = 0
c += unit_c * K

if S[0] == S[-1] and count_seq_start % 2 == 1 and count_seq_end % 2 == 1:
    c += K - 1

is_all = True
for i in range(1, len(S)):
    if S[i] == S[0]:
        pass
    else:
        is_all = False
        break

if is_all:
    if (len(S) * K) % 2 == 0:
        c = len(S) * K // 2
    else:
        c = (len(S) * K - 1) // 2

ans = int(c)
print(ans)

Solution (Python):
import sys
s = input()
k = int(input())
ret = 0
t = 0
ss = []
ss.append(s[0])
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        ss[-1]+=s[i]
    else:
        ss.append(s[i])

for ss_ in ss[1:-1]:
    ret += len(ss_) // 2 * k

if len(ss) >= 3:
    if ss[0][0] != ss[-1][0]:
        ret += len(ss[0]) // 2 * k
        ret += len(ss[-1]) // 2 * k
    else:
        ret += (len(ss[-1]) + len(ss[0]))  // 2 * (k-1)
        ret += len(ss[0]) // 2
        ret += len(ss[-1]) // 2

if len(ss) == 1:
    ret += len(ss[0]) * k // 2

if len(ss) == 2:
    ret += len(ss[0]) * k // 2
    ret += len(ss[1]) * k // 2

print(ret)