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:
s = input()
k = int(input())
import math

if len(s) == 1:
    print(math.floor(k/2))
    exit()
if len(s) == 2:
    if s[0] == s[1]:
        print(k)
        exit()
    else:
        print(0)
        exit()
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()

ans=0
last=s[0]
length=1
change=0
for i in range(1,len(s)):
    if s[i] == last:
        length+=1
    else:
        change += math.floor(length/2)
        length=1
    last=s[i]

change += math.floor(length/2)
ans=change*k
if s[0] != s[-1]:
    print(ans)
    exit()

l_length=1
last=s[0]
for i in range(1,len(s)):
    if s[i] == last:
        l_length+=1
    else:
        break
r_length=1
last=s[-1]
for i in range(len(s)-2,-1,-1):
    if s[i] == last:
        r_length+=1
    else:
        break

if l_length%2==1 and r_length%2==1:
    print(ans+k-1)
    exit()

print(ans)

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = list(sr())
K = ir()

unit = 0
cur = ''
T = S.copy()
for i in range(len(T)):
    if T[i] == cur:
        T[i] = '_'
        unit += 1
    cur = T[i]

if len(set(S)) == 1:
    answer = len(S) * K // 2
elif S[0] != S[-1]:
    answer = unit * K
else:
    #Sの先頭と末尾が同じで間に他の文字
    T = S.copy()
    head = 1; tail = 1
    for i in range(1, len(T)):
        if T[i] == T[0]:
            head += 1
        else:
            break
    for i in range(-2, -len(T)-1, -1):
        if T[i] == T[-1]:
            tail += 1
        else:
            break
    
    answer = unit * K - (head//2 + tail//2 - (head+tail)//2) * (K-1)

print(answer)
# 24

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = list(sr())
K = ir()

def count(s):
    t = s.copy()
    cur = ''
    ret = 0
    for i in range(len(t)):
        if t[i] == cur:
            t[i] = '_'
            ret += 1
        cur = t[i]
    
    return ret

one = count(S)
two = count(S + S)
diff = count(S * 3) - count(S)
if K&1:
    answer = one + (K//2) * diff
else:
    answer = two + ((K//2)-1) * diff

print(answer)
# 24

Submission status: Accepted

Solution code:
S = input()
K = int(input())
check = 0
ans = 0
St = []
for s in S:
    if s != check:
        check = s
    else:
        check = 0
        ans += 1
ans *= K
if S[0] == S[-1]:
    st = 1
    en = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            st += 1
        else:
            break
    for i in range(len(S)-1):
        if S[-1*(i+1)] == S[-1*(i+2)]:
            en += 1
        else:
            break
    if st % 2 == 1 and en % 2 == 1:
        ans += K - 1
if len(S) == 1:
    ans = K // 2
if len(set(list(S))) == 1:
    ans = len(S) * K // 2
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  if len(s)%2==1 and k%2==1:
    print(len(s)//2 + len(s)*(k-1)//2 )
  else:
    print(len(s)*k//2)
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))

Submission status: Accepted

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

ans = 0
flag = False
before = ""
for i in range(len(s)):
    if s[i] == before:
        if flag:
            flag = False
        else:
            flag = True
            ans += 1
    else:
        flag = False
    before = s[i]

n = len(s)
if len(set(list(s))) == 1:
    print(len(s) * k // 2)
    exit()
elif s[0] == s[-1]:
    a = 1
    b = 1
    for i in range(len(s)):
        if s[i] == s[i+1]:
            a += 1
        else:
            break
    for i in range(len(s)):
        if s[n-1-i] == s[n-2-i]:
            b += 1
        else:
            break
    if a % 2 == 1 and b % 2 == 1:
        print(ans*k + (k-1))
    else:
        print(ans*k)
else:
    print(ans * k)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
l = []
cnt = 1
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
l.append(cnt)

if len(l) == 1:
    print(len(S) * K // 2)
else:
    if S[0] == S[-1]:
        ans = sum([x // 2 for x in l]) * K
        ans -= (l[0] // 2 + l[-1] // 2 - (l[0] + l[-1]) // 2) * (K - 1)
    else:
        ans = sum([x // 2 for x in l]) * K
    print(ans)

Submission status: Accepted

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

if len(set(s)) == 1:
    ans = len(s) * k //2
else:
    if s[0] != s[-1]:
        i = 0
        cnt = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt += 1
                i += 2
            else:
                i += 1
        ans = cnt * k
    else:
        i = 0
        cnt = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt += 1
                i += 2
            else:
                i += 1
        tmp = cnt * k

        cnt_s = 0
        i = 0
        while i < len(s) - 1:
            if s[i] == s[i+1]:
                cnt_s += 1
                i += 2
            else:
                i += 1
        
        s_2 = s * 2
        cnt_s_2 = 0
        i = 0
        while i < len(s_2) - 1:
            if s_2[i] == s_2[i+1]:
                cnt_s_2 += 1
                i += 2
            else:
                i += 1

        diff = cnt_s_2 - 2 * cnt_s
        ans = tmp + diff * (k-1)



print(ans)

Submission status: Accepted

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

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

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

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

    b = 0
    r_s = s[::-1]
    for i in range(len(s)-1):
        if r_s[0] == r_s[i]:
            b +=1
        else:
            break

    print(ans * k - (a // 2 + b // 2 - (a+b) // 2) * (k - 1))

Submission status: Accepted

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

i = 0
cnt = 0
while i + 1 < len(S):
    if S[i] == S[i + 1]:
        cnt += 1
        i += 2
    else:
        i += 1

if S[0] != S[-1]:
    print(cnt * K)
else:
    i = 1
    a = 1
    prev = S[0]
    while i < len(S):
        if prev != S[i]:
            break
        else:
            a += 1
        i += 1
    prev = S[-1]
    b = 1
    i = len(S) - 2
    while 0 <= i:
        if prev != S[i]:
            break
        else:
            b += 1
        i -= 1
    print(cnt * K - (K - 1) * (a // 2 + b // 2 - (a + b) // 2))

Submission status: Accepted

Solution code:
def main():
    def f(s):
        n = len(s)

        ret = 0
        i = 0
        while i < n - 1:
            if s[i] == s[i + 1]:
                ret += 1
                i += 2
                continue
            i += 1

        return ret

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

    if k == 1:
        print(f(s))
        return

    if k <= 4:
        print(f(s * k))
        return

    d2 = f(s * 4) - f(s * 3)
    d1 = f(s * 3) - f(s * 2)

    q, r = divmod(k - 1, 2)
    ret = f(s * 1) + (d1 + d2) * q + d1 * r

    print(ret)

    # for x in range(1, 14):
    #     print(f(s * x), x)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
change = False

for i in range(1, len(s)):
    if s[i-1] == s[i] and not change:
        count += 1
        change = True
    else:
        change = False

count2 = 0
change2 = False
for i in range(2, len(s)):
    if s[i-1] == s[i] and not change2:
        count2 += 1
        change2 = True
    else:
        change2 = False


if len(s) == 1:
    print(k//2)
elif s[0] == s[-1] and not change:
    if change2:
        print((count+count2+1)*(k//2)+count*(k%2))
    else:
        print(count+(count2+1)*(k-1))
else:
    print(count*k)

Submission status: Accepted

Solution code:
from itertools import groupby

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

N = len(S)

if len(set(list(S))) == 1:
    print(N * K // 2)
else:
    G = list(map(lambda g: len(list(g[1])), (groupby(S))))
    cnt = sum(map(lambda g: g // 2, G)) * K

    if S[0] != S[N - 1]:
        print(cnt)
    else:
        a = G[0]
        b = G[-1]
        print(cnt - (a // 2 + b // 2 - (a + b) // 2) * (K - 1))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    s = S()
    k = I()
    l = len(s)
    a = []
    t = s[0]
    c = 1
    for u in s[1:]:
        if t == u:
            c += 1
        else:
            a.append(c)
            t = u
            c = 1
    if len(a) == 0:
        return l * k // 2
    r = 0
    if s[0] == s[-1]:
        for b in a:
            r += b // 2
        r += c // 2
        a[0] += c
        k -= 1
    else:
        a.append(c)
    o = 0
    for b in a:
        o += b // 2

    return r + o * k


print(main())

Submission status: Accepted

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

def solve(s):
	tmp = ""
	streak = 1
	cnt = 0
	for i in range(len(s)):
		if s[i] == tmp:
			streak += 1
		else:
			cnt += streak // 2
			tmp = s[i]
			streak = 1
	cnt += streak // 2
	return cnt
	

if len(list(set(list(map(str,s))))) == 1:
	ans = len(s)*k//2
elif s[0] == s[-1]:
	key_c = s[0]
	pre_ind = 0
	epi_ind = 0

	for i in range(len(s)):
		if s[i] != key_c:
			pre_ind = i-1
			break
	for i in range(len(s)):
		if s[-(i+1)] != key_c:
			epi_ind = len(s)-i
			break

	cnt_mid = solve(s[pre_ind+1:epi_ind])
	cnt_left = (pre_ind+1)//2
	cnt_right = (len(s)-epi_ind)//2
	cnt_conection = (pre_ind+1+len(s)-epi_ind)//2

	ans = cnt_left + cnt_mid*k + cnt_conection*(k-1) + cnt_right
	

elif s[0] != s[-1]:
	cnt = solve(s)
	ans = cnt * k

print(ans)

Submission status: Accepted

Solution code:
def is_all(t):
    for i in range(len(t)-1):
        if t[i]!=t[i+1]:
            return False
    return True

s=input()
n=len(s)
k=int(input())
cor=[1]
ans=0
c=1
if is_all(s):
    print(n*k//2)
    exit()
i=0
while i<n-1:
    if s[i]==s[i+1]:
        cor[0]+=1
        i+=1
    else:
        i+=1
        break
while i<n-1:
    if s[i]==s[i+1]:
        cor.append(1)
        while i<n-1:
            if s[i]==s[i+1]:
                cor[-1]+=1
                i+=1
            else:
                break
    i+=1
#print(cor)
if s[0]==s[-1]:
    for i in range(1,len(cor)-1):
        ans+=cor[i]//2
    ans*=k
    ans+=(cor[0]+cor[-1])//2*(k-1)
    print(ans+cor[0]//2+cor[-1]//2)
else:
    for i in range(len(cor)):
        ans+=cor[i]//2
    print(ans*k)

Submission status: Accepted

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

if len(set(S)) == 1:
    print(len(S)*k//2)
else:
    ans = 0
    cnt = 1
    for i in range(1, len(S)):
        if S[i] == S[i-1]:
            cnt += 1
        else:
            ans += cnt//2
            cnt = 1
    if cnt > 1:
        ans += cnt//2
    ans = ans * k
    if S[0] == S[-1]:
        f, b = 0, 0
        for s in S:
            if s == S[0]:
                f += 1
            else:
                break
        for s in S[::-1]:
            if s == S[0]:
                b += 1
            else:
                break
        ans -= (f//2 + b//2 - (f+b)//2) * (k-1)
    print(ans)

Submission status: Accepted

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

#
#  文字数一種類
#
if len(set(s)) == 1:
    print((len(s) * k) // 2)
    exit()

#
# 先頭と末尾が異なる
#
if s[0] != s[-1]:
    cnt = 0
    bf_same = False
    tmp = 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            tmp += 1
        else:
            if tmp > 1:
                cnt += (tmp // 2) * k
                tmp = 1
    else:
        if tmp > 1:
            cnt += (tmp // 2) * k
        print(cnt)
    exit()

#
# 先頭と末尾が一緒
#

# 先頭のダブリ
tp = 1
tp_idx = -1
for i in range(len(s) - 1):
    if s[i] == s[i + 1]:
        tp += 1
        tp_idx = i + 1
    else:
        break

# 末尾のダブリ
bt = 1
bt_idx = -1
for i in range(len(s) - 1, 0, -1):
    if s[i] == s[i - 1]:
        bt += 1
        bt_idx = i - 1
    else:
        break

# 連結部分以外のダブリ
cnt = 0
bf_same = False
tmp = 1
for i in range(tp_idx + 1, bt_idx - 1):
    if s[i] == s[i + 1]:
        tmp += 1
    else:
        if tmp > 1:
            cnt += (tmp // 2) * k
            tmp = 1
else:
    if tmp > 1:
        cnt += (tmp // 2) * k

#print(tp, bt, cnt)
#print(tp // 2 + bt // 2 + ((tp + bt) // 2) * (k - 1))
ans = cnt + tp // 2 + bt // 2 + ((tp + bt) // 2) * (k - 1)
print(ans)

Submission status: Accepted

Solution code:
s = input()
n = len(s)
flg = 0
ans1 = 0
ans2 = 0
for i in range(1,n):
  if s[i] == s[i-1] and flg == 0:
    flg = 1
    ans1 += 1
  else:
    flg = 0
s = s*2
n *= 2
flg = 0
for i in range(1,n):
  if s[i] == s[i-1] and flg == 0:
    flg = 1
    ans2 += 1
  else:
    flg = 0
k = int(input())
if len(set(list(s))) == 1:
  print(n*k//4)
elif ans2>ans1*2:
  print((k//2)*ans2+(k%2)*ans1+(k-1)//2)
else:
  print((k//2)*ans2+(k%2)*ans1)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import numpy as np
import collections

##  input functions for me
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def rip(sep = ''):
    if sep == '' :
        return map(int, input().split()) 
    else: return map(int, input().split(sep))
def ria(sep = ''): 
    return list(rip(sep))
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()
##
def main():
    S = rs()
    K = ri()
    N = len(S)
    key = []
    cnt = []
    key.append(S[0])
    cnt.append(1)
    for i in range(1,N):
        if S[i] == key[len(key) - 1]:
            cnt[len(cnt) - 1] += 1
        else:
            key.append(S[i])
            cnt.append(1)

    if len(cnt) == 1:
        tot = N * K
        print(tot // 2)
        sys.exit(0)

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






if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

count = 0
flag = True
for i in range(1, len(s)):
    if not flag:
        flag = True
        continue
    if s[i] == s[i-1]:
        count += 1
        flag = False

count2 = 0
flag = True
s = s*2
for i in range(1, len(s)):
    if not flag:
        flag = True
        continue
    if s[i] == s[i-1]:
        count2 += 1
        flag = False
# 連結部分の数を加える
print(count * k + (count2-count*2)*(k-1))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
n=len(s)
cnt=1
ans=0
l=[]
for i in range(n-1):
  if s[i]==s[i+1]:
    cnt+=1
  else:
    l.append(cnt)
    cnt=1
l.append(cnt)
if len(l)==1:
  print(k*sum(l)//2)
else:
  if s[0]!=s[n-1]:
    for i in range(len(l)):
      ans+=k*(l[i]//2)
  else:
    for i in range(1,len(l)-1):
      ans+=k*(l[i]//2)
    ans+=(k-1)*((l[0]+l[len(l)-1])//2)
    ans+=l[0]//2+l[len(l)-1]//2
  print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

def count(s):
    l = []
    c = 0
    for key,value in groupby(s):
        l.append(list(value))
    for i in l:
        c += len(i)//2
    return c
s = list(input())
k =int(input())
if k <= 10**5:
    print(count(s*k))
else:
    d2 = count(s*3)-count(s*2)
    d3 = count(s*4)-count(s*3)
    if d2 != d3:
        if k%2 == 1:
            print(d2*((k-2)//2+1) + count(s*2) + d3*((k-2)//2))
        else:
            print(d2*((k-2)//2) + count(s*2) + d3((k-2)//2))
    else:
        print(d2*(k-2) + count(s*2))

Submission status: Accepted

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

sames = 1
ans = 0
prev = 'prev'

# 文字列1回分の重複削除の操作回数
for s in S:
	if prev == s:
		sames+=1
	else:
		if sames > 1:
			ans+=(sames//2)
			sames = 1
	prev = s
ans+=(sames//2)

# 先頭と末尾の連続回数をカウント
add_f = 0
add_b = 0
if S[0] == S[len(S)-1]:
	for s in S:
		if s == S[0]:
			add_f+=1
		else:
			break
	for sr in reversed(S):
		if sr == S[len(S)-1]:
			add_b+=1
		else:
			break
add = add_f+add_b

# すべて同じ文字の時とそうじゃない時を場合分け
if len(set(list(S))) == 1:
	print(K*len(S)//2)
else:
	print(ans*K + (add//2)*(K-1) - (add_f//2)*(K-1) - (add_b//2)*(K-1))

Submission status: Accepted

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

sames = 1
ans = 0
prev = 'prev'

for s in S:
	if prev == s:
		sames+=1
	else:
		if sames > 1:
			ans+=(sames//2)
			sames = 1
	prev = s
ans+=(sames//2)

add_f = 0
add_b = 0
if S[0] == S[len(S)-1]:
	for s in S:
		if s == S[0]:
			add_f+=1
		else:
			break
	for sr in reversed(S):
		if sr == S[len(S)-1]:
			add_b+=1
		else:
			break
add = add_f+add_b

if len(set(list(S))) == 1:
	print(K*len(S)//2)
else:
	print(ans*K + (add//2)*(K-1) - (add_f//2)*(K-1) - (add_b//2)*(K-1))

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
l=[]
p=1
for i in range(len(s)-1):

    if s[i]==s[i+1]:
        p+=1
    else:
        l.append(p)
        p=1
l.append(p)
m=0
if len(l)==1:
    print((l[0]*k)//2)

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

    else:
        for i in range(len(l)):
            m+=k*(l[i]//2)
        print(m)

Submission status: Accepted

Solution code:
s, k = input(), int(input())
r = 0
f = True
fir = 0
las = 0

for i in range(len(s)):
    if s[i] == s[0]:
        fir += 1
    else:
        break

for i in range(len(s) - 1, -1, -1):
    if s[i] == s[0]:
        las += 1
    else:
        break

i = 1
tmp = 1
while i < len(s):
    if s[i] == s[i - 1]:
        tmp += 1
    else:
        f = False
        r += tmp // 2
        tmp = 1
    i += 1
r += tmp // 2

if f:
    print(len(s) * k // 2)
else:
    print(r * k - (k - 1) * (fir // 2 + las // 2 - (fir + las) // 2))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
if len(s)==1:
	print(k//2)
	exit()
cnt=0
same_length=1
first=1
for i in range(len(s)-1):
 		 if s[i]==s[i+1]:
 		 	same_length+=1
 		 	if cnt==0:first+=1
 		 elif s[i]!=s[i+1]:
 		 	if cnt==0:s_left=first
 		 	cnt+=int(same_length//2)
 		 	same_length=1
if same_length==len(s):
	print(len(s)*k//2)
	exit()
s_right=same_length
cnt+=int(same_length//2)
if s[0]==s[-1] and s_left%2==1 and s_right%2==1:
	print(cnt*k+k-1)
else:
	print(cnt*k)

Submission status: Accepted

Solution code:
from itertools import groupby
S = input()
K = int(input())
A = list(map(lambda x: (x[0], len(list(x[1]))), groupby(S)))
a1 = A[0]
a2 = A[-1]
if len(A) == 1:
    print(len(S) * K // 2)
elif a1[0] != a2[0]:
    print(K * sum(x // 2 for _, x in A))
else:
    print(K * sum(x // 2 for _, x in A) - (K - 1) * (a1[1] // 2 + a2[1] // 2 - (a1[1] + a2[1]) // 2))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

def merge(A, B):
    res = [[inf]*26 for _ in range(26)]
    for i in range(26):
        for j1 in range(26):
            for j2 in range(26):
                if j1 == j2:
                    continue
                for k in range(26):
                    res[i][k] = min(res[i][k], A[i][j1]+B[j2][k])
    return res
S = list(map(lambda x: ord(x) - 97, readline().strip()))
K = int(readline())
inf = 10**18+13
dp = [None]*26

for st in range(26):
    Dp = [inf]*26
    if st == S[0]:
        Dp[st] = 0
    else:
        Dp[st] = 1
    for s in S[1:]:
        Dp2 = [inf]*26
        for i in range(26):
            for j in range(26):
                if i == j:
                    continue
                if s != i:
                    Dp2[i] = min(Dp2[i], Dp[j] + 1)
                else:
                    Dp2[i] = min(Dp2[i], Dp[j])
        Dp = Dp2[:]
    dp[st] = Dp[:]

stack = [dp]

for _ in range(32):
    stack.append(merge(stack[-1], stack[-1]))

E = [[0]*26 for _ in range(26)]
for i in range(32):
    if (1<<i) & K:
        E = merge(E, stack[i])
print(min(min(d) for d in E))

Submission status: Accepted

Solution code:
S = list(input().rstrip("\n"))
K = int(input())

# 全ての文字が同じとき
if S.count(S[0]) == len(S):
    answer = (len(S) * K) // 2
    print(answer)
    exit()

from itertools import groupby

# 初項(K=1)を求める
init = 0
for _,group in groupby(S):
    tmp = sum(1 for _ in group)
    if tmp >= 2:
        init += tmp // 2

if K == 1:
    print(init)
    exit()

# 公差を求める
com_diff = 0
for _ ,group in groupby(S+S):
    tmp = sum(1 for _ in group)
    if tmp >= 2:
        com_diff += tmp // 2
com_diff -= init

print(init + (K-1)*com_diff)

Submission status: Accepted

Solution code:
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif len(list(set(a)))==1:
  print((len(a)*k)//2)
else:  
  tmp=a[0]
  count1=0
  count2=0
  l=0
  while a[l]==tmp:
    count1+=1
    l+=1
  j=-1
  while a[j]==tmp:
    count2+=1
    j-=1
    
  for i in range(len(a)-1):
    if a[i]==a[i+1]:
      a[i+1]='1'
      ans+=1
  if a[0]==a[-1]:
    if count1%2==0:
      print(ans*k)
    else:
      print(ans*k+k-1)
  else:
    print(ans*k)

Submission status: Accepted

Solution code:
# coding: utf-8
S = input()
K = int(input())
if len(list(set(S))) == 1:
    print(len(S) * K // 2)
    exit()
l = []
SS = S * 2
s = SS[0]
cnt = 1
for i in range(1, len(SS)):
    if s != SS[i]:
        l.append(cnt)
        s = SS[i]
        cnt = 1
    else:
        cnt += 1
l.append(cnt)
ans = 0
if S[0] == S[-1]:
    for i in range(len(l)//2 + 1):
        # if i == 0 or i == len(l)//2:
        #     ans += l[i] // 2
        if i == 0:
            ans += l[i] // 2
        elif i == len(l) // 2:
            ans += l[i] // 2 * (K-1)
        else:
            ans += l[i] // 2 * K
    ans += l[-1] // 2
    # ans -= (l[0] + l[-1]) // 2
else:
    # print(l[:len(l)//2])
    for i in range(len(l)//2):
        ans += l[i] // 2 * K
print(ans)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
l = []
cnt = 0
for i in range(len(s)-1):
  cnt += 1
  if s[i] != s[i+1]:
    l.append(cnt)
    cnt = 0
else:
  cnt += 1
  l.append(cnt)

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

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
cnt = 0
start = S[0]
for i in range(len(S)):
    if start != S[i]:
        break
    if i == len(S) - 1:
        print(len(S) * K // 2)
        exit()
ans = 0
now = start
for i in range(len(S)):
    if i == len(S) - 1:
        if now == S[i]:
            cnt += 1
        ans += cnt // 2
    else:
        if S[i] != now :
            ans += cnt // 2
            cnt = 1
            now = S[i]
        else:
            cnt += 1

end = S[-1]
start_cnt = 0
end_cnt = 0
for c in S:
    if c == start:
        start_cnt += 1
    else:
        break
for i in range(len(S) - 1 , - 1, -1):
    if S[i] == end:
        end_cnt += 1
    else:
        break
add = 0
if end_cnt % 2 == 1 and start_cnt % 2 == 1 and start == end:
    add = (K - 1)
print(ans * K + add)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = readline().rstrip()
K = int(read())

def solve_small(S,K):
    T = S * K
    prev = 0
    cnt = 0
    for x in T:
        if x != prev:
            prev = x
            continue
        else:
            prev = 0
            cnt += 1
    return cnt

A = [solve_small(S,k) for k in [0,1,2,3,4,5]]

if K <= 5:
    answer = A[K]
else:
    if K&1:
        d = A[5] - A[3]
        answer = A[5] + (K-5)//2 * d
    else:
        d = A[4] - A[2]
        answer = A[4] + (K-4)//2 * d

print(answer)

Submission status: Accepted

Solution code:
from itertools import groupby
S = input()
k = int(input())

seq = [(s, len(list(k))) for s, k in groupby(S)]

if len(seq) == 1:
    ans = (k * len(S)) // 2
else:
    if seq[0][0] != seq[-1][0]:
        ans = k*sum(a[1] // 2 for a in seq)
    else:
        left = seq[0][1]
        right = seq[-1][1]
        ans = k*sum(a[1] // 2 for a in seq[1:-1])
        ans += ((left + right) // 2) * (k - 1)
        ans += left // 2 + right // 2

print(ans)

Submission status: Accepted

Solution code:
def counter(s):
    res, cnt = 0, 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
        else:
            res += cnt // 2
            cnt = 1
    res += cnt // 2
    return res


def solve():
    S = list(input())
    K = int(input())
    
    N = len(S)
    if len(set(S)) == 1:
        print(N * K // 2)
        exit()
    
    a = counter(S)
    b = counter(S + S)
    print(a + (K - 1) * (b - a))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  if len(s)%2==1 and k%2==1:
    print(len(s)//2 + len(s)*(k-1)//2 )
  else:
    print(len(s)*k//2)
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))

Submission status: Accepted

Solution code:
from itertools import groupby
S = list(input())
K = int(input())
newS = list([list(g) for k, g in groupby(S)])
change = 0
for i in range(len(newS)):
    change += len(newS[i])//2
chain = 0
if len(newS) == 1:
    print(K*len(S)//2)
else:
    if len(newS) > 2 and newS[0] == newS[len(newS)-1]:
        chain = 1
    print(change*K+chain*(K-1))

Submission status: Accepted

Solution code:
import re
from collections import Counter
s = input()
k = int(input())
len_s = len(s)
res = re.findall(r"(.)(\1{0,})", s)
res = [i[0] + i[1] for i in res]

INF = 10**18
best = INF
ct = Counter(s)

if len(res) == 1:
    print(len_s * k // 2)
else:
    score = 0
    if res[0][0] == res[-1][0]:
        score += len(res[0] + res[-1]) // 2 * (k-1) + len(res[0]) // 2 + len(res[-1]) // 2
        res.pop(0)
        res.pop(-1)
    for r in res:
        score += k * (len(r) // 2)
    print(score)

Submission status: Accepted

Solution code:
def count(st):
    n=len(st)
    tmp=1
    res=[]
    for i in range(n-1):
        if st[i]==st[i+1]:
            tmp+=1
        else:
            res.append(tmp)
            tmp=1
    res.append(tmp)
    return res
S=input()
K=int(input())
T=S*3
N=len(S)
if N==1:
    print(K//2)
    exit()
if K<=2:
    Y=count(S*K)
    ans=0
    for i in Y:
        ans+=i//2
    print(ans)
    exit()
if S.count(S[0])==N:
    print((N*K)//2)
    exit()
X=[]
if S[0]==S[-1]:
    for j in range(1,N):
        if S[j-1]!=S[j]:
            break
    tmp=1
    for i in range(j,j+N-1):
        if T[i]==T[i+1]:
            tmp+=1
        else:
            X.append(tmp)
            tmp=1
    X.append(tmp)
else:
    X=count(S)

cnt=0
for i in X:
    cnt+=i//2
Y=count(T)
cnt2=0
for i in Y:
    cnt2+=i//2
print(cnt*(K-3)+cnt2)

Submission status: Accepted

Solution code:
def solve(s, k):
    n = len(s)
    s = list(s)
    if len(set(s)) == 1:
        return n*k//2
    a = 0
    for i in range(n):
        if s[0] == s[i]:
            a += 1
        else:
            break
    b = 0
    for i in range(n):
        if s[-1] == s[-1-i]:
            b += 1
        else:
            break
    c = 0
    for i in range(a, n-b-1):
        if s[i] == s[i+1]:
            c += 1
            s[i+1] = "."
    res = a//2 + b//2 + c*k
    if s[0] == s[-1]:
        res += ((a+b)//2) * (k-1)
    else:
        res += (a//2 + b//2) * (k-1)
    return res

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

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
s = input()
k = int(input())

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

dup = [1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    dup[-1]+=1
  else:
    dup.append(1)
    
ans = 0
for i in dup:
  ans += (i//2)*k

if s[0]==s[-1]:
  ans += ((dup[0]+dup[-1])//2 - (dup[0]//2+dup[-1]//2))*(k-1)

print(ans)

Submission status: Accepted

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

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

t=[1]
for i in range(1,len(s)):
  if s[i-1]==s[i]:
    t[-1]+=1
  else:
    t.append(1)
    
ans=0
for i in t:
  ans+=(i//2)*k

if s[0]==s[-1]:
  ans+= ((t[0]+t[-1])//2 - (t[0]//2+t[-1]//2))*(k-1)

print(ans)

Submission status: Accepted

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

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


def count(s):
    res = 0
    cnt = 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            cnt += 1
        else:
            res += cnt // 2
            cnt = 1
    res += cnt // 2
    return res


a = count(S)
b = count(S + S)
print(a + (K - 1) * (b - a))