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
input = sys.stdin.readline
def inputs():
    return [int(x) for x in input().split()]
S = list(input())
N = int(input())
if len(S)==2:
  print(N//2)
  exit()
ans = 0
flag=0
if S[0]==S[-2]:
  cnt=1
  cntb=1
  for i in range(len(S)-2):
    if S[i]==S[i+1]:
      cnt+=1
    else:
      break
  for j in range(len(S)-2,0,-1):
    if S[j]==S[j-1]:
      cntb +=1
    else:
      break
  if cnt==cntb==len(S)-1 and cnt%2==1:
    S[0]="aaaaaa"
    ans+=1
    flag=2
  elif cntb%2!=0:
    if cnt%2==0:
      S[0]="aaa"
      ans+=1
    else:
      S[0]="aaa"
      ans+=1
      flag = 1
for i in range(len(S)-2):
    if S[i]==S[i+1]:
        ans +=1
        S[i+1]="aa"
if flag==0:
	print(ans*N)
elif flag==1:
  print(ans*(N-1)+ans-1)
else:
  print(ans*N-(N+2-1)//2)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
query = []
query2 = []
tmp = 1
if len(set(s)) == 1:
  print(len(s)*k//2)
  exit()

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tmp += 1
  else:
    query.append(tmp)
    tmp = 1
query.append(tmp)
tmp = 1
s2 = s+s
for i in range(len(s2)-1):
  if s2[i] == s2[i+1]:
    tmp += 1
  else:
    query2.append(tmp)
    tmp = 1
query2.append(tmp)
ans1 = 0
ans2 = 0
for i in query:
  ans1 += i//2
for i in query2:
  ans2 += i//2

if k== 1:
  print(ans1)
else:
  ans = ans1+(ans2-ans1)*(k-1)
  print(ans)

Submission status: Accepted

Solution code:
# 圧縮
# 001101 -> [0,1,0,1],[2,2,1,1]
def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == len(array)-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif array[i] != array[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = array[i+1]
            cnt = 0
    return pressed,cnts

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

N = len(S)
pressed, cnt = press(S)
if len(cnt) == 1:
    ans = N*K // 2
elif S[0] != S[-1]:
    ans = 0
    for c in cnt:
        ans += (c // 2) * K
else:
    ans = (cnt[0] + cnt[-1])//2 * (K-1)+ cnt[0]//2 + cnt[-1]//2
    for i in range(1,len(cnt)-1):
        ans += (cnt[i] // 2) * K

print(ans)

Submission status: Accepted

Solution code:
# test
def test():
    param = []
    val = ['issii', '2', '4']
    param.append(val)
    
    # issii issii issii
    #   x x   x x   x x
    val = ['issii', '3', '6']
    param.append(val)
    
    # qq qq qq
    #  x  x  x
    val = ['qq', '81', '81']
    param.append(val)

    val = ['cooooooooonteeeeeeeeeest', '999993333', '8999939997']
    param.append(val)

    # aaa aaa aaa aaa
    # _x_ x_x _x_ x_x
    val = ['aaa', '4', '6']
    param.append(val)
    
    # aaaa aaaa aaaa aaaa
    #  x x  x x  x x  x x
    val = ['aaaa', '4', '8']
    param.append(val)
    
    # aaaaa aaaaa aaaaa aaaaa
    #  x x  x x x  x x  x x x
    val = ['aaaaa', '4', '10']
    param.append(val)
    
    # i i i i
    #   x   x
    val = ['i', '4', '2']
    param.append(val)
    
    # isi isi isi isi
    #     x   x   x
    val = ['isi', '4', '3']
    param.append(val)
    
    # isssi isssi isssi
    #   x   x x   x x
    val = ['isssi', '3', '5']
    param.append(val)
    
    # isiii isiii isiii
    #    x  x  x  x  x
    val = ['isssi', '3', '5']
    param.append(val)
    
    # iissi iissi iissi
    #  x x  x  x  x  x
    val = ['iissi', '3', '6']
    param.append(val)
    
    print(param)

    for i in range(len(param)):
        ret = f(param[i][0], param[i][1])
        if ret == param[i][2]:
            chk = 'o'
        else:
            chk = 'x'
        space = '  '
        print(chk + space + param[i][0] + space + param[i][1] + space + param[i][2] + space + 'test...' + ret)

def f(s, k):
    
    k = int(k)
    
    cnt = 0
    ss = s
    for i in range(len(ss)):
        if i == 0:
            continue

        if ss[i-1] == ss[i]:
            ss = ss[:i] + '\\' + ss[i+1:]
            cnt += 1
        
    cnt = cnt * k
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 1:
            cnt = cnt + (k//2)
    else:    
        cntchk1 = 0
        cntchk2 = 0
        if s[0] == s[len(s)-1]:
            for i in range(len(s)):
                if s[len(s)-1] == s[len(s)-1 - i]:
                    cntchk1 += 1
#                 else:
#                     break
                if i < len(s)-1 and s[0] == s[i+1]:
                    cntchk2 += 1
                    
            if cntchk1 % 2 == 0 and cntchk1 % 2 == 0:
                cnt = cnt + (k -1)
                
    return str(cnt)

## Submit
print(f(input(),input()))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k);x=y=0
g=[len(list(v))for _,v in groupby(s)]
for c in g:x+=c//2
if(s[0]==s[-2])*g[0]%2&g[-2]%2:y=k//2if-len(s)==~g[0]else k-1
print(x*k+y)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
if len(set(S)) == 1:
    print(len(S) * K // 2)
    exit()
def get_kyotsu(st):
    kyotsu = []
    tmp = 1
    for s1, s2 in zip(st[:-1], st[1:]):
        if s1 == s2:
            tmp += 1
        if s1 != s2 and tmp != 1:
            kyotsu.append(tmp)
            tmp = 1
    if tmp != 1:
        kyotsu.append(tmp)
    res = 0
    for k in kyotsu:
        res += k//2
    return res

sento = S[0]
for s1, s2 in zip(S[:-1], S[1:]):
    if s1 == s2:
        sento+=s2
    if s1 != s2:
        break
matsubi = S[-1]
for s1, s2 in zip(S[::-1][:-1], S[::-1][1:]):
    if s1 == s2:
        matsubi+=s2
    if s1 != s2:
        break

conn = matsubi + sento 

print(K * get_kyotsu(S) - (get_kyotsu(sento) + get_kyotsu(matsubi) - get_kyotsu(conn)) *(K-1))

Submission status: Accepted

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

    def f(s):
        prev = "A"
        cnt = 0
        for e in s:
            if e == prev:
                cnt += 1
                prev = "A"
            else:
                prev = e

        return cnt

    li = [f(s*i) for i in range(6)]

    if k < 6:
        ans = li[k]

    else:
        idx = 5 - (k + 1) % 2
        d = li[idx] - li[idx-2]
        ans = li[idx] + d * (k - idx) // 2
        #d = li[5] - li[4]
        #ans = li[5] + d * (k - 5)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def olcnt(s):
    i = 0
    lst = []
    while i < len(s):
        j = 1
        while i + j < len(s) and s[i] == s[i + j]:
            j += 1
        lst.append((s[i], j))
        i += j
    return lst

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

if len(set(s)) == 1:
    print(int(len(s)*k/2))
else:
    cnt1 = sum([int(x[1]/2) for x in olcnt(s)])
    cnt2 = sum([int(x[1]/2) for x in olcnt(s+s)])

    print(cnt1 + (cnt2-cnt1)*(k-1))

Submission status: Accepted

Solution code:
def f(s, k):
    n = len(s)
    if len(set(s)) == 1:
        return n * k // 2
    ans = 0

    c = [1]
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            c[-1] += 1
        else:
            ans += (c[-1] // 2) * k
            if len(c) > 1:
                c.pop()
            c.append(1)
    ans += (c[-1] // 2) * k

    if s[0] == s[n - 1]:
        if c[0] % 2 == 1 and c[-1] % 2 == 1:
            ans += (k - 1)
    return ans

assert f("aba", 1) == 0
assert f("aba", 2) == 1 # aba_ba
assert f("aba", 4) == 3 # aba _ba _ba _ba
assert f("aaa", 4) == 6 # a_a _a_ a_a _a_
assert f("aaa", 3) == 4 # a_a_a_a_a
assert f("aaa", 5) == 7 # a_a_a_a_a_a_a_a
assert f("aa", 1) == 1 # a_
assert f("aa", 2) == 2 # a_a_
assert f("aa", 3) == 3 # a_a_a_
assert f("aa", 4) == 4 # a_a_a_a_
assert f("aaa", 1) == 1 # a_a
assert f("aaa", 2) == 3 # a_a_a_
assert f("aaaa", 1) == 2 # a_a_
assert f("aaaa", 2) == 4 # a_a_a_a_
assert f("aaaa", 3) == 6 # a_a_a_a_a_a_
assert f("qq", 81) == 81
assert f("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert f("issii", 2) == 4 # is_i_is_i_

s = input()
k = int(input())
ans = f(s, k)
print(ans)

Submission status: Accepted

Solution code:
s = input()

k = int(input())

n = len(s)

res = 0

i = 0
l = 1

R = []

while i<n :
    if i==n-1 :
        R.append(l)
        break
    if s[i]==s[i+1] :
        l = l+1
    else :
        R.append(l)
        l = 1
    i = i+1

m = len(R)
for i in range(m) :
    if i==0 :
        if s[0]==s[n-1] :
            res += R[i]//2
        else :
            res += R[0]//2*k;
    elif i==m-1:
        if s[0]==s[n-1] :
            res += (R[m-1]+R[0])//2*(k-1)
            res += R[m-1]//2
        else :
            res += R[m-1]//2*k
    else :
        res += R[i]//2*k
#for x in R :print(x,end=" ")

if len(R)==1 :
    print(n*k//2)
else:
    print(res)

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k,x=int(k),0
g=[len(list(v))for _,v in groupby(s)]
for c in g:x+=c//2
x*=k
if(s[0]==s[-2])*g[0]%2&g[-2]%2:x+=k//2if-len(s)==~g[0]else k-1
print(x)

Submission status: Accepted

Solution code:
import sys
s = input()
k = int(input())
if len(set(list(s))) == 1:
	print((len(s)*k)//2)
	sys.exit()
tmp = s[0]
cnt = 1
base = 0
is_first = True
for i in range(1, len(s)):
	if s[i] == tmp:
		cnt += 1
	else:
		if is_first:
			is_first = False
			first = cnt//2
		else:
			base += cnt//2
		tmp = s[i]
		cnt = 1

if s[0] != tmp:
	base += cnt//2
	print((first+base)*k)
else:
	i = 0
	last = cnt//2
	while s[i] == tmp:
		cnt += 1
		i += 1
	base += cnt//2
	#print(first, base, last)
	print(first + base*k - cnt//2 + last)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def main():
    s = input()
    k = int(input())
    print(solve(s, k))

def solve(s, k):
    ans = 0
    if len(set(s)) == 1:
        ans += replace_count(len(s)*k)
    else:
        same = 1
        for i in range(0, len(s)-1):
            if s[i] == s[i+1]:
                same += 1
            else:
                ans += replace_count(same) * k
                same = 1
        ans += replace_count(same) * k
        if s[0] == s[i]:
            a = 1
            b = 1
            a_idx = 0
            b_idx = -1
            while s[a_idx] == s[a_idx+1]:
                a += 1
                a_idx += 1
            while s[b_idx] == s[b_idx-1]:
                b += 1
                b_idx -= 1
            dup = (replace_count(a) + replace_count(b) - replace_count(a+b)) * (k-1)
            ans -= dup

    return ans


def replace_count(n):
    return n//2

main()

Submission status: Accepted

Solution code:
def count_difference(S):
    last = ''
    cnt = 0
    for s in S:
        if(s==last):
            cnt+=1
            last='0'
        else:
            last=s
    return cnt,last
#%%
S = input()
K = int(input())

cnt_diff = 0
last = ''
cnt_diff, last = count_difference(S)
if(len(S)==S.count(S[0])):
    print(len(S)*K//2)
elif(S[0]==S[-1] and last!='0'):
    cnt_diff2,_ = count_difference(S*2)
    if(cnt_diff2==cnt_diff*2):
        print(cnt_diff*K)
    else:
        print(cnt_diff+(cnt_diff+1)*(K-1))
else:
    print(cnt_diff*K)

Submission status: Accepted

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


K = int(K)
ans = 0
S = [c for c in S]

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

cnt = [1]
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt[-1] += 1
    else:
        cnt.append(1)

ans = 0
for i in cnt:
    ans += (i//2) * K

if S[0] == S[len(S)-1]:
    #頭と終わりが両方とも偶数、もしくは片側が奇数の時は上の計算ですでにダブり部分の処理は完了
    #片方だけ奇数の時は、繰り返しK一回につき1回の余分な余計な置換が必要
    #提出 #7882407参照
    if cnt[0]%2 == cnt[-1]%2 ==1:
        ans += K - 1
print(ans)

Submission status: Accepted

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

s_list = list(S)

modified = "mdf"

def solve():
    # その単語に1種類の文字しかなければ、合計の文字数を2で割捨てした数
    if len(set(s_list)) == 1:
        return (len(s_list)*K)//2
    
    cnt = 1
    cnt_list = []
    for i in range(len(s_list)-1):
        if s_list[i] == s_list[i+1]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    
    # 最後の対応
    # cnt = 1の時、最後は1文字だけ
    # cnt >= 2の時、最後は同じ文字が続いてる
    cnt_list.append(cnt)
    
    # 最初と最後が同じ時は最初と最後を足したやつを作る必要。
    head = cnt_list[0]
    tail = cnt_list[-1]
    body = cnt_list[1:-1]
    
    def body_change_cnt(body):
        cnt = 0
        for b in body:
            cnt += b//2
        return cnt
    
    if s_list[0] == s_list[-1]:
        head_tail = cnt_list[0] + cnt_list[-1]
        return head//2 + tail//2 + body_change_cnt(body)*K + (head_tail//2)*(K-1)
    else:
        return (head//2 + tail//2 + body_change_cnt(body)) * K

result = solve()

print(result)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

def main():
    s = input()
    k = int(input())
    n = len(s)
    # aajvurediaaa aajvurediaaa
    pri = 1
    suf = 0
    for i in range(n - 1):
        if s[i] != s[i + 1]:
            pri = i + 1
            break
    else:
        print(n * k // 2)
        exit()
    for i in range(n - 1, 0, -1):
        if s[i] != s[0]: break
        suf += 1
    pre = s[pri]
    mid = 0
    cnt = 1
    for i in range(pri + 1, n):
        if pre == s[i]:
            cnt += 1
        else:
            mid += cnt // 2
            cnt = 1
            pre = s[i]
    if s[0] != s[-1]:
        mid += cnt // 2
    # print(mid, pri, suf)
    print(mid * k + pri // 2 + suf // 2 + ((pri + suf) // 2) * (k - 1))

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10**9
MOD = 10**9+7

S, K = input(), int(input())
N = len(S)
if S[0] == S[-1]:
    i = 1
    while i < N and S[i-1] == S[i]:
        i += 1
    j = -1
    while j > -N and S[j] == S[j-1]:
        j -= 1
    if N+j <= i:
        print(N*K//2)
    else:
        ans = (K-1)*((i-j)//2)+i//2+(-j)//2
        S = S[i:j]
        l, r = 0, 0
        n = len(S)
        lst = []
        while r < n:
            while r+1 < n and S[r] == S[r+1]:
                r += 1
            r += 1
            lst.append(r-l)
            l = r
        for x in lst:
            ans += K*(x//2)
        print(ans)
else:
    l, r = 0, 0
    lst = []
    while r < N:
        while r+1 < N and S[r] == S[r+1]:
            r += 1
        r += 1
        lst.append(r-l)
        l = r
    ans = 0
    for x in lst:
        ans += K*(x//2)
    print(ans)

Submission status: Accepted

Solution code:
s=list(input())
K=int(input())
import copy
S=copy.deepcopy(s)
F=0
for i in range(1,len(S)):
    if S[i]!=S[i-1]:
        F=1
        break
if F==0:
    print((len(S)*K)//2)
elif K==1:
    ans=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans+=1
            S[i]="#"
    print(ans)
else:
    ans1=0
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            ans1+=1
            S[i]="#"
    ans2=0
    S2=s*2
    #print(S2)
    for i in range(1,len(S2)):
        if S2[i]==S2[i-1]:
            ans2+=1
            S2[i]="#"
    print(ans1+(ans2-ans1)*(K-1))
    #print(ans1,ans2)

Submission status: Accepted

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

pre_c = ''
flg = 0
count = 1

f_c = S[0]
f_e = S[-1]
t = []

for c in S:
  if c == pre_c:
    count += 1
  elif pre_c == '':
    pass
  else:
    t.append(count)
    count = 1
  pre_c = c

t.append(count)
count_1 = 0
for i in t:
  count_1 += i // 2
  
if f_c == f_e and len(t) != 1:
  t[0] = t[0] + t[-1]
  t.pop(-1)

count_2 = 0
for i in t:
  count_2 += i // 2
  
if len(S) == 1:
  print(K // 2)
elif len(t) == 1:
  print(K * len(S) // 2)
else:
  print(count_1 + count_2 * (K - 1))

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
N = len(S)

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

Submission status: Accepted

Solution code:
s = raw_input().strip()
k = input()

n = len(s)

ans = 0

if n == 1:
    print k / 2
    exit()

if n == 2:
    if s[0] != s[1]:
        print 0
    else:
        print k

    exit()

# case 1: s[0] = s[n - 1]
if s[0] == s[n - 1]:
    prev = n - 1

    suff = 0
    ridx = 0
    for i in xrange(n - 1, -1, -1):
        if s[i] == s[prev]: suff += 1
        else: break

        prev = i
        ridx = i

    if suff == n:
        print (n * k) // 2
        exit()

    pref = 0
    prev = 0
    lidx = 0
    for i in xrange(n):
        if s[i] == s[prev]: pref += 1
        else: break

        prev = i
        lidx = i

    left = pref
    mid = pref + suff
    right = suff

    ans = (left // 2) + (mid // 2) * (k - 1) + (right // 2)

    prev = lidx + 1
    sz = 0
    for i in xrange(lidx + 1, ridx):
        if s[i] == s[prev]: sz += 1
        else:
            ans += ((sz // 2) * k)
            sz = 1

        prev = i

    if (sz > 1):
        ans += ((sz // 2) * k)
        sz = 0

    print ans
    exit()
else:
    ans = 0
    sz = 0
    prev = 0
    for i in xrange(n):
        if s[i] == s[prev]:
            sz += 1
        else:
            ans += (sz // 2)
            sz = 1
            
        prev = i

    ans += (sz // 2)
    sz = 0

    ans *= k

    print ans
    exit()

Submission status: Accepted

Solution code:
from copy import deepcopy
s = list(input())
K = int(input())
dummy = "A"
S = deepcopy(s)
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    replace_cnt = 0
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            S[i] = dummy
            replace_cnt += 1
    if S[0] != S[-1]:
        print(replace_cnt*K)
    else:
        S = s+s
        replace_cnt_two = 0
        for i in range(1, len(S)):
            if S[i] == S[i-1]:
                S[i] = dummy
                replace_cnt_two += 1
        diff = replace_cnt_two - replace_cnt
        print(diff*(K-1)+replace_cnt)

Submission status: Accepted

Solution code:
S = input() + 'Z'
K = int(input())

n = 0
x = S[0]
T = []

for s in S:
    if s == x:
        n += 1
    else:
        x = s
        if n != 0:
            T.append(n)
            n = 1

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

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()
        K = int(input())

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

        k1 = self.connection_and_disconnection(S)
        k2 = self.connection_and_disconnection(S * 2)

        ans = k1 + (k2 - k1) * (K - 1)
        print(ans)

    def connection_and_disconnection(self, S: str):
        S = S + '$'
        cont_same_letter = 1
        need_to_change = 0
        for i in range(len(S) - 1):
            if S[i] == S[i + 1]:
                cont_same_letter += 1
            else:
                need_to_change += cont_same_letter // 2
                cont_same_letter = 1
        return need_to_change


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

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


lis = []
i = 0
cnt = 1

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

res = 0

if len(lis) == 1:
    res += (lis[0]*k)//2
else:
    if s[0] == s[len(s)-1]:
        for a in lis[1:-1]:
            res += ((a)//2)*k
        res += ((lis[0])//2)*1
        res += ((lis[-1])//2)*1
        res += ((lis[0]+lis[-1])//2)*(k-1)
    else:
        for a in lis:
            res += ((a)//2)*k

print(res)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

S = input()
K = int(input())
ele, num = get_sequence(S)
if len(ele) == 1:
    print(K*len(S)//2)
else:
    if ele[0]==ele[-1]:
        ans = num[0]//2 + num[-1]//2
        ans += sum(map(lambda x: x//2, num[1:-1]))*K
        ans += (num[0]+num[-1])//2 * (K-1)
    else:
        ans = sum(map(lambda x: x//2, num))*K
    print(ans)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
def get_sequence(arr):
    if len(arr) == 0:
        return [], []
    elements = []
    nums = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            elements.append(prev)
            nums.append(n)
            prev = c
            n = 1
    elements.append(prev)
    nums.append(n)
    return elements, nums

S = input()
K = int(input())
ele, num = get_sequence(S)
if len(ele) == 1:
    print(K*len(S)//2)
elif len(ele) == 2:
    print((num[0]//2 + num[1]//2)*K)
else:
    if ele[0]==ele[-1]:
        ans = num[0]//2 + num[-1]//2
        ans += sum(map(lambda x: x//2, num[1:-1]))*K
        ans += (num[0]+num[-1])//2 * (K-1)
    else:
        ans = sum(map(lambda x: x//2, num))*K
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def solve():
    ans=0;f=0
    s=input();n=len(s);k=int(input())
    if len(s)==1:
        return k//2
    for i in range(n-1):
        if s[i]!=s[i+1]:
            break
    else:return (n*k)//2
    count=1
    c1=0
    tmp=s[0]
    if s[-1]==tmp:
        j=1
        for j in range(n-1,1,-1):
            if s[j]!=tmp:
                break
            c1+=1
        n=j+1
        f=1

    for j in range(1,n):
        if s[j]!=tmp:
            break
        count+=1
    if f:
        ans+=(count+c1)//2*(k-1)
        ans+=c1//2
        ans+=count//2
    else:
        ans+=count//2*k
        
        
    i=j
    while i<n-1:
        count=1
        tmp=s[i]
        for j in range(i+1,n):
            if s[j]!=tmp:
                break
            count+=1
        i=j
        ans+=count//2*(k)
    return ans

print(solve())

Submission status: Accepted

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

from collections import Counter
l=Counter(s)
if len(l)==1:
    print(len(s)*k//2)
    exit()

f=1
ans=0
for i in range(len(s)-1):
    if f and s[i]==s[i+1]:
        ans+=1
        f=0
    else:
        f=1

if s[0]!=s[-1]:
    print(ans*k)
else:
    ans2=0
    ss=s+s
    f=1
    for i in range(2*len(s)-1):
        if f and ss[i]==ss[i+1]:
            ans2+=1
            f=0
        else:
            f=1
    print(ans+(ans2-ans)*(k-1))

Submission status: Accepted

Solution code:
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 += int(a[i]/2)
    print(int(c*k))
elif d == 1:
    print(int(a[0]*k/2))
else:
    for i in range(1,d-1):
        c += int(a[i]/2)
    c=c*k
    c += (k-1)*int((a[0]+a[d-1])/2) + int(a[0]/2) + int(a[d-1]/2)
    print(int(c))

Submission status: Accepted

Solution code:
def opeCounter(S, K):
    T = S * K
    c = 0
    i = 1
    while i < len(T):
        if T[i-1] == T[i]:
            c += 1
            i += 1
        i += 1
    return c

def agc039a(S, K):
    s1 = opeCounter(S, 1)
    s2 = opeCounter(S, 2)
    s3 = opeCounter(S, 3)
    d1 = s2 - s1
    d2 = s3 - s2

    ans = 0
    if d1 == d2: ans = s1 + d1 * (K-1)
    else:
        if(K%2 != 0): ans = s1 + (d1+d2) * ((K+2-1)//2-1) ##(k/2):roundup
        else: ans = s2 + (d1+d2) * (K//2-1)
    return ans

S = input()
K = int(input())
print(agc039a(S, K))

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
r = len(s) * k // 2
a = [len(''.join(g)) & 1 for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Submission status: Accepted

Solution code:
from itertools import groupby
from functools import reduce
from operator import xor

s = input()
k = int(input())
r = len(s) * k // 2
a = [reduce(xor, (1 for _ in g)) for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
r = len(s) * k // 2
a = [len(list(g)) & 1 for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Submission status: Accepted

Solution code:
S = input()
N = int(input())
T = [S*i + "#" for i in range(1,5)]
F = [0]*4
for i in range(len(T)) :
    t = T[i]
    acc = 0
    c = 1
    for j in range(1,len(t)) :
        if t[j] == t[j-1] :
            c += 1
        else :
            acc += c//2
            c = 1
    F[i] = acc

if N <= 4 :
    ans = F[N-1]
elif N % 2 == 1 :
    d = F[2] - F[0]
    ans = F[2] + d * ((N-3)//2)
else :
    d = F[3] - F[1]
    ans = F[3] + d * ((N-4)//2)

print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
if len(set(s)) == 1:
    print(k*n//2)
else:
    ans1 = 0
    i = 0
    while i+1<n:
        if s[i%n]==s[(i+1)%n]:
            i+=1
            ans1+=1
        i+=1
    ans2 = 0
    i = 0
    while i+1<2*n:
        if s[i%n]==s[(i+1)%n]:
           i+=1
           ans2+=1
        i+=1
    print(ans1+(ans2-ans1)*(k-1))

Submission status: Accepted

Solution code:
def main():

    S = input()
    K = int(input())
    count = []

    ct = 1
    for i in range(1, len(S)):
        if(S[i] == S[i-1]): ct += 1
        else:
            count.append(ct)
            ct = 1
    count.append(ct)

    v = 0
    for c in count:
        v += c // 2

    if(len(count) == 1):
        return K*len(S) // 2
    elif S[0] == S[-1]:
        return K*v + (K-1)*((count[0]+count[-1])//2 - count[0]//2 - count[-1]//2)
    else:
        return K*v

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

Submission status: Accepted

Solution code:
S=list(input())
K=int(input())
S2 = S*2
tmp=0
tmp2=0

if all([S[i] == S[0] for i in range(1,len(S))]):
    print((len(S)*K)//2)
    exit()
for i in range(1,len(S)):
    if S[i-1]==S[i]:
        S[i]="0"
        tmp += 1

for i in range(1,len(S2)):
    if S2[i-1]==S2[i]:
        S2[i]="0"
        tmp2 += 1

print(tmp+(tmp2-tmp)*(K-1))

Submission status: Accepted

Solution code:
import re

def main(s):
    #文字列sに同じ文字がn個以上連続している部分文字列を見つける
    n = 2
    # カンマを取ると n 個ちょうどになる
    reg = re.compile("(.)\\1{%d,}" %(n-1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]


if __name__ == "__main__":
    s = input()
    K = int(input())

    # 1文字の場合
    if len(s) == 1:
        print(K // 2)
        exit()

    t = list(main(s))

    # 全ての文字が同じ場合
    if len(t) == 1 and len(t[0]) == len(s):
        print((len(s) * K) // 2)
        exit()

    # 先頭と末尾が同じ場合
    ans1 = 0
    if len(s) >= 3 and s[0] == s[-1]:
        if len(t) >= 2 and t[0][0] == s[0] and t[-1][0] == s[-1]:
            ans1 = len(t[0]) // 2 + len(t[-1]) // 2 - ((len(t[0]) + len(t[-1])) // 2)
            ans1 *= K-1
        
        elif len(t) >= 1 and t[0][0] != s[0]:
            ans1 = len(t[-1]) // 2 - ((1 + len(t[-1])) // 2)
            ans1 *= K-1
        
        elif len(t) >= 1 and t[-1][0] != s[-1]:
            ans1 = len(t[0]) // 2 - ((len(t[0] + 1)) // 2)
            ans1 *= K-1
        
        else:
            ans1 = -1 * (K-1)

    # 先頭と末尾が異なる場合
    ans2 = 0
    for i in t:
        ans2 += len(i) // 2
    ans2 *= K

    ans = -ans1 + ans2
    print(ans)

Submission status: Accepted

Solution code:
def calc(S, T):
    U = [(S[0], 1)]
    un = 0
    for i in range(1, len(S)):
        if U[un][0] == S[i]:
            U[un] = (U[un][0], U[un][1] + 1)
        else:
            U += [(S[i], 1)]
            un += 1
    if un == 0:
        return (U[0][1] * T) // 2
    else:
        if U[0][0] != U[un][0]:
            return sum([u[1] // 2 for u in U]) * T
        else:
            return U[0][1] // 2 + U[un][1] // 2 \
                + (U[0][1] + U[un][1]) // 2 * (T - 1) \
                + sum([u[1] // 2 for u in U[1:un]]) * T


S = input()
T = int(input())
print(calc(S, T))

Submission status: Accepted

Solution code:
a = [input() for i in range(2)]

text = a[0]
iter = int(a[1])

text_list = list(text)
ren = []
start = text_list[0]
a = 0
for i in range(1,len(text_list)):
  if text_list[i] == start:
    a += 1
    if i == len(text_list)-1:
      ren.append(a)
  else:
    start = text_list[i]
    ren.append(a)
    a = 0

for i in range(len(ren)):
  ren[i] += 1

if ren == []:
  ren = [1]
  
ans = 0
if len(ren) == 1:
  ans = ren[0] * iter // 2
elif text_list[0] == text_list[-1]:
  if ren[0] % 2 == 1 and ren[-1] % 2 == 1:
    for r in ren:
      ans += (r//2)
    ans *= iter
    ans += iter-1
  else:
    for r in ren:
      ans += (r//2)
    ans *= iter    
else:
  for r in ren:
    ans += (r//2)
  ans *= iter

print(ans)

Submission status: Accepted

Solution code:
import copy

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

def count(L):
    cnt = 0
    for i in range(len(L)-1):
        if L[i] == L[i+1]:
            L[i+1] = "1"
            cnt += 1
    return cnt

T = copy.copy(S)
T.insert(0, T[len(T)-1])
cnt_T = count(T)

if T[-1] != "1":
    ans = count(S) + (cnt_T * (K-1))
else:
    TT = S * 2
    cnt_TT = count(TT)
    if K % 2 == 0:
        ans = cnt_TT * (K // 2)
    else:
        ans = (cnt_TT * (K // 2)) + count(S)

print(ans)