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



Sample input:
issii
2


Sample output:
4

Submission status: Accepted

Solution code:
import sys

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

def count_min_operation(s):
	i = 0
	j = 0
	count = 0
	length = len(s)
	while j < length-1:
		c = 1
		for j in range(i+1,length):
			if s[i] == s[j]:
				c += 1
			else:
				count += c // 2
				i = j
				c = 1
				break

		if j == length-1:
			count += c // 2

	return count

for i in range(len(s)-1):
	if s[i] != s[i+1]:
		s1 = count_min_operation(s)
		s2 = count_min_operation(s+s)
		update = s2 - s1
		print(s1 + update * (k-1))
		sys.exit()

print(k * len(s) // 2)

Submission status: Accepted

Solution code:
import sys
S = input() 
K = int(input()) 
l = len(S)
ren = [0]*l
cnt = 0
 
for i in range(1,l):
  if S[i] == S[i-1]:
    cnt += 1
  elif S[i] != S[i-1]:
    ren[cnt] +=1
    cnt = 0
  if i == l-1:
    ren[cnt] +=1
    
ren = [K*i for i in ren]
   
if ren[l-1] or l ==1:
  print((l*K)//2)
  sys.exit()
  
if S[0]==S[-1]:
  start = 0
  while S[start]==S[start+1]:
    start += 1
  end = 0
  while S[-1-end]==S[-2-end]:
    end += 1
  ren[start+end+1] += K-1
  ren[start] -= K-1
  ren[end] -= K-1
 
 
ans = 0
 
for i in range(1,l):
  if i%2 ==0:
    ans += (i//2)*ren[i]
  else:
    ans += ((i+1)//2)*ren[i]
      
print(ans)

Submission status: Accepted

Solution code:
import sys
s_ = input()
k = int(input())

s = s_+"1"
#print(s)
RLE = []

cha = s[0]
cnt = 1

for i in range(1,len(s)):
    if s[i-1] == s[i]:
        cnt += 1
    else:
        RLE.append(cnt)
        cnt = 1
        cha = s[i]
#print(RLE)
ans = 0
if len(RLE)==1:
    print(RLE[0]*k//2)
    sys.exit()
if s[0]!= s[-2]:
    for c in RLE:
        ans += c//2
    #print(ans)
    ans *= k
    print(ans)
else:
    RLE[-1]+= RLE[0]
    for c in RLE[1:]:
        ans += c//2
    
    ans *= k
    ans -= RLE[-1]//2
    ans += RLE[0]//2
    ans += (RLE[-1]-RLE[0])//2
    print(ans)

Submission status: Accepted

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

ls = len(s)
l = []
tmp = 1
for i in range(ls-1):
    if (s[i] == s[i+1]):
        tmp += 1
    else:
        l.append(tmp)
        tmp = 1
l.append(tmp)

ans = 0
ss = 0
ll = len(l)
#文字列が1つの文字列で構成されていないとき
if (ll != 1) :
    #最初と最後の文字が同じとき
    if (s[0] == s[ls-1]):
        ans -= (l[0] // 2 + l[ll-1] // 2 - (l[0] + l[ll-1]) // 2) * (k-1)

    for i in range(ll):
        ss += l[i] // 2
    print(ans+ss*k)
else :
    print(ls * k // 2)

Submission status: Accepted

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

A = [0]
a = 0
for i in range(1, len(S)):
  if S[i-1] != S[i]:
    a += 1
    A.append(i)
A.append(len(S))
if a == 0:
  print((K * len(S)) // 2)
u = 0
if a != 0:
  B = [A[i] - A[i-1] for i in range(1, len(A))]
  for i in range(1, len(B) - 1):
    u += B[i] // 2
  u *= K
  if S[0] == S[len(S)-1]:
    u += B[0] // 2 + B[len(B)-1] // 2 + (K-1) * ((B[0] + B[len(B)-1]) // 2)
  else:
    u += K * (B[0] // 2 + B[len(B)-1] // 2)
  print(u)

Submission status: Accepted

Solution code:
import itertools as it 

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

groups = [(k, len(list(g))) for k, g in it.groupby(s)]

# print(groups)

if s[0] != s[-1]:
    answer = sum([x[1] // 2 for x in groups]) * k

elif len(set(s)) == 1:
    answer = (len(s) * k) // 2

elif s[0] == s[-1]:
    answer = sum([x[1] // 2 for x in groups[1:-1]]) * k + groups[0][1] // 2 + groups[-1][1] // 2
    answer += (k - 1) * ((groups[0][1] + groups[-1][1]) // 2)

print(answer)

Submission status: Accepted

Solution code:
from itertools import groupby

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

gr = []
for k, g in groupby(S):
    gr.append(len(list(g)))

if len(gr) == 1:
    print(len(S) * K // 2)
elif S[0] != S[-1]:
    print(sum([i // 2 for i in gr]) * K)
else:
    print(sum([i // 2 for i in gr]) * K - (gr[0] // 2 + gr[-1] // 2 - (gr[0] + gr[-1]) // 2) * (K - 1))

Submission status: Accepted

Solution code:
S = input()
s = len(S)
K = int(input())

if len(set(list(S))) == 1:
    print(len(S) * K // 2)
else:
    l = []
    left = 0
    right = 1
    while right < s:
        if S[left] == S[right]:
            right += 1
            if right == s:
                l.append(right - left)
        else:
            if right - left > 1:
                l.append(right - left)
                left = right
                right += 1
            else:
                left = right
                right += 1
    l_o = l[:]
    l = [i // 2 for i in l]
    if S[0] == S[-1]:
        if S[0] != S[1] and S[-1] != S[-2]:
            print(sum(l) * K + K - 1)
        elif S[0] == S[1] and S[-1] == S[-2]:
            print(sum(l) * K - (l[0] + l[-1] - (l_o[0] + l_o[-1]) // 2) * (K - 1))
        elif S[0] == S[1]:
            print(sum(l) * K - (l[0] - (l_o[0] + 1) // 2) * (K - 1))
        else:
            print(sum(l) * K - (l[-1] - (l_o[-1] + 1) // 2) * (K - 1))
    else:
        print(sum(l) * K)

Submission status: Accepted

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

ss = s + s

if len(set(s))==1:
    print(int((len(s)*k)/2))
else:
    a=0
    prev = "?"
    for v in s:
        if v==prev:
            a += 1
            prev = "?"
        else:
            prev = v

    b=0
    prev = "?"
    for v in ss:
        if v==prev:
            b += 1
            prev = "?"
        else:
            prev = v

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

Submission status: Accepted

Solution code:
s=list(input())
n=len(s)
k=int(input())
S=s*3
for i in range(n*3-1):
    if S[i]==S[i+1]:S[i+1]="A"
    
A=S[:n].count("A")
B=S[n:n*2].count("A")
C=S[n*2:n*3].count("A")

if s.count(s[0])==n:print(n*k//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
test=s*3
for i in range(len(s)*3-1):
    if test[i]==test[i+1]:
        test[i+1]="A"
A=test[:len(s)].count("A")
B=test[len(s):len(s)*2].count("A")
C=test[len(s)*2:len(s)*3].count("A")

if s.count(s[0])==len(s):
    print((len(s)*k)//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Submission status: Accepted

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

def connection_list(s):
  res = [0 for i in range(len(s))]
  prev = ''
  cnt = 0
  start = 0
  connected = 0
  for i in range(len(s)):
    if s[i] == prev:
      cnt += 1
    else:
      res[start] = cnt
      cnt = 0
      start = i
      prev = s[i]
  if cnt > 0:
    res[-1] = cnt
  if s[0] == s[-1]:
    connected = res[-1] + res[0] + 1
  return res, connected

def calc_ceil(n):
  return math.ceil(n/2)
cl, cn = connection_list(s)
if cl[-1] == len(cl) - 1:
  res = calc_ceil(len(cl) * k - 1)
else:
  cl = list(map(calc_ceil, cl))
  if cn > 0:
    res = sum(cl[1:-1]) * k + cl[0] + cl[-1] + calc_ceil(cn) * (k - 1)
  else:
    res = sum(cl) * k
print(res)

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
A,B,C=0,0,0
test=s*3
for i in range(len(s)*3-1):
    if test[i]==test[i+1]:
        test[i+1]="A"
A=test[:len(s)].count("A")
B=test[len(s):len(s)*2].count("A")
C=test[len(s)*2:len(s)*3].count("A")

if s.count(s[0])==len(s):
    print((len(s)*k)//2)
else:
    if k==1:print(A)
    elif k==2:print(A+B)
    else:print(A+B*(k-2)+C)

Submission status: Accepted

Solution code:
s = list(map(str, input().strip()))
k = int(input())

pre = 0
cnt = 1
ans = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        ans += cnt // 2
        cnt = 1
    pre = i

ans += cnt//2

if cnt == len(s):
    print((cnt*k)//2)
    exit(0)
pcnt = cnt
dcnt = 0
for i in s:
    if i == pre:
        cnt += 1
    else:
        dcnt = cnt - pcnt
        break

if dcnt % 2 and pcnt % 2:
    ans = ans * k + k - 1
else:
    ans *= k

print(ans)

Submission status: Accepted

Solution code:
s = input()
K = int(input())
a = 1
ans = 0
e = 0
l_s = []
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        a += 1
    else:
        l_s.append(a)
        a = 1
l_s.append(a)
if len(l_s) == 1:
    print((l_s[0]*K)//2)
    exit()
if s[0] == s[-1]:
    f = l_s[0]
    l = l_s [-1]
    l_s[-1] += l_s[0]
    del l_s[0]
    e = 1
for i in l_s:
    ans += i//2
if e == 1:
    M = ans*K - l_s[-1]//2
    M += f//2
    M += l//2
else:
    M = ans*K
print(M)

Submission status: Accepted

Solution code:
from itertools import groupby as gb
s=input()
k=int(input())
if len(set(s))==1:print((len(s)*k)//2);exit()
elif len(s)==2 and len(set(list(s)))==1:print(k);exit()
ji=[len(list(b))//2 for a,b in gb(s)]
l=k*sum(ji)
if s[0]==s[-1]:
  ji=[len(list(b)) for a,b in gb(s)]
  if ji[0]%2 and ji[-1]%2:l+=k-1
print(l)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
def counter(str):
    str_list = list(str)
    count = 0
    for i in range(1, len(str_list)):
        if str_list[i] == str_list[i-1]:
            str_list[i] = "&"
            count += 1
    return count
if k <= 3:
    print(counter(s*k))
else:
    moto = counter(s)
    cost1 = counter(s*2) - 2*moto
    cost2 = counter(s*3) - counter(s*2) - moto
    if k%2 == 0:
        print(moto*k + ((k-1)//2 + 1)*cost1 + ((k-1)//2)*cost2)
    else:
        print(moto*k + ((k-1)//2)*(cost1+cost2))

Submission status: Accepted

Solution code:
s = input()
k = int(input())
if len(set(list(s))) == 1:
  print(len(s)*k//2)
  exit()
l = list()
t = 0
c = s[0]
for x in s:
  if x == c:
    t += 1
  else:
    c = x
    l.append(t)
    t = 1
l.append(t)
ans = 0
if s[0]==s[-1]:
  ans += l[0]//2 + l[-1]//2 - (l[0]+l[-1])//2
  l[0] += l[-1]
  l.pop()
for x in l:
  ans += (x//2)*k
print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
key = s[0]
count = 1
sum_count = 0
list1=[]
ans = 0
ans2 =0
for i in range(1,len(s)):
    if key == s[i]:
        count += 1
    else:
        list1.append(count)
        count = 1
        key = s[i]
list1.append(count)
if len(list1) == 1:
    print(list1[0]*k//2)
elif s[0] == s[len(s)-1]:
    ans += (list1[0]+list1[len(list1)-1])//2*(k-1)
    for i in range(1,len(list1)-1):
        ans2 += list1[i]//2
    print(ans2*k+ans+list1[0]//2+list1[len(list1)-1]//2)
else:
    for i in range(len(list1)):
        ans += list1[i]//2
    print(ans*k)

Submission status: Accepted

Solution code:
s=input()
n=int(input())
l=len(s)
ct=0
for i in range(l-1):
 if s[i]==s[i+1]:
  ct=ct+0.5
 else:
  ct=int(ct+0.5)
ct=int(ct+0.5)
s1=s*3
l1=l*3
ct1=0
for i in range(l1-1):
 if s1[i]==s1[i+1]:
  ct1+=0.5
 else:
  ct1=int(ct1+0.5)
ct1=int(ct1+0.5)
ans=int((ct1-ct*3)/2*(n-1)+0.5)+ct*n
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
string = input()
K = int(input())

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


f = string[0]
l = string[-1]
if f == l:
    first = 1
    for i in range(1, len(string)):
        if string[i] == f:
            first += 1
        else:
            break
    last = 1
    for i in range(len(string) - 2, 0, -1):
        if string[i] == l:
            last += 1
        else:
            break
else:
    first = 0
    last = 0

ans = 0
final = 0
if (first + last) % 2 == 0 and first % 2 and last % 2:
    final = K - 1

string = string + '_'

count = 1
for n in range(1, len(string)):
    if string[n - 1] == string[n]:
        count += 1
    elif string[n - 1] != string[n] or n == len(string) - 1:
        if count >= 2:
            ans += (count // 2)
            #print(f'n: {n}')
            #print(count)
            #print(ans)
        count = 1
print(ans * K + final)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

    # Sの文字が1種類かどうか
    if len(pair) == 1:
        # WAの原因：kが偶奇の場合で場合分けしていたが不要だった
        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()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


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

    # Sの文字が1種類かどうか
    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()

Submission status: Accepted

Solution code:
# WAなんだこれ・・
# Sが１文字の時
# Sが同じ文字で複数文字の時
import sys
input = sys.stdin.readline

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


def main():
    comp_s = compress(S)
    # print(comp_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)
        # 暫定でS一つ分の合計をだす
        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

        # 残りのSの繰り返し分を足す
        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()

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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
 
# 2個連続の場合
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)

Submission status: Accepted

Solution code:
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 and flag2 % 2 != 0:
    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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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(count, ff, ll)

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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)

Submission status: Accepted

Solution code:
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)