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

REPLACED = '_'


def get_start(s):
    i = 1
    while i < len(s):
        if s[0] != s[i]:
            break
        i += 1
    return s[:i]


def replace(s):
    characters = list(s)
    s_length = len(s)
    s_types = len(set(s))

    if s_length == s_types:
        return s

    start = get_start(s)
    if len(start) == 1:
        pass
    elif len(start) % 2 == 0:
        for i in range(0, len(start), 2):
            characters[i] = REPLACED
    else:
        for i in range(1, len(start), 2):
            characters[i] = REPLACED

    i = 0
    while i < s_length - 1:
        chunk = characters[i:i + 3]

        if REPLACED in chunk:
            i += 1
            continue

        chunk_length = len(chunk)
        chunk_types = len(set(chunk))
        if chunk_length == 1:
            break
        elif chunk_length == 2:
            if chunk_types == 2:
                break
            else:
                characters[i + 1] = REPLACED
                break
        elif chunk_length == 3:
            if chunk_types == 3:
                i += 2
                continue
            elif chunk_types == 2:
                if chunk[0] == chunk[1]:
                    characters[i] = REPLACED
                    i += 2
                    continue
                elif chunk[1] == chunk[2]:
                    characters[i + 2] = REPLACED
                    i += 3
                    continue
                else:
                    i += 2
                    continue
            else:
                characters[i + 1] = REPLACED
                i += 2
                continue
        else:
            raise Exception

    return ''.join(characters)


def check(s, k):
    s_length = len(s)
    s_types = len(set(s))
    if (s_types == 1) or (s_length == 1):
        return s_length * k // 2
    elif s_types == s_length:
        return 0
    else:
        replaced_s = replace(s)
        replaced_count = replaced_s.count(REPLACED)
        if replaced_s[0] == replaced_s[-1] and replaced_s[0] != REPLACED:
            return (replaced_count * k) + (k - 1)
        else:
            return replaced_count * k


print(check(S, K))

Solution (Python):
s = input()
k = int(input())
c = 0
d = 0
a = []
s += "A"
for i in range(len(s)-1):
    if (s[i] == s[i+1]):
        c += 1
    else:
        a.append(c+1)
        d += 1
        c = 0
c = 0
if s[0] != s[len(s)-2]:
    for i in range(d):
        c += a[i]//2
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k//2))
else:
    for i in range(d):
        c += a[i]//2
    c = c*k - (k-1)*(a[0]//2 + a[d-1]//2 - (a[0] + a[d-1])//2)
    print(int(c))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



S = str(input())
n = len(S)
K = I()

num = 0
flag = False
for i in range(1,n):
    if S[i]==S[i-1] and flag==False:
        num += 1
        flag = True
    else:
        flag = False

if flag==False and S[0]==S[-1]:
    num2 = 1
    flag2 = False
    for i in range(2,n):
        if S[i]==S[i-1] and flag2==False:
            num2 += 1
            flag2 = True
        else:
            flag2 = False
    if n==1:
        flag2 = True
    if flag2==False:
        print(num+num2*(K-1))
        exit()
    else:
        print(num2*(K//2) + num*(K-K//2))
        exit()
else:
    print(num*K)

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
        if i == 0:
            p = 1
        while(S[i] == S[i+1]):

            if S[i+2] == '\n':
                p = 2
            count += 1
            i += 1
        if S[i+1] == '\n':
            p = 2
        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 (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):
from itertools import groupby

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

t = [(k, len(list(v))) for k, v in groupby(s)]
ans = 0
if len(t) == 1:
    ans += t[0][1] * k // 2
else:
    if t[0][0] == t[-1][0] and t[0][1] % 2 == 1 and t[-1][1] % 2 == 1:
        ans += k - 1
    ans += sum(v[1] // 2 for v in t) * k
print(ans)

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

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

left = 0
right = 1

while left < N and S[left] == S[0]:
    left += 1
while right <= N and S[-right] == S[-1]:
    right += 1
right -= 1

change = 0
isChanged = False
T = list(S[::])

for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1]:
        if not isChanged:
            change += 1
            isChanged = True
            continue
    isChanged = False

ans = change * K

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

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

def main():
    s = input().strip()
    k = int(input())

    d = [[s[0], 1]]
    for i in range(1, len(s)):
        if s[i] == d[-1][0]:
            d[-1][1] += 1
        else:
            d.append([s[i], 1])
    
    if len(d) == 1:
        print((len(s) * k) // 2)
    else:
        ans = 0
        for c, count in d:
            ans += count // 2
        ans *= k
        if d[0][0] == d[-1][0]:
            a = d[0][1]
            b = d[-1][1]
            ans -= ((a//2) + (b//2) - ((a+b)//2)) * (k-1)
        print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def a_connection_and_disconnection():
    from itertools import groupby
    S = input()
    K = int(input())

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

    
    counter = [len(list(g)) for _, g in groupby(S)]
    
    ans = sum([c // 2 for c in counter]) * K

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

print(a_connection_and_disconnection())

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

    if K == 1:
        print(cnt(S))
        return
    if K == 2:
        print(cnt(S*2))
        return
    i = 0
    j = 0
    while True:
        if S[i] != S[(j-1) % N]:
            break
        i += 1
        j = (j + 1) % N
        if i >= N:
            i = -1
            break
    if i == -1:
        print(K * N // 2)
        return
    start = S[:i]
    end = S[j:]
    mid = S[i:] + S[:j]
    
    
    
    
    
    print(cnt(start) + cnt(mid) * (K-1) + cnt(end))


def cnt(S):
    prev = "あ"
    cnt = 0
    for c in S:
        if c == prev:
            cnt += 1
            prev = "あ"
        else:
            prev = c
    return cnt
main()

Solution (Python):
S = input()
K = int(input())
d=[[0 for _ in range(400)] for _ in range(4)]
ans = [0,0,0,0]
l = len(S)
odd_diff = 0
even_diff = 0
if l == 1:
    print(int(K//2))
if l > 1:
    for i in range(4):
        s = S * (i + 1)
        l = len(s)
        for j in range(l-1):
            
            if j == 0 and s[j] == s[j+1]:
                ans[i] += 1
                d[i][0] = 1
            if j > 0 and s[j] == s[j+1] and d[i][j-1] == 0:
                ans[i] += 1
                d[i][j] = 1
    if K <= 4:
        print(int(ans[K-1]))
    elif K > 4 and K % 2 == 0:
        even_diff = ans[3] - ans[1]
        print(int(ans[1]+(K/2-1)*even_diff))
    elif K > 4 and K % 2 == 1:
        odd_diff = ans[2] - ans[0]
        print(int(ans[0]+(K//2)*odd_diff))

Solution (Python):

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

ppp=1
for i in range(n-1):
	if s[i]!=s[i+1]:
		ppp=0
		break
		
if ppp==1:
	print(n*k//2)
else:		
	counts=0
	i=0
	while i<n-1:
		if s[i]!=s[i+1]:
			i+=1
		else:
			i+=2
			counts+=1
			
	countss=0
	i=0
	while i<n*2-1:
		if ss[i]!=ss[i+1]:
			i+=1
		else:
			i+=2
			countss+=1
			
	if countss==counts*2:
		print(counts*k)
	else:
		print(counts*k+k-1)

Solution (Python):
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()

Solution (Python):
s=input()
t=int(input())
b=0
c=0
for i in range(c,len(s)-1):
  a=1
  for j in range(c+1,len(s)):
    if s[c]==s[j]:
      a+=1
    else:
      c=j
      break
  c=j
  b+=(a//2)
b=b*t
if s[0]==s[len(s)-1]:
  a=1
  c=1
  for k in range(len(s)-1):
    if s[k]==s[k+1]:
      c+=1
    else:
      break
  u=s[::-1]
  for l in range(len(s)-1):
    if u[l]==u[l+1]:
      a+=1
    else:
      break
  if a%2==1 and c%2==1:
    b+=t-1
w=s[::-1]
if w==s:
  if len(w)%2==1:
    b=t*len(w)//2
print(b)

Solution (Python):
S = input()


K = int(input())

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

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

elif S[0] != S[-1]:
    
    print(mycnt * K)

else:
    myleft = 1
    myright = 0
    mys = S[0]
    i = 1
    while S[i] == mys:
        myleft += 1
        i += 1
    
    myright = 0
    i = -1
    while S[i] == mys:
        myright += 1
        i -= 1
    
    print( (mycnt * K) - ( myleft//2 + myright//2 - (myright + myleft)//2 ) * ( K - 1 ) )

Solution (Python):
s = input()
k = int(input())
n = len(s)
if n == 1:
    if k == 1:
        print(0)
    else:
        print(k//2)
else:
    ans = []
    ss = 0
    i = 0
    while True:
        if i == n:
            break
        curr = s[i]
        temp = 1
        i += 1
        if i == n :
            break
        while s[i] == curr:
            temp += 1
            i += 1
            if i == n:
                break
        ans.append(temp)
    if len(ans) > 1:
        for i in ans:
            ss += (i//2)*k
        if s[0] == s[-1] and ans[0]%2 == ans[-1]%2 == 1:
            ss += k-1
    else:
        ss = n*k//2
    print(ss)

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

Solution (Python):
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

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

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

if s[-1] != s[-2]:
    li.append(1)

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

if s[0] != s[-1]:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

else:
    co = 0
    for i in range(len(li)):
        co += li[i]//2
    co *= k
    co -= ((li[0]//2+li[-1]//2)-(li[0]+li[-1])//2)*(k-1)
    print(co)

Solution (Python):
import sys


def connection_count(word)->list:
    i=0
    a=[]
    count=1
    while i<(len(word)-1):
        while word[i]==word[i+1]:
            count+=1
            i+=1
            if i>=(len(word)-1):
                break
        a.append(count)
        count=1
        i+=1
    if a[-1]==1:
        a.append(count)
    return a


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


if len(S)==1:
    print(K//2)
    sys.exit()


connection=connection_count(S)


if len(connection)==1:
    print(((connection[0]*K)//2))

elif S[0]!=S[-1]:
    connection2=list(map(lambda x:x//2,connection))
    print((sum(connection2)*K))

else:
    t=S*2
    connection2=list(map(lambda x:x//2,connection))
    connection3=list(connection_count(t))
    connection4=map(lambda x:x//2,connection3)
    a=sum(connection2)
    b=sum(connection4)
    ans=a+(b-a)*(K-1)
    print(ans)

Solution (Python):
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

li = [len(tuple(s)) for _ , s in groupby(s)]

if s[0] == s[-1] and k>1:
    if len(li) == 1:
        print(li[0]*k//2)
        exit()
    else:
        co = 0
        for l in range(1,len(li)-1):
            co += li[l]//2
        co = co*k
        co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
        print(co)
        exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

Solution (Python):
S = list(input())
K = int(input())
cnt = 1
ans = 0
counter = []

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

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

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

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

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

Solution (Python):
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import copy




'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  
print(list(set(a) & set(b)))  
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
midcount = 1
res = []
ch = ''
for t in range(len(T)):
    if (ch == T[t] and t != 0):
        midcount += 1
    elif (ch != T[t] and t != 0):
        res.append(midcount)
        midcount = 1
    if (ch == T[t] and t == len(T) - 1):
        res.append(midcount)
    ch = T[t]
if (len(T) == 1):
    res = [1]
if (len(res) == 1):
    print(str(int(num * len(T) / 2)))
elif (T[0] == T[len(T) - 1]):
    counter = 0
    tes = []
    tes.append(res[0] + res[len(res) - 1])
    tes += res[1: len(res) - 1]
    for x in tes:
        counter += int(x / 2)
    counter *= num - 1
    for x in res:
        counter += int(x / 2)
    print(counter)
else:
    counter = 0
    for x in res:
        counter += int(x / 2)
    counter *= num
    print(counter)

Solution (Python):


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

n=len(s)


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

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 )
    sys.exit(0)
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):
from collections import Counter
def main():
   s = input()
   k = int(input())
   li = []
   num = 1
   for i in range(len(s)-1):
       if s[i] != s[i+1]:
           li.append(num)
           num = 1
       else:
           num += 1
   li.append(num)
   temp = 0
   ans = 0
   if len(li) == 1:
       ans += len(s) * k // 2
   else:
       for i in li:
           temp += i//2
       if s[0] != s[-1]:
           ans = temp * k
       else:
           li[0] += li[-1]
           for i in li[:-1]:
               ans += i//2
           ans = ans * (k-1) + temp
   print(ans)
main()

Solution (Python):
from collections import Counter
def main():
   s = input()
   k = int(input())
   li = [1]
   for i in range(len(s)-1):
       if s[i] != s[i+1]:
           li.append(1)
       else:
           li[-1] += 1
   temp = 0
   ans = 0
   if len(li) == 1:
       ans += len(s) * k // 2
   else:
       for i in li:
           temp += i//2
       if s[0] != s[-1]:
           ans = temp * k
       else:
           li[0] += li[-1]
           for i in li[:-1]:
               ans += i//2
           ans = ans * (k-1) + temp
   print(ans)
main()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5)


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

    c = S[0]
    count = [1]
    for s in S[1:]:
        if c == s:
            count[-1] += 1
        else:
            count.append(1)
            c = s

    if S[0] != S[-1]:
        ans = sum(map(lambda x: x // 2, count))
        ans *= K
        print(ans)
        return

    if len(count) == 1:
        ans = (count[0] * K) // 2
        print(ans)
        return

    ans = 0
    for c in count:
        ans += c // 2
    count[0] += count[-1]
    ans += sum(map(lambda x: x // 2, count[:-1])) * (K - 1)
    print(ans)


main()

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

    li = []
    num = 1
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            li.append(num)
            num = 1
        else:
            num += 1
    li.append(num)

    temp = 0
    ans = 0
    if len(li) == 1:
        ans += len(s) * k // 2
    else:
        for i in li:
            temp += i//2
        if s[0] != s[-1]:
            ans = temp * k
        else:
            li[0] += li[-1]
            for i in li[:-1]:
                ans += i//2
            ans = ans * (k-1) + temp
    print(ans)

main()

Solution (Python):

from itertools import groupby


def main():
    S = input().rstrip()
    K = int(input())
    cnt = [len(tuple(s)) for _, s in groupby(S)]  
    if S[0] == S[-1] and K > 1:
        if len(cnt) == 1:  
            ans = cnt[0] * K // 2
        else:  
            ans = cnt[0] // 2 + cnt[-1] // 2 + (cnt[0] + cnt[-1]) // 2 * (K - 1)
            ans += sum(i // 2 for i in cnt[1:-1]) * K
    else:
        ans = sum(i // 2 for i in cnt) * K
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import groupby


def solve(s, k):
    rle = [len(list(g)) for c, g in groupby(s)]
    if len(rle) == 1:
        return len(s) * k // 2
    ans = sum(l // 2 for l in rle) * k
    if s[0] == s[-1] and rle[0] % 2 == 1 and rle[-1] % 2 == 1:
        ans += k - 1
    return ans


s = input()
k = int(input())
print(solve(s, k))

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

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

import collections


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

    
    if len(collections.Counter(S).keys()) == 1:
        print(len(S)*K//2)
        return

    
    n = f(S)

    
    if S[0] != S[-1]:
        print(n*K)
        return

    

    
    a = 0
    for c in S[::-1]:
        if c == S[0]:
            a += 1
        else:
            break
    
    b = 0
    for c in S:
        if c == S[0]:
            b += 1
        else:
            break

    
    ans = n*K
    
    
    ans += (K-1) * (-(a//2) -(b//2) + (a+b)//2)
    print(ans)

if __name__ == '__main__':
    main()

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

import sys
if len(s) == 1:
    if k == 1:
        print(0)
    else:
        print(k // 2)
    sys.exit(0)

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

count = 0
seq_len = 1
seq_len_array = []
for i in range(0, len(s) - 1):
    if s[i] != s[i+1]:
        seq_len_array.append(seq_len)
        seq_len = 1
    else:
        seq_len += 1
seq_len_array.append(seq_len)
count = sum(s // 2 for s in seq_len_array)
count = count * k

if s[0] == s[-1]:
    count = count - \
            (seq_len_array[0] // 2 + seq_len_array[-1] // 2) * (k - 1) + \
            ((seq_len_array[0] + seq_len_array[-1]) // 2) * (k - 1)
print(count)

Solution (Python):
def main():
    S = input()
    K = int(input())
    N = len(S)
    ans = 0
    from collections import Counter
    if N == 1:
        return print(K//2)
    tmp = 1
    for i in range(1,N):
        if S[i-1] == S[i]:
            tmp += 1
        else:
            ans += tmp // 2
            tmp = 1
    else:
        if tmp == N:
            ans = N*K//2
        else:
            ans += (tmp // 2)
            ans *= K
            if S[0] == S[-1]:
                first = 1
                for i in range(1,N):
                    if S[i-1] != S[i]:
                        break
                    first += 1
                last = 1
                for i in range(N-1,0,-1):
                    if S[i-1] != S[i]:
                        break
                    last += 1
                revise = first//2 + last//2 - (first+last)//2
                revise *= (K-1)
                ans -= revise
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

from itertools import groupby


def main():
    S = input().rstrip()
    K = int(input())
    cnt = [len(tuple(s)) for _, s in groupby(S)]  
    if S[0] == S[-1] and K > 1:
        if len(cnt) == 1:  
            ans = cnt[0] * K // 2
        else:  
            ans = cnt[0] // 2 + cnt[-1] // 2 + (cnt[0] + cnt[-1]) // 2 * (K - 1)
            ans += sum(i // 2 for i in cnt[1:-1]) * K
    else:
        ans = sum(i // 2 for i in cnt) * K
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = sys.stdin.buffer.readline
S = list(input())[: -1]
ls = S * 2
K = int(input())
if len(S) == 1:
  print(K // 2)
  exit(0)
if S.count(S[0]) == len(S):
  print(len(S) * K // 2)
  exit(0)
left = 0
mid = 0
right = 0
last = 0
i = 0
while i < len(S):
  if ls[i] == ls[i + 1]:
    left += 1
    if i == len(S) - 1:
      last = 1
    i += 1
  i += 1
i = 0 + last
while i < len(S):
  if ls[i] == ls[i + 1]:
    mid += 1
    i += 1
  i += 1
i = 0 + last
while i < len(S) - 1:
  if ls[i] == ls[i + 1]:
    right += 1
    i += 1
  i += 1
if K == 1:
  i = 0
  res = 0
  while i < len(S) - 1:
    if ls[i] == ls[i + 1]:
      res += 1
      i += 1
    i += 1
  print(res)
else:
  print(left + mid * max(K - 2, 0) + right)

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

from itertools import groupby
S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(K*len(S) // 2)
else:
    counts = [len(list(g)) for k, g in groupby(S)]
    if S[0] == S[-1] and counts[0] % 2 == counts[-1] % 2 == 1:
        t = 1
        counts[0] += counts.pop()
    else:
        t = 0
    print(K*sum(map(lambda x: x//2, counts)) - t)

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

from itertools import groupby
S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(len(S)*K // 2)
else:
    counts = [len(list(g)) for k, g in groupby(S)]
    if S[0] != S[-1]:
        t = 0
    else:
        t = 1 if counts[0] % 2 == counts[-1] % 2 == 1 else 0
        counts[0] += counts.pop()
    print(K*sum(map(lambda x: x//2, counts)) - t)

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

def replacePoint(s):
    arr = [-1]
    for i in range(len(s)):
        if arr[-1] != i-1:
            if s[i] == s[i-1]:
                arr.append(i)
    arr.remove(-1)
    return arr

double = s*2
triple = s*3
arr = replacePoint(s)
darr = replacePoint(double)
tarr = replacePoint(triple)
a = len(arr)
b = len(darr) - len(arr)

if arr+[i+len(s) for i in arr] == darr:
    print(a*k)
elif darr+[i+len(s)*2 for i in arr] == tarr:
    print(a*((k+1)//2)+b*(k//2))
else:
    print(a+b*(k-1))

Solution (Python):
def compress(s):
    z = []
    cont = 1
    for ca, cb in zip(s, s[1:]):
        if ca == cb:
            cont += 1
        else:
            z.append(cont)
            cont = 1
    z.append(cont)
    
    return z


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

    z1 = compress(s)
    if len(z1) == 1:
        
        return n * k // 2

    k1 = sum(map(lambda x: x // 2, z1))
    if s[0] != s[-1]:
        
        return k1 * k
    else:
        
        z2 = compress(s * 2)
        k2 = sum(map(lambda x: x // 2, z2))
        d = k2 - k1
        return k1 + d * (k - 1)


if __name__ == '__main__':
    print(main())

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

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

if __name__ == '__main__': main()