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

left = S[0]
right = S[-1]

inside = []
tmp = 1
bef = ""
for idx, val in enumerate(S):
    if idx == 0:
        bef = val
        continue
    if val == bef:
        tmp += 1
    else:
        inside.append(tmp)
        tmp = 1
    bef = val
else:
    inside.append(tmp)

ans = 0
if left == right:
    if len(inside) == 1:
        print((len(S) * K )// 2)
    elif len(S) > 2:
        head = inside[0]
        tail = inside[-1]
        center = inside[1:-1]
        init = inside[:-1]
        last = inside[1:]
        joint = head + tail

        init_op = 0
        last_op = 0
        for i in init:
            if i != 1:
                init_op += i //2
        for i in last:
            if i != 1:
                last_op += i //2
        joint_op = joint // 2
        center_op = 0
        for i in center:
            if i != 1:
                center_op += i //2

        if K == 1:
            ans = init_op + tail // 2
        else:
            ans += init_op
            ans += last_op
            ans += joint_op * (K - 1)
            ans += center_op * (K - 2)
        print(ans)
    elif len(S) == 1:
        print(K // 2)
    elif len(S) == 2:
        print(K)
else:
    for i in inside:
        if i != 1:
            ans += i //2
    print(ans * K)

Solution (Python):
import sys
inputs = sys.stdin.readlines()

S = inputs[0].strip()
K = int(inputs[1])

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

elif S[0] != S[-1]:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    print(K*sum(map(lambda x: x//2, counts)))
else:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    if counts[0] % 2 == 1 and counts[-1] % 2 == 1:
        t = 1
    else:
        t = 0
    counts[0] += counts.pop()
    print(K*sum(map(lambda x: x//2, counts)) - t)

Solution (Python):
s=str(input())
k=int(input())
t=s*2

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


ans_=0
a_=1
for i in range(len(t)-1):
    if t[i]==t[i+1]:
        a_+=1
    else:
        ans_+=a_//2
        a_=1
ans_+=a_//2

if len(set(s))!=1:
    print(ans+(k-1)*(ans_-ans))
elif k%2==0:
    print(ans+(k-1)*(ans_-ans))
else:
    print((k//2)*(len(s))+(k%2)*(len(s)//2))

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

tmp = ""
cnt = 0
ans = 0
for s in S:
    if s == tmp:
        cnt += 1
    else:
        ans += cnt//2
        cnt = 1
    tmp = s

if cnt == len(S):
    ans += (cnt*K)//2
else:
    ans *= K
    
    first_cnt = 0
    if S[0] == S[-1]:
        for s in S:
            if s == S[0]:
                first_cnt += 1
            else:
                break
        ans -= first_cnt//2 * (K-1)
        ans += (cnt+first_cnt)//2 * (K-1)
        ans += cnt//2
    else:
        ans += cnt//2 * K

print(ans)

Solution (Python):
from itertools import groupby

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

if len(comp_s) == 1:
  print(comp_s[0] * n // 2)
  quit()

a = 0
if comp_k[0] == comp_k[-1]:
  a += comp_s[0]//2 + comp_s[-1]//2
  comp_s[0] += comp_s.pop()
  a -= comp_s[0]//2

for i in comp_s:
  ans += i // 2
ans *= n

print(ans + a)

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


prev = ""

n = 0

if len(s) == 1:
    print(num // 2)
else:
    if len(set(s)) == 1:
        ans = len(s) * num // 2
    
    elif s[0] == s[-1]:
        b = 1
        for i in range(1, len(s)):
            if s[0] == s[i]:
                b += 1
            else:
                start = i
                break
        
        ss = s[::-1]
        c = 1
        for i in range(1, len(s)):
            if ss[0] == ss[i]:
                c += 1
            else:
                end = i
                break
        
        for i in range(start, len(s) - end):
            if prev == s[i]:
                n += 1
                prev = ""
            else:
                prev = s[i]
        ans = ((b + c) // 2) * (num - 1)
        ans += b // 2 + c // 2
        ans += n * num

    else:
        for x in s:
            if prev == x:
                n += 1
                prev = ""
            else:
                prev = x
        ans = n * num


    print(ans)

Solution (Python):
s, k = input(), int(input())
char=[]
prev = "0"
cnt = 0
for i in s:
    if i==prev: cnt+=1
    else: char.append(cnt);cnt=1
    prev = i
char.append(cnt)

ans = 0
for i in char: ans += i//2
ans *= k
if s[0]==s[-1]: ans += (char[1]%2 and char[-1]%2) * (k-1)

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

print(ans)

Solution (Python):
from itertools import groupby
s = list(input())
k = int(input())

G = []
for key, group in groupby(s):
    G += [list(group)]

count = 0

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

elif s[0]!=s[-1]:
    for g in G:
        count += len(g)//2
    print(count*k)

else:
    d = len(G[0]+G[-1])//2-(len(G[0])//2)-(len(G[-1])//2)
    for g in G:
        count += len(g)//2
    print(count*k + d*(k-1))

Solution (Python):
S=input()
K=int(input())
x=0
l=[]
for i in range(len(S)):
    if S[i-1]==S[i]:
        x+=1
    else:
        l.append(x)
        x=0
l.append(x)
if len(l)==1:
    print(-(-len(S)*K)//2)
elif S[-1]!=S[0]:
    print(sum([-(-e//2) for e in l])*K)
elif S[-2]==S[-1]:
    print(-(-(l[0]-1)//2)+sum([-(-e//2) for e in l[1:-1]])*K-((-l[0]-l[-1])//2)*(K-1)-((-l[-1])//2))
else:
    print(-(-(l[0]-1)//2)+sum([-(-e//2) for e in l[1:]])*K-(-l[0]//2)*(K-1))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


def summarize_list(l):
  sl=l

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append([a,c])
      a=x
      c=1
  res.append([a,c])

  return res

def main():
  s=S()
  k=I()

  if len(s)==1:
    return k//2

  l=summarize_list(s)

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

  ans=0
  bf=False
  
  if l[0][0]==l[-1][0] and (l[0][1]//2+l[-1][1]//2!=(l[0][1]+l[-1][1])//2):
    bf=True

  _ans=0
  for x in l:
    _ans+=x[1]//2
  ans+=_ans*k

  if bf:
    ans+=k-1

  return ans


print(main())

Solution (Python):
word = list(input())
num = int(input())
if len(word) == 1:
  print(num//2)
else:
  p = []
  count = 1
  for i in range(len(word)-1):
    if word[i] == word[i+1]:
      count += 1
      if i == len(word)-2:
        p.append(count)
    else:
      p.append(count)
      count = 1

  point = 0
  
  for i in p:
    point += i // 2
  if len(p) == 1:
    print(p[0]*num//2)
  elif word[0] == word[-1]:
    print((p[0] + p[-1])//2 * (num-1) + p[0]//2 + p[-1]//2 + (point - p[0]//2 - p[-1]//2)*num)
  else:
    print(point * num)

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

def do(s):
    x = 0
    cur = ""
    count = 1
    
    for i in range(len(s)):
        
        if cur == s[i]:
            count += 1
            
        else:
            x += count // 2
            
            count = 1
        cur = s[i]
    x += count // 2
    
    return x

res1 = do(s)
res2 = do(s*2)
res3 = do(s*3)


if k == 1:
    print(res1)
elif k == 2:
    print(res2)
elif k == 3:
    print(res3)
else:
    if (k-1) % 2 == 1:
        res = res1
        res += ( (res3-res2) + (res2-res1) ) * ( (k-1) // 2)
        res += (res2-res1)
    elif (k-1) % 2 == 0:
        res = res1
        res += ( (res3-res2) + (res2-res1) ) * ( (k-1) // 2)
    print(res)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
s=input()
k=int(input())
cnt=0
tmp=0
flag=0

for i in range(len(s)):
    if s[0]==s[i]:
        flag+=1
if flag==len(s):
    print(len(s)*k//2)
    quit()

for i in range(len(s)-1):
    if s[i]==s[i+1]:
        tmp+=1
    else:
        cnt+=(tmp+1)//2
        tmp=0
cnt+=(tmp+1)//2
hoka=0

left=0
right=0
if s[0]==s[-1]:
    for i in range(len(s)):
        if s[0]==s[i]:
            left+=1
        else:
            break
    for i in range(len(s)):
        if s[-1]==s[-1-i]:
            right+=1
        else:
            break
    if right%2==1 and left%2==1:
        hoka=1

print(cnt*k+hoka*(k-1))

Solution (Python):
import sys
import collections
import copy
import math
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
S= input()
K =int(input())
count = 1
ans_list = []
ans = 0
if len(S) ==1:
    print(len(S)*K//2)
else:
    if S [0] != S[-1]:
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                count += 1
            else:
                if count ==1:
                    continue
                else:
                    ans_list.append(count)
                    count = 1
        if count !=1:
            ans_list.append(count)
        for j in range(len(ans_list)):
            if ans_list[j]%2 == 1:
                ans += (ans_list[j]-1)//2
            else:
                ans += (ans_list[j])//2
        print(ans*K)
    else:
        a = 0
        while S[a] == S[a+1] :
            a +=1
            if a == len(S)-1:
                break
        a += 1
        b = 0
        while S[-1-b] == S[-2-b]:
            b += 1
            if b == len(S)-1:
                break
        b += 1
        if a != len(S):
            S_alt = S[a:len(S)-b]
            for i in range(len(S_alt)-1):
                if S_alt[i] == S_alt[i+1]:
                    count += 1
                else:
                    if count ==1:
                        continue
                    else:
                        ans_list.append(count)
                        count = 1
            if count !=1:
                ans_list.append(count)
            for j in range(len(ans_list)):
                if ans_list[j]%2 == 1:
                    ans += (ans_list[j]-1)//2
                else:
                    ans += (ans_list[j])//2
            ans = (ans * K)
            ans += a//2 + b//2
            ans += (a+b)//2*(K-1)
            print(ans)
        else:
            ans += len(S)*K//2
            print(ans)

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

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

def change(s, s0=""):
    t = list(s0+s)
    ret = 0
    for i in range(1, len(t)):
        if t[i] == t[i-1]:
            t[i] = '*'
            ret += 1
    return "".join(t[len(s0):]), ret

def main():
    s = input()
    k = ri()
    memo = dict()
    memo2 = []
    bef = ""
    if k < 100:
        _, c = change(s*k, "")
        print(c)
        return
    rep = 0
    for i in range(k):
        ns, c = change(s, bef)
        
        if ns in memo:
            
            rep = i-memo[ns][1]
            break
        memo[ns] = (c, i)
        memo2.append((c, ns))
        bef = ns
    ans = 0
    for m in memo2:
        ans += m[0]
    assert(len(memo2) < 3)
    rem = k-len(memo2)
    if rep == 1:
        ans += rem*memo2[-rep][0]
    elif rep == 2:
        ans += (rem//2)*sum(m[0] for m in memo2)
        if rem % 2 == 1:
            ans += memo2[0][0]
    print(ans)
    


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import groupby
S = input()
K = int(input())
lenS = len(S)

cnt1 = 0

idx = 0
for key, values in groupby(S):
  len_values = len(list(values))
  if idx == 0:
    fst_key = key
    fst_len_values = len_values

  if len_values >= 2:
    cnt1 += len_values//2
  idx += 1

if idx == 1:
  print((lenS*K)//2)
  exit()

cnt2 = cnt1
cnt3 = 0
if key == fst_key:
  if fst_len_values >= 2:cnt2 -= fst_len_values//2
  if len_values >= 2:cnt2 -= len_values//2 
  cnt3 = (fst_len_values+len_values)//2
    
print(cnt1 + (cnt2+cnt3)*(K-1))

Solution (Python):
S = input()
K = int(input())
S_0 = [i for i in S]
S_1 = S_0+S_0
ans_0 = 0
tmp = S_0[0]
tmp_0 = S_0[0]
flag = True
for i in range(1,len(S_0)):
    if S_0[i] == tmp:
        S_0[i] = "0"
        ans_0 += 1
    tmp = S_0[i]
    if S[i] != tmp_0:
        flag = False
ans_1 = 0
tmp = S_1[0]
for i in range(1,len(S_1)):
    if S_1[i] == tmp:
        S_1[i] = "0"
        ans_1 += 1
    tmp = S_1[i]
ans = ans_0 + (ans_1 - ans_0)*(K-1)
if flag == False:
    print(ans)
else:
    print((len(S)*K)//2)

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


new_s = s + "
n = len(new_s)
i = 0
j = 1
cnt = 1
ans = 0
while True:
    if j >= n:
        break
    if new_s[i] == new_s[j]:
        cnt += 1
        j += 1
        continue
    if new_s[i] != new_s[j]:
        ans += cnt // 2
        cnt = 1
        i = j
        j = i + 1
        
ans1 = ans
t = 2*s + "
n = len(t)
i = 0
j = 1
cnt = 1
ans2 = 0
while True:
    if j >= n:
        break
    if t[i] == t[j]:
        cnt += 1
        j += 1
        continue
    if t[i] != t[j]:
        ans2 += cnt // 2
        cnt = 1
        i = j
        j = i + 1


t = 3*s + "
n = len(t)
i = 0
j = 1
cnt = 1
ans3 = 0
while True:
    if j >= n:
        break
    if t[i] == t[j]:
        cnt += 1
        j += 1
        continue
    if t[i] != t[j]:
        ans3 += cnt // 2
        cnt = 1
        i = j
        j = i + 1

tmp1 = ans2 - ans1
tmp2 = ans3 - ans2
ans = ans1 
k = k - 1
ans = ans + tmp1*(k - k//2) + tmp2*(k//2)
print(ans)

Solution (Python):

def solve():
  S=input()
  K=int(input())
  if len(S)==1:
    return K//2
  count=1
  L=[]
  for i in range(len(S)):
      try:
          if S[i] == S[i+1]:
             count = count + 1
          else:
              L.append(count)
              count = 1
      except:
          
          L.append(count)

  if S[-1]==S[1]:
    flag=0
    if len(L)==1:
      return len(S)*K//2
    else:
      if L[-1]%2==1 and L[0]%2==1:
        flag=1
      L[0]=(L[0]+L[-1])//2*2
      L[-1]=0
    return sum([x//2 for x in L])*K-flag
  else:
    return sum([x//2 for x in L])*K

print(solve())

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

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

SS = S[:] + S[:]
S = list(S)
SS = list(SS)
ans0 = 0
ans1 = 0

c = 0
cnt = 1
for i in range(1, n):
    if S[i - 1] == S[i]:
        cnt += 1
        if i == n-1:
            c += cnt // 2
    else:
        if cnt >= 2:
            c += cnt // 2
        cnt = 1
ans0 = c
c = 0
cnt = 1
m = len(SS)
for i in range(1, m):
    if SS[i - 1] == SS[i]:
        cnt += 1
        if i == m - 1:
            c += cnt // 2
    else:
        if cnt >= 2:
            c += cnt // 2
        cnt = 1

ans1 = c


num = ans1 - ans0
print(ans0 + (K-1) * num)

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

n=len(s)
ss=s+s
nn=len(ss)
sss=ss+s
nnn=len(sss)

tmps=1
cnts=0
tmpss=1
cntss=0
tmpsss=1
cntsss=0

for i in range(n-1):
    if s[i]==s[i+1]:
        tmps+=1
    else:
        cnts+=(tmps//2)
        tmps=1
cnts+=(tmps//2)

for i in range(nn-1):
    if ss[i]==ss[i+1]:
        tmpss+=1
    else:
        cntss+=(tmpss//2)
        tmpss=1
cntss+=(tmpss//2)

for i in range(nnn-1):
    if sss[i]==sss[i+1]:
        tmpsss+=1
    else:
        cntsss+=(tmpsss//2)
        tmpsss=1
cntsss+=(tmpsss//2)

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

elif (cntsss-cntss)!=(cntss-cnts):
    print((cntsss-cntss)*((k+1)//2)+(cntss-cnts)*(k//2))

else:
    print((cntsss-cntss)*(k-1)+cnts)

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

def f(n):
    s_ = s * n
    r = 0
    i = 0
    while i < len(s_) - 1:
        if s_[i] == s_[i + 1]:
            r += 1
            i += 2
        else:
            i += 1

    return r

x1=f(1)
x2=f(2)
x3 = f(3)





if s.count(s[0]) == len(s):
    ans = k // 2 * x2 + (x1 if k % 2 == 1 else 0)
else:
    ans = x1 + (x2 - x1) * (k - 1)

print(ans)

Solution (Python):
s = input()
k = int(input())
if len(s) == 1:
    print(k // 2)
else:

    def counts(s):
        count = 0
        for i in range(len(s) - 1):

            if s[i] == s[i + 1]:
                count += 1
                s = s[: i + 1] + "." + s[i + 2 :]
        return count

    res1 = counts(s)
    res2 = counts(s * 2)
    res3 = counts(s * 3)
    if res3 - res2 == res2 - res1:
        print(res1 + (res2 - res1) * (k - 1))
    else:
        if k % 2 == 1:
            print(res2 * (k // 2) + res1)
        else:
            print(res2 * (k // 2))

Solution (Python):
s = list(input())
k = int(input())
n = len(s)
s.append(' ')

l = []
cnt = 1
for i in range(1, n + 1):
    if s[i - 1] != s[i]:
        l.append(cnt)
        cnt = 1
    else:
        cnt += 1
m = sum(map(lambda li: li // 2, l))
if len(l) == 1:
    ans = k * n // 2
elif s[0] == s[-2] and l[0] % 2 == l[-1] % 2 == 1:
    ans = k * m + k - 1
else:
    ans = k * m
print(ans)

Solution (Python):

s = list(input())
k = int(input())
n = len(s)
if s.count(s[0]) == n:
    print(n * k // 2)
    exit()
c = 0
t = s * 3
for i in range(n * 3 - 1):
    if t[i + 1] == 'X':
        continue
    if t[i] == t[i + 1]:
        t[i + 1] = 'X'
ans = t[0:n].count('X') + t[n:2 * n].count('X') * (k - 2) + t[2 * n:3 * n].count('X')
print(ans)

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


l=len(s)


if len(s)==1:
    print(k//2)
else:
    i=0
    cnt=0
    flag1=0
    while(i<l-1):
        if s[i]==s[i+1]:
            cnt+=1
            if i+1==l-1:
                flag1=1
            i+=2
        else:
            i+=1
    
    
    
    i=1
    cnt2=1
    flag2=0
    while(i<l-1):
        if s[i]==s[i+1]:
            cnt2+=1
            if i+1==l-1:
                flag2=1
            i+=2
        else:
            i+=1
    
    
    if s[0]==s[-1]:
        if flag1==1:
            ans=cnt*k
        elif flag2==1:
            ans=(k//2+k%2)*cnt+(k//2)*cnt2
        else:
            ans=cnt+cnt2*(k-1)
    else:
        ans=cnt*k
            
    print(ans)

Solution (Python):
s=input()
n=int(input())
ss=s+s
tmp1=0
tmps=""
for i in s:
    if tmps==i:
        tmp1+=1
        tmps=""
    else:
        tmps=i
tmp2=0
tmps=""
for i in ss:
    if tmps==i:
        tmp2+=1
        tmps=""
    else:
        tmps=i
sss=s+s+s
tmp3=0
tmps=""
for i in sss:
    if tmps==i:
        tmp3+=1
        tmps=""
    else:
        tmps=i

sa1=tmp2-tmp1
sa2=tmp3-tmp2
if n%2==1:
    print(tmp1+(sa1+sa2)*(n//2))
else:
    print(tmp1+(sa1+sa2)*(n//2-1)+sa1)

Solution (Python):

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

import sys

if N==1:
    print(K*N//2)
    sys.exit()

if S[0] == S[-1]:
    
    cnt = 1
    m = 0
    flag = True
    tmpcnt1 = 0
    for i in range(N-1):
        if S[i+1] == S[i]:
            cnt += 1
        else:
            if flag:
                tmpcnt1 = cnt
                flag = False
                cnt = 1
            else:
                m += cnt//2
                cnt = 1
    ans = 0
    ans += m*K
    ans += (tmpcnt1 + cnt)//2 *(K-1)
    ans += tmpcnt1//2
    ans += cnt//2
    
    if cnt == N:
        print(K*N//2)    
    else:
        print(ans)
        
else:
    cnt = 1
    m = 0
    for i in range(N-1):
        if S[i+1] == S[i]:
            cnt += 1
        else:
            m += cnt//2
            cnt = 1
    m += cnt//2
    print(m*K)

Solution (Python):
from itertools import groupby

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

s2=[[h, len(list(g))] for h, g in groupby(s1)]

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

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

print(c)

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


before = S[0]
check = False
for s in S:
    if s != before:
        check = True

if not(check):
    x = len(S)*K
    print(x//2)

elif S[0] == S[len(S)-1]:
    before = ""
    cnt = 0
    x = 0
    head_size = 0
    tail_size = 0
    head = S[0]
    for s in S:
        if head == s:
            head_size += 1
        else:
            break

    for s in S[head_size:]:
        if before == s:
            cnt+=1
        else:
            x += (cnt//2)
            cnt = 1
            before = s
    tail_size = cnt
    x += ((head_size + tail_size)//2)
    ans = K * x

    
    ans += (head_size//2)

    
    ans -= ((head_size + tail_size)//2)
    ans += (tail_size//2)

    print(ans)

else:
    before = ""
    cnt = 0
    x = 0
    for s in S:
        if before == s:
            cnt+=1
        else:
            x += (cnt//2)
            cnt = 1
            before = s
    x += (cnt//2)

    print((K*x))

Solution (Python):
s=input()
k=int(input())
n=len(s)
ss=s[0]
li=[]
check=0
for i in range(n-1):
    if s[i+1]==s[i]:
        check+=1
    elif s[i+1]!=s[i]:
        li.append(check)
        check=0
li.append(check)

ans=0
for i in li:
    ans+=(i+1)//2
ans*=k

if k>0:
    aa=0
    for i in range(n-1):
        if s[i+1]==s[i]:
            aa+=1

    if aa==n-1:
        ans=(n*k)//2
    elif s[0]==s[-1]:

        a=li[0]+li[-1]+2
        ans=ans+(a//2)*(k-1)
        ans=ans-((li[0]+1)//2*(k-1))-((li[-1]+1)//2*(k-1))

   
print(ans)

Solution (Python):
import math

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

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

ans *= K


if S[0] == S[-1]:
    cntf = 1
    cnte = 1
    for i in range(1,len(S)):
        
        if S[i] == S[i-1]:
            cntf += 1
        else:
            break
    
    for i in range(1,len(S)):
        
        if S[-i-1] == S[-i]:
            cnte += 1
        else:
            break
    
    if cnte % 2 ==1 and cntf % 2 ==1:
        
        ans += (K-1)  

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

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

def count(S, K):
    S += S * (K-1)
    tmp = ''
    cnt = 0
    for i in range(len(S)):
        if S[i] == tmp:
            cnt += 1
            tmp = 'X'
        else:
            tmp = S[i]
    return cnt

cnt1 = count(S,1)
cnt21 = count(S, 2) - cnt1
cnt32 = count(S, 3) - count(S, 2)

print(cnt1 + cnt21*(K//2) + cnt32*((K-1)//2))

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

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

    isSameChar = True
    for s in S:
        if s != S[0]:
            isSameChar = False
    if isSameChar:
        print((len(S) * K) // 2)
        exit()

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

    doubleS = S * 2
    count2 = 0
    now_count = 1
    for i in range(len(doubleS) - 1):
        if doubleS[i] == doubleS[i + 1]:
            now_count += 1
        else:
            count2 += now_count // 2
            now_count = 1
    count2 += now_count // 2

    ans = count1 + (count2 - count1) * (K - 1)
    print(ans)



    

if __name__ == '__main__':
    main()

Solution (Python):








def main():
    s=input()
    k=int(input())
    n=len(s)
    cnt1=0
    tmp1=0
    ss=s*2
    nn=n*2
    sss=s*3
    nnn=n*3
    for i in range(n-1):
        if s[i]==s[i+1] and tmp1==0:
            cnt1+=1
            tmp1=1
        elif s[i]==s[i+1]:
            tmp1=0
        else:
            tmp1=0
    cnt2=0
    tmp2=0
    for i in range(nn-1):
        if ss[i]==ss[i+1] and tmp2==0:
            cnt2+=1
            tmp2=1
        elif ss[i]==ss[i+1]:
            tmp2=0
        else:
            tmp2=0
    cnt3=0
    tmp3=0
    for i in range(nnn-1):
        if sss[i]==sss[i+1] and tmp3==0:
            cnt3+=1
            tmp3=1
        elif sss[i]==sss[i+1]:
            tmp3=0
        else:
            tmp3=0
    if cnt2==2*cnt1:
        print(cnt1*k)
    elif cnt3-cnt2!=cnt2-cnt1:
        if k%2==0:
            print(cnt2*(k//2))
        else:
            print(cnt2*(k//2)+cnt1)
    else:
        print(cnt1+(cnt2-cnt1)*(k-1))



if __name__ == '__main__':
    main()

Solution (Python):


import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    S = i2s()
    K = i2n()
    ps = S[0]
    cnt = 0
    s_n = []
    for s in S:
        if ps == s:
            cnt += 1
        else:
            s_n.append(cnt)
            cnt = 1
            ps = s
    s_n.append(cnt)
    if len(s_n) == 1:
        print((s_n[0] * K) // 2)
        return
    sum_n = sum((x//2 for x in s_n[1:-1])) * K
    if S[0] == S[-1]:
        sum_n += (s_n[0] // 2 + s_n[-1] // 2)
        sum_n += ((s_n[0] + s_n[-1]) // 2) * (K-1)
    else:
        sum_n += (s_n[0] // 2 + s_n[-1] // 2) * K
    print(sum_n)
    return

main()

Solution (Python):
def main():
    s = input()
    k = int(input())
    s_ = []
    f = False
    for i in range(len(s)):
        if not s_:
            s_.append(s[i])
        elif not s_[-1][0] == s[i]:
            s_.append(s[i])
        else:
            s_[-1] += s[i]
    if s_[0][0] == s_[-1][0]:
        if len(s_) > 1:
            if len(s_[0])%2 == 1 and len(s_[-1])%2==1:
                f = True
            s_[0] = s_[0] + s_[-1]
            s_ = s_[:-1] 
    ans = 0
    if len(s_) == 1 and len(s_[0])%2==1:
        l = len(s_[0])
        if k%2==0:
            ans = l*k//2
        else:
            ans = l*(k-1)//2+l//2
        print(ans)
    else:
        for i in s_:
            ans += len(i)//2
        ans *= k
        if f == True and not len(s_) == 1:
          ans -= 1
        print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
s = list(input())
n = len(s)
k = int(input())
flg = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        flg = 0
if flg:
    print(n * k // 2)
    exit(0)
t = s + s
tmp1 = 0
for i in range(n * 2 - 1):
    if t[i] == t[i + 1]:
        tmp1 += 1
        t[i + 1] = 'X'
tmp2 = 0
for i in range(n - 1):
    if s[i] == s[i + 1]:
        tmp2 += 1
        s[i + 1] = 'X'
tmp3 = tmp1 - tmp2 * 2
print(tmp3 * (k - 1) + tmp2 * k)

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

sameword = [1]
for i in range(1, len(s)):
  if s[i] == s[i-1]:
    sameword[-1] += 1
  else:
    sameword.append(1)  
    
if len(sameword) == 1:
  print((sameword[0] * k) // 2) 
elif s[0] != s[-1]:
  change = 0    
  for seq in sameword:
    change += seq // 2
  print(change * k)
else:
  init = sameword[0]
  last = sameword[-1]
  change = 0
  for i in range(1, len(sameword)-1):
    change += sameword[i] // 2
  print(change * k + (init // 2) + (last // 2) + ((init + last) // 2) * (k - 1))

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7


debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    S = SI()
    K = II()
    N = len(S)

    same_cnt = 0
    for c in S:
        if c == S[0]:
            same_cnt += 1

    if same_cnt == N:
        print((N*K)//2)
        return

    chunks = []
    prev = ''
    chunk_len = 0
    in_chunk = False
    for i, c in enumerate(S):
        dprint(c, in_chunk, prev, c)
        if prev == c:
            if in_chunk:
                chunk_len += 1
            else:
                chunk_len = 2
                chunk_start = i-1
                in_chunk = True
        else:
            if in_chunk:
                in_chunk = False
                chunks.append((chunk_start, chunk_len))
        prev = c

    if in_chunk:
        chunks.append((chunk_start, chunk_len))

    
    if S[0] == S[N-1]:
        start_chunk_head = chunks[0][0]
        end_chunk_tail = chunks[len(chunks)-1][0] + chunks[len(chunks)-1][1] - 1
        start_n, end_n = 1, 1
        start_chunk_i = 0
        cnt_for_end_chunk = 0
        cnt_for_start_chunk = 0
        end_chunk_i = len(chunks) - 1
        if start_chunk_head == 0:
            start_n = chunks[0][1]
            start_chunk_i = 1
            cnt_for_start_chunk = start_n // 2
        if end_chunk_tail == N-1:
            end_n = chunks[len(chunks)-1][1]
            end_chunk_i = len(chunks) - 2
            cnt_for_end_chunk = end_n // 2

        start_end_chunk_n = start_n + end_n

        dprint(chunks, start_n, end_n, start_end_chunk_n)
        dprint(start_chunk_i, end_chunk_i)
        cnt = 0
        for chead, clen in chunks[start_chunk_i:end_chunk_i+1]:
            cnt += clen //2
        dprint(cnt)
        cnt = cnt*K + start_end_chunk_n//2 * (K-1) + cnt_for_end_chunk + cnt_for_start_chunk

        print(cnt)
        return


    else:
        cnt = 0
        for chead, clen in chunks:
            cnt += clen //2
        print(cnt * K)
        return


solve()

Solution (Python):
import sys

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

onaji = 1
before = s[0]
for i in range(len(s)):
    now = s[i]
    if now != before:
        onaji = 0

if onaji == 1:
    print((len(s)*k)//2)
    sys.exit()

s_2 = s + s

num_first = 0
renzoku = 1
before = 0
for i in range(len(s)):
    now = s[i]
    if now == before:
        renzoku += 1
    else:
        num_first += renzoku // 2
        renzoku = 1
    before = now
num_first += renzoku // 2

num_rest = 0
renzoku = 1
before = 0
for i in range(len(s_2)):
    now = s_2[i]
    if now == before:
        renzoku += 1
    else:
        num_rest += renzoku // 2
        renzoku = 1
    before = now
num_rest += renzoku // 2

num_rest -= num_first

print(num_first + (k-1) * num_rest)

Solution (Python):
s=input()
k=int(input())
if len(set(s))==1:
  print(len(s)*k//2)
else:
  cnt=1
  s_ans=0
  x=s[0]
  for a in s[1:]:
    if a==x:
      cnt+=1
    else:
      s_ans+=cnt//2
      x=a
      cnt=1
  s_ans+=cnt//2
  if s[0]!=s[-1]:
    print(s_ans*k)
  else:
    ss=s*2
    cnt=1
    ss_ans=0
    x=ss[0]
    for a in ss[1:]:
      if a==x:
        cnt+=1
      else:
        ss_ans+=cnt//2
        x=a
        cnt=1
    ss_ans+=cnt//2
    print(s_ans*k+(ss_ans-s_ans*2)*(k-1))