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

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

if S[-1] == S[0]:
    letter = S[0]
    i = 1
    left_count = 1
    while letter == S[i]:
        left_count += 1
        i += 1
    right_count = 1
    i = len(S) - 2
    while letter == S[i]:
        right_count += 1
        i -= 1

    if left_count % 2 == 1 and right_count % 2 == 1:
        ans = K - 1
    else:
        ans = 0

else:
    ans = 0
tmp_count = 1
tmp_ans = 0
for s in range(1, len(S)):
    tmp = S[s]
    if tmp == S[s - 1]:
        tmp_count += 1
        if s == len(S) - 1:
            tmp_ans += tmp_count // 2
    else:
        tmp_ans += tmp_count // 2
        tmp_count = 1
print(tmp_ans * K + ans)

Solution (Python):

if __name__ == '__main__':

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

	leng = len(S)
	ans = 0

	
	if K == 1:
		ans = 0
	
	elif S.count(S[0]) == leng:
		ans = int(leng*K/2)
	else:
		
		start = ""
		cnt = 0
		rep = 0
		for x,y in enumerate(S):
			if x == 0:
				start = y
			else:
				if start == y:
					cnt += 1
				else:
					
					if cnt > 0:
						cnt += 1	
						rep += int(cnt/2)
						cnt = 0	

					start = y
		
		if cnt > 0:
			cnt += 1	
			rep += int(cnt/2)

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

			
			cnt1 = 1
			smoji = S[0]
			for x in range(1,leng):
				if S[0] == S[x]:
					cnt1 += 1
					smoji += S[0]
				else:
					break
			moto1 = int(cnt1/2)


			
			cnt2 = 1
			
			S = S[::-1]
			smoji += S[0]
			for x in range(1,leng):
				if S[0] == S[x]:
					cnt2 += 1
					smoji += S[0]
				else:
					break
			moto2 = int(cnt2/2)

			
			slen = len(smoji)
			moto3 = int(slen/2)

			kekka = (moto1 + moto2) - moto3
			kekka = kekka * (K -1)

			ans = rep * K - kekka
		else:
			ans = rep * K

	print(ans)

Solution (Python):



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

    prev = s[0]
    count = 1
    pair = list()

    
    for cur in s[1:]:
        if cur == prev:
            count += 1
        else:
            pair.append((prev, count))
            prev = cur
            count = 1

    if count >= 1:
        pair.append((prev, count))

    
    if len(pair) == 1:
        
        print(len(s) * k // 2)
    else:
        first = pair[0]
        last = pair[-1]
        ans = 0

        
        if first[0] == last[0]:
            for a, count in pair[1:-1]:
                ans += count // 2

            print(ans * k + first[1] // 2 + last[1] // 2 + (first[1] + last[1]) // 2 * (k - 1))
        else:
            for a, count in pair:
                ans += count // 2

            print(ans * k)


if __name__ == '__main__':
    main()

Solution (Python):



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

    prev = s[0]
    count = 1
    pair = list()

    
    for cur in s[1:]:
        if cur == prev:
            count += 1
        else:
            pair.append((prev, count))
            prev = cur
            count = 1

    if count >= 1:
        pair.append((prev, count))

    
    if len(pair) == 1:
        print(len(s) * k // 2)
    else:
        first = pair[0]
        last = pair[-1]
        ans = 0

        
        if first[0] == last[0]:
            for a, count in pair[1:-1]:
                ans += count // 2

            print(ans * k + first[1] // 2 + last[1] // 2 + (first[1] + last[1]) // 2 * (k - 1))
        else:
            for a, count in pair:
                ans += count // 2

            print(ans * k)


if __name__ == '__main__':
    main()

Solution (Python):



import sys
input = sys.stdin.readline

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


def main():
    comp_s = compress(S)
    

    
    if len(comp_s) == 1:
        char_length = comp_s[0][1] * K
        
        print(char_length // 2)
    else:
        increment = 0
        for s in comp_s:
            increment += (s[1] // 2)
        
        total = increment

        
        if comp_s[0][0] == comp_s[-1][0]:
            
            if comp_s[0][1] % 2 != 0 and comp_s[-1][1] % 2 != 0:
                increment += 1

        
        total += increment * (K - 1)

        print(total)


def compress(S):
    prevc = None
    tmp_count = 1
    comp_s = []
    for i in range(len(S) - 1):
        if i == 0:
            pass
        elif prevc == S[i]:
            tmp_count += 1
        else:
            comp_s.append([prevc, tmp_count])
            tmp_count = 1

        prevc = S[i]

    
    comp_s.append([prevc, tmp_count])

    return comp_s


if __name__ == '__main__':
    main()

Solution (Python):
def count(t):
    uniqued_t = [t[0]]
    cnt = [1]
    for ti in t[1:]:
        if ti == uniqued_t[-1]:
            cnt[-1] += 1
        else:
            cnt.append(1)
            uniqued_t.append(ti)
    return sum(i // 2 for i in cnt)

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

if all(i == s[0] for i in s):
    print(len(s) * k // 2)
else:
    t1 = count(s)
    t2 = count(s * 2)
    print(t1+(t2-t1)*(k-1))

Solution (Python):
s = input()
k = int(input())
if len(set(s)) == 1:
    print((len(s)*k)//2)
else:
    l_list = []
    check = s[0]
    cnt = 1
    for i in s[1:]:
        if check == i:
            cnt += 1
        else:
            l_list.append(cnt)
            check = i
            cnt = 1
    l_list.append(cnt)
    ans = 0
    if s[0] == s[-1]:
        for i, l in enumerate(l_list):
            if 0 < i < len(l_list)-1:
                ans += (l//2)*k
            else:
                ans += l//2
        ans += ((l_list[0]+l_list[-1])//2)*(k-1)
    else:
        for l in l_list:
            ans += (l//2)*k
    print(ans)

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

chkd, flg = 0, 1
cnt = 0
cnt_list =[]
 
for i in range(1, len(S)):
    cnt += 1
    if S[i] != S[i-1]:
        flg = 0
    if chkd == 1:
        chkd = 0
        continue
    if S[i] == S[i-1]:
        cnt_list.append(cnt)
        chkd = 1
 

S2 = S*2
chkd = 0
cnt = 0
cnt_list2 =[]
 
for i in range(1, len(S2)):
    cnt += 1
    if chkd == 1:
        chkd = 0
        continue
    if S2[i] == S2[i-1]:
        cnt_list2.append(cnt)
        chkd = 1

if flg: 
    print(len(S)*K // 2)
elif S[0] == S[-1]:
    print(len(cnt_list) + (len(cnt_list2)-len(cnt_list)) * (K - 1))
else :
    print(len(cnt_list)*K)

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

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

if len(set(S)) == 1 and len(S) % 2 == 1:
    print(len(S)*K//2)
    exit(0)

pre_s = ''
flag1 = 0
flag2 = 0
flag = True

pre_s = S[0]
flag1 = 1
for s in S[1:]:
    if pre_s == s:
        flag1 += 1
    else:
        break

if S[-1] == S[0]:
    pre_s = S[-1]
    flag2 = 1
    for i, s in enumerate(S[::-1]):
        if i == 0:
            continue
        if pre_s == s:
            flag2 += 1
        else:
            break

    
    if flag1 > 0 and flag1 % 2 == 0:
        flag = False

pre_s = ''
rep_count = 0
    

for s in S:
    if pre_s == s:
        rep_count += 1
        pre_s = s.upper()
    else:
        pre_s = s

rep_count *= K
if S[0] == pre_s and flag:
    rep_count += K - 1


print(rep_count)

Solution (Python):
import sys
S = input()
K = int(input())
if len(S) == 1:
    print(K//2)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and cnt0%2 != 0 and cntL %2 != 0:
    ans += K-1
if cntL == len(S):
    if (K*cntL)%2==0:
        print((K*cntL)//2)
    else:
        print((K*cntL-1)//2)
    sys.exit()
print(ans)

Solution (Python):
s = input()
k = int(input())
count = []
now = 1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        now += 1
    else:
        count.append(now)
        now = 1
count.append(now)
if len(count) == 1:
    ans = (count[0] * k) // 2
else:
    if s[0] == s[-1]:
        temp = 0
        for i in count:
            temp += i // 2
        ans = (temp * k) - ((count[0] // 2) + (count[-1] // 2)  - ((count[0] + count[-1]) // 2)) * (k - 1)
    else:
        ans = sum([c // 2 for c in count]) * k
print(ans)

Solution (Python):
S=input()
N=int(input())
a=len(S)
b=0
for i in range(a-1):
 if S[i]==S[i+1]:
  b+=0.5
 else:
  b=int(b+0.5)
b=int(b+0.5)
S2=S*3
a2=a*3
b2=0
for i in range(a2-1):
 if S2[i]==S2[i+1]:
  b2+=0.5
 else:
  b2=int(b2+0.5)
b2=int(b2+0.5)
c=int((b2-b*3)/2*(N-1)+0.5)+b*N
print(c)

Solution (Python):
S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l * K // 2)
    exit()

i = ff
c = 1
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 1
    i += 1
count += c // 2



print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)

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

tmp = S[0]
count = 1
S_num = list()
for i in range(1,len(S)):
	if S[i] == tmp:
		count += 1
	else:
		S_num.append(count)
		tmp = S[i]
		count = 1
S_num.append(count)

if len(S_num) == 1:
	print(len(S)*K//2)
else:
	if S[0] != S[-1]:
		S_num = list(map(lambda x: x//2, S_num))
		print(sum(S_num)*K)
	else:
		a = S_num[0]
		b = S_num[-1]
		S_num = list(map(lambda x: x//2, S_num))
		print(sum(S_num)*K - (a//2 + b//2 - (a+b)//2)*(K-1))

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

f = False
pre = S[0]
for s in S:
    if pre != s:
        break
else:
    f = True

def one(S):
    c = 0
    r = 1
    pre = ""
    for s in S:
        if pre == s:
            r += 1
        else:
            c += r // 2
            r = 1
        pre = s
    c += r // 2
    return c

def two(S):
    c = 0
    r = 1
    pre = ""
    for s in S*2:
        if pre == s:
            r += 1
        else:
            c += r // 2
            r = 1
        pre = s
    c += r // 2
    return c

o = one(S)
t = two(S)

if f:
    print(len(S)*K//2)
else:
    print(o*K + (t-o*2)*(K-1))

Solution (Python):
s=input()
k=int(input())
d=[]
w=""
cnt=0
for i in s:
    if w!=i:
        w=i
        if cnt:
            d.append(cnt)
        cnt=1
    else:
        cnt+=1
d.append(cnt)
t=len(d)
if t==1:
    print(d[0]*k//2)
else:
    ans=0
    for i in range(t):
        if i==0 and s[0]!=s[-1] and d[0]>1:
            ans+=d[0]//2*k
        elif i==0 and s[0]==s[-1] and d[0]>1:
            ans+=d[0]//2
        elif i==t-1 and s[0]==s[-1]:
            ans+=(d[0]+d[-1])//2*(k-1)
            ans+=d[-1]//2
        elif i==t-1 and s[0]!=s[-1]:
            ans+=d[-1]//2*k
        elif d[i]>1:
            ans+=d[i]//2*k
    print(ans)

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

li = []

count = 1
tmp = s[0]

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

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

if len(li) == 1:
    print(li[0][1] * k // 2)
elif li[0][0] == li[-1][0] and k >= 2:
    li2 = [(li[0][0],li[0][1]+li[-1][1])]
    for i in range(1,len(li)-1):
        li2.append(li[i])
    ans = 0
    for i in li2:
        ans += i[1] // 2
    ans *= k - 1
    for i in li:
        ans += i[1] // 2
    print(ans)
else:
    ans = 0
    for i in li:
        ans += i[1] // 2
    ans *= k
    print(ans)

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

X = []
b = S[0]
cnt = 1
for i in range(1, len(S)):
    s = S[i]
    if b == s:
        cnt += 1
    else:
        X.append([b, cnt])
        cnt = 1
        b = s
X.append([b, cnt])

if len(X) == 1:
    print(X[0][1] * K // 2)
elif X[0][0] != X[-1][0]:
    print(sum(x[1] // 2 for x in X) * K)
else:
    s = X[0]
    e = X.pop(-1)
    if (s[1] + e[1]) % 2 == 0:
        X.append(e)
        print(sum(x[1] // 2 for x in X) * K + (K - 1))
    else:
        X[0] = [s[0], s[1] + e[1]]
        print(sum(x[1] // 2 for x in X) * K)

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

X = []
b = S[0]
cnt = 1
for i in range(1, len(S)):
    s = S[i]
    if b == s:
        cnt += 1
    else:
        X.append([b, cnt])
        cnt = 1
        b = s
X.append([b, cnt])

if len(X) == 1:
    print(X[0][1] * K // 2)
elif X[0][0] != X[-1][0]:
    print(sum(x[1] // 2 for x in X) * K)
else:
    s = X.pop(0)
    e = X.pop()
    result = sum(x[1] // 2 for x in X) * K + s[1] // 2 + e[1] // 2 + ((s[1] + e[1]) // 2) * (K - 1)
    print(result)

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

X = []
b = S[0]
cnt = 1
for i in range(1, len(S)):
    s = S[i]
    if b == s:
        cnt += 1
    else:
        X.append([b, cnt])
        cnt = 1
        b = s
X.append([b, cnt])

if len(X) == 1:
    print(X[0][1] * K // 2)
elif X[0][0] == X[-1][0]:
    result = 0
    s = X.pop(0)
    e = X.pop()
    result = sum(x[1] // 2 for x in X) * K + s[1] // 2 + e[1] // 2 + ((s[1] + e[1]) // 2) * (K - 1)
    print(result)
else:
    print(sum(x[1] // 2 for x in X) * K)

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

X = []
b = S[0]
cnt = 1
for i in range(1, len(S)):
    s = S[i]
    if b == s:
        cnt += 1
    else:
        X.append([b, cnt])
        cnt = 1
        b = s
X.append([b, cnt])

result = 0
if len(X) == 1:
    print(X[0][1] * K // 2)
elif X[0][0] == X[-1][0]:
    s = X.pop(0)
    e = X.pop()
    for x in X:
        result += x[1] // 2
    result *= K

    result += s[1] // 2
    result += e[1] // 2
    x = (s[1] + e[1]) // 2
    result += x * (K - 1)
    print(result)
else:
    print(sum([x[1] // 2 for x in X]) * K)

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

X = []
b = S[0]
cnt = 1
for i in range(1, len(S)):
    s = S[i]
    if b == s:
        cnt += 1
    else:
        X.append([b, cnt])
        cnt = 1
        b = s
X.append([b, cnt])

result = 0
if len(X) == 1:
    print(X[0][1] * K // 2)
elif X[0][0] == X[-1][0]:
    s = X.pop(0)
    e = X.pop()
    for x in X:
        result += x[1] // 2
    result *= K

    result += s[1] // 2
    result += e[1] // 2
    x = (s[1] + e[1]) // 2
    result += x * (K - 1)
    print(result)
else:
    for x in X:
        result += x[1] // 2
    result *= K
    print(result)

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


def count(s):
    n = 0
    while 1 < len(s):
        i = 1
        while i < len(s) and s[i] == s[0]:
            i += 1
        n += i // 2
        s = s[i:]
    return n


if S[0] == S[-1]:
    if len(set(S)) != 1:
        len_head = len(S) - len(S.lstrip(S[0]))
        len_tail = len(S) - len(S.rstrip(S[-1]))
        num_connection = (len_head + len_tail) // 2
        n = count(S.strip(S[0])) * K + num_connection * (K - 1) + len_head // 2 + len_tail // 2
    else:
        n = len(S) * K // 2
else:
    n = count(S) * K
print(n)

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

if len(set(s[:])) == 1:
    print(round(len(s) * n / 2.0))
else:
    current_ch = start_ch = s[0]
    end_ch = s[-1]
    counter = 1
    px = prev_max_len = 0

    for i in s[1:]:
        if i == current_ch:
            counter += 1
        else:
            if prev_max_len == 0:
                prev_max_len = counter

            current_ch = i
            px += math.floor(counter/2.0)
            counter = 1

    px += math.floor(counter/2.0)
    ans = px * n
    if start_ch == end_ch and prev_max_len % 2 == 1 and counter % 2 == 1:
        ans += n - 1

    print(ans)

Solution (Python):
S = str(input())
K = int(input())
count = 0
connected = []
for i,x in enumerate(S[1:]):
    count += 1
    if x!=S[i]:
        connected.append(count)
        count=0
    if i==len(S)-2:
        connected.append(count+1)

result = 0
if len(S)==1:
    result=K//2
elif len(connected)==1:
    result=(connected[0]*K)//2
elif S[0]!=S[-1]:
    for c in connected:
        result+=c//2
    result *= K
elif S[0]==S[-1]:
    _connected = connected[1:]
    _connected[-1] += connected[0]
    for _c in _connected:
        result+=_c//2
    result *= (K-1)
    for c in connected:
        result+=c//2    
print(result)

Solution (Python):
S=list(input())
K=int(input())
l=[1]
for i in range(len(S)-1):
    if S[i]==S[i+1]:l[-1]+=1;
    else:l.append(1)
if len(l)==1:ans = len(S)*K//2;
else:
    l2=[i//2 for i in l]
    if S[0]==S[-1]:
        ht=(l[0]+l[-1])//2
        ans=l2.pop(0)
        ans+=l2.pop(-1)
        ans+=sum(l2)*K
        ans+=ht*(K-1)
    else:
        ans=sum(l2)*K
print(int(ans))

Solution (Python):



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

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

if flag:
    print(N*K // 2)
    exit()
cnt = 0
changed = [False for i in range(N)]
for i in range(1,N):
    if S[i] == S[i-1] and not changed[i-1]:
        cnt += 1
        changed[i] = True
ans = cnt * K
if S[0] == S[N-1]:
    head = 1
    tail = 1
    i = 0
    while S[i] == S[i+1]:
        head += 1
        i += 1
    i = N-1
    while S[i] == S[i-1]:
        tail += 1
        i -= 1
    if (head + tail) % 2 == 0:
        ans += K-1
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

S = input().strip()
K = int(input())

fs = S[0]
fnum = 1
fi = len(S)-1
for i, ts in enumerate(S[1:]):
	if ts==fs:
		fnum += 1
	else:
		fi = i+1
		break
fnum = fnum//2

if fi==len(S)-1:
	ans = (len(S)*K)//2
else:
	ls = S[-1]
	lnum = 1
	for i, ts in enumerate(S[-2::-1]):
		if ts==ls:
			lnum += 1
		else:
			li = len(S)-i-2
			break
	lnum = lnum//2

	mnum = 0
	pre = ''
	num = 0
	for ts in S:
		if ts==pre:
			num += 1
		else:
			mnum += num//2
			num = 1
			pre = ts
	mnum += num//2
	mnum = mnum - fnum - lnum

	if S[0]==S[-1]:
		clen = len(S)-(li-fi+1)
		cnum = clen//2
		ans = fnum + lnum + mnum*K + cnum*(K-1)
	else:
		ans = (fnum+mnum+lnum)*K

print(ans)

Solution (Python):
from math import floor

word = input()
num = int(input())
length=len(word)

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=word[index]
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        if length==1:
            char_last=char_now
            stack.append(count)
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)

for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first and len(stack)>=2:
    sum =(subsum+1)*num -1
elif len(stack)==1:
    sum = floor(length*num/2)
else:
    sum =(subsum)*num

print(sum)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, pdb
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf, eps, mod = 10 ** 20, 1.0 / 10**10, 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 LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def _cumsum(l): return list(itertools.accumulate(l)) 
"""
なかのものと端のもの
両端が一緒かどうか
一緒なら、K-1個変える
中を変える/2切り下げ
まず、端が一緒か
"""
S = input()
K = _I()
ans = 0

if len(set(S)) == 1:
    print(int(math.floor(len(S)*K/2)))
    exit()

S2 = S*2
S3 = S*3

torn_list_1 = []
previous = 'hoge'
for s in S2:
    if s == previous:
        torn_list_1[-1] += 1
    else:
        torn_list_1.append(1)
        previous = s
torn_list_2 = []
previous = 'hoge'
for s in S3:
    if s == previous:
        torn_list_2[-1] += 1
    else:
        torn_list_2.append(1)
        previous = s

ans2 = 0
ans3 = 0
for t in torn_list_1:
    if t>=2:
        ans2+=int(math.floor(t/2))

for t in torn_list_2:
    if t>=2:
        ans3+=int(math.floor(t/2))

diff = ans3-ans2

ans = ans2 + (K-2)*diff
print(ans)

Solution (Python):
S = input()
K = int(input())
l = [1]
for i in range(len(S) - 1):
  if S[i] == S[i+1]:
    l[-1] += 1
  else:
    l.append(1)
if len(l) == 1:
  ans = len(S) * K // 2
else:
  l_2 = []
  for i in range(len(l)):
    l_2.append(l[i] // 2)
  if S[-1] == S[0]:
    LtoF = (l[0] + l[-1]) // 2
    ans = l_2.pop(0)
    ans += l_2.pop(-1)
    ans += sum(l_2) * K
    ans += LtoF * (K - 1)
  else:
    ans = sum(l_2) * K
print(ans)

Solution (Python):
txt, n = [input() for i in range(2)]
n = int(n)

first_flg = True
present_char = txt[0]
present_cnt = 0
numlen_list = []

for x in txt:
  if x == present_char:
    present_cnt += 1
  else:
    first_flg = False
    numlen_list.append(present_cnt)
    present_cnt = 1
    present_char = x

numlen_list.append(present_cnt)
guruguru_flg = (txt[0] == txt[-1])


if len(numlen_list) == 1:
  print(numlen_list[0] * n // 2)
elif guruguru_flg:
  print(sum([x // 2 for x in numlen_list[1:-1]]) * n
        + (numlen_list[0] + numlen_list[-1]) // 2 * (n-1)
        + numlen_list[0] // 2
        + numlen_list[-1] // 2)
else:
  print(sum([x // 2 for x in numlen_list]) * n)

Solution (Python):
import math

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

if len(set(S)) == 1:
  print(math.floor(len(S)*K/2))
  quit()
    
first = S[0]

a = 0
for l in S:
  if l == first:
    a+=1
  else:
    break

last = S[-1]
b = 0
for l in S[::-1]:
  if l == last:
    b+=1
  else:
    break

prev = S[a]
c = 0

for l in S[a+1:-b]:
  if prev == l:
    c += 1
    prev = ""
  else:
    prev = l

d = 0
if first == last:
  d =  math.floor((a+b)/2)
else:
  d = math.floor(a/2) +  math.floor(b/2)
ans = math.floor(a/2) + d*(K-1) + c*K + math.floor(b/2)
print(ans)

Solution (Python):
s = list(input())
k = int(input())
S = s.copy()
S_3 =s.copy()
cnt = 0
cnt_2 = 0
cnt_3 = 0


for i in range(1, len(s)):
    if s[i-1] == s[i]:
        cnt += 1
        s[i] = "A"



for i in range(len(s)):
    S.append(S[i])

for i in range(1, len(S)):
    if S[i-1] == S[i]:
        cnt_2 += 1
        S[i] = "A"








for i in range(len(S)):
    S_3.append(S_3[i])

for i in range(1, len(S_3)):
    if S_3[i-1] == S_3[i]:
        cnt_3 += 1
        S_3[i] = "A"



if k == 1:
    print(cnt)
elif k == 2:
    print(cnt_2)
elif k == 3:
    print(cnt_3)
elif cnt_2 - cnt == cnt_3 - cnt_2:
    print(cnt + (cnt_2 - cnt) * (k-1))
else:
    if k % 2 == 0:
        print(int(cnt + (cnt_2 - cnt) * (k / 2) + (cnt_3 - cnt_2) * (k / 2 - 1)))
    else:
        print(int(cnt + (cnt_2 - cnt) * ((k - 1) / 2) + (cnt_3 - cnt_2) * ((k - 1) / 2)))

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

def counter(s):
    count = 0
    for i,letter in enumerate(s):
        if i != 0:
            if letter == letter_prev:
                count += 1
                letter_prev = '0'
            else:
                letter_prev = letter
        else:
            letter_prev = letter
    return count    

if s.count(s[0]) == len(s):
    print(len(s)*k//2)
elif s[0] != s[-1]:
    print(counter(s)*k)
else:
    a = 0
    b = 0
    i = 0
    while s[i] == s[0]:
        i += 1
        a += 1
    i = 1
    while s[(-1)*i] == s[0]:
        i += 1
        b += 1
    print(counter(s)*k + (k-1)*((a+b)//2 - a//2 - b//2))

Solution (Python):

def func():
    if len(S) == 1:
        return int(K/2)
    
    cnt = 1
    pre_s = S[0]
    tup_list = []
    for s in S[1:]:
        if pre_s == s:
            cnt += 1
        else:
            tup_list.append([pre_s, cnt])
            cnt = 1
        pre_s = s
    else:
        tup_list.append([pre_s, cnt])

    if len(tup_list) == 1:
        return int(cnt* K/2)
    
    ext = 0
    if tup_list[0][0] == tup_list[-1][0]:
        a = tup_list.pop()[1]
        b = tup_list[0][1]
        ext = int(a/2) + int(b/2) - int((a+b)/2)
        tup_list[0][1] += a
        
    res = 0
    for _,cnt in tup_list:
        res += int(cnt/2) * K
    return res + ext
    
S = input()
K = int(input())
print(func())

Solution (Python):
s = [c for c in input()]
k = int(input())

import copy

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

elif len(s) == len(set(s)):
    print(0)
    
elif s[0] != s[-1]:
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    print(c*k)

else: 
    s2 = copy.copy(s)
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    if s[-1] == 'Z':
        print(c*k)
    else:
        c2 = 1
        s2[0] = 'Z'
        for i in range(1, len(s)):
            if s2[i-1] == s2[i]:
                s2[i] = 'Z'
                c2 = c2 + 1
        if s2[-1] == 'Z':
            if k % 2 == 0:
                print(int((c+c2)*(k/2)))
            else:
                print(int((c+c2)*((k-1)/2)+c))
        else:
            print(c + c2*(k-1))

Solution (Python):
import copy


def count(s):
    s = list(s)
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '*'
            cnt += 1
    return cnt


def main():
    S = input()
    K = int(input())
    if len(set(S)) == 1:
        return (len(S) * K) // 2

    else:
        n1 = count(S)
        n2 = count(S + S)
        return (K - 1) * (n2 - n1) + n1
            

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

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

sum = 0
x = 1
if L == 1:
  print(K//2)
elif L == 2 and S[0] == S[1]:
  print(K)
elif S[0] != S[-1]:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)*K
  print(sum)
else:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)
      break
  if x == L:
    print((L * K) // 2)
  else:
    y = x
    x = 1
    for i in range(y, L-1):
      if S[i] == S[i+1]:
        x += 1
      else:
        sum += (x // 2) * K
        x = 1
    sum += (x // 2)
    sum += ((x+y) // 2) * (K-1)
    print(sum)

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

a = 1
data = []
for i in range(n-1):
    if s[i] == s[i+1]:
        a += 1
    else:
        data.append(a)
        a = 1
data.append(a)

ans = 0

if s[0] != s[-1]:
    for d in data:
        ans += d//2
    ans *= k
elif len(data) == 1:
    ans = n*k//2
else:
    for i in range(1,len(data)-1):
        ans += data[i]//2
    ans *= k
    ans += data[0]//2 + data[-1]//2 + (data[0] + data[-1])//2 * (k-1)

print(ans)

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




A = [] 
count = 1
for i in range(n): 
 if(i != 0):
  if(S[i]== tmp): count += 1
  if((S[i] != tmp)or(i == n-1)):
    A.append([tmp,count])
    count = 1
 tmp = S[i]
if(n == 1): A.append([S[0],1])

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

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

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

def nasu(L):
  ans = 0
  N = len(L)
  t = L[0]
  cnt = 1
  for i in range(1, N):
    if t == L[i]:
      cnt += 1
    else:
      ans += cnt // 2
      cnt = 1
    t = L[i]
  ans += cnt // 2
  return ans 

a = nasu(S)
b = nasu(S + S)

print(a + (b - a) * (K - 1))

Solution (Python):
from fractions import gcd
import math

mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def cnt(s, k):
    s = s * k
    ans = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = ' '
            ans += 1
    return ans


def main():
    s = [i for i in input()]
    k = int(input())
    if len(set(s)) == 1:
        print(k * len(s) // 2)
        return
    if k <= 100:
        ans = cnt(s, k)
        print(ans)
        return
    cnt50 = cnt(s, 50)
    cnt51 = cnt(s, 51)
    dif = cnt51 - cnt50
    print(cnt50 + (k - 50) * dif)

main()

Solution (Python):
S = list(input())
K = int(input())
H=S[0]

flag=0
if(S[0]==S[len(S)-1]):
    flag=1
count=0
if(flag==0):
    for i in range(len(S)-1):
        if(S[i]==S[i+1]):
            S[i+1]="
            count+=1        
    print(count*K)
    exit()

flag2=0
for i in range(len(S)-1):
    if(S[i]!=S[i+1]):
        flag2=1
if(flag2==0):
    print((len(S)*K)//2)
    exit()

S1=S[:]
S2=S[:]+S[:]

count1=0
for i in range(len(S1)-1):
    if(S1[i]==S1[i+1]):
        S1[i+1]="
        count1+=1
count2=0
for i in range(len(S2)-1):
    if(S2[i]==S2[i+1]):
        S2[i+1]="
        count2+=1

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

Solution (Python):
S = list(input())
K = int(input())
H=S[0]

flag=0
if(S[0]==S[len(S)-1]):
    flag=1
count=0
if(flag==0):
    for i in range(len(S)-1):
        if(S[i]==S[i+1]):
            S[i+1]="
            count+=1        
    print(count*K)
    exit()

flag2=0
for i in range(len(S)-1):
    if(S[i]!=S[i+1]):
        flag2=1
if(flag2==0):
    print((len(S)*K)//2)
    exit()

S1=S[:]
S2=S[:]+S[:]
S3=S[:]+S[:]+S[:]
RS=S[:]

count1=0
for i in range(len(S1)-1):
    if(S1[i]==S1[i+1]):
        S1[i+1]="
        count1+=1
count2=0
for i in range(len(S2)-1):
    if(S2[i]==S2[i+1]):
        S2[i+1]="
        count2+=1
count3=0
for i in range(len(S3)-1):
    if(S3[i]==S3[i+1]):
        S3[i+1]="
        count3+=1

N=count3-count2
E=count2-N

while(True):
    if(RS[0]==H):
        RS.pop(0)
    elif(RS[len(RS)-1]==H):
        RS.pop(len(RS)-1)
    else:
        break
rcount=0
for i in range(len(RS)-1):
    if(RS[i]==RS[i+1]):
        RS[i+1]="
        rcount+=1

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

Solution (Python):
from itertools import groupby
s = input()
k = int(input())
l = [[k, len(list(g))] for k, g in groupby(s)]
ans = 0
ans2 = 0
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
if len(l) > 1:
  if l[0][0] == l[-1][0]:
    if l[0][1] % 2 == 1 and  l[-1][1] % 2 == 1 :
        ans2 = k - 1
if len(s) == 1:
  print(k // 2)
elif len(l) == 1:
  
  
  
  print(len(s) * k // 2)
else:
  print(ans * k + ans2)