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
from collections import Counter
def main():
    S = input()
    K = int( input())
    CS = Counter(S)
    if CS[ S[0]] == len(S):
        print( len(S)*K//2)
        return
    N = len(S)

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

    T = S[check:] + S[:check]
    ans = 0
    now = S[0]
    cnt = 1

    for t in S[1:check]:
        if now == t:
            cnt += 1
            continue
        ans += cnt//2
        cnt = 1
        now = t
    ans += cnt//2

    cnt = 1
    now = S[check]
    for t in S[check+1:]:
        if now == t:
            cnt += 1
            continue
        ans += cnt//2
        cnt = 1
        now = t
    ans += cnt//2
    
    cnt = 1
    now = T[0]
    cc = 0

    for t in T[1:]:
        if t == now:
            cnt += 1
            continue
        cc += cnt//2
        cnt = 1
        now = t
    cc += cnt //2
    ans += cc*(K-1)
    
    print(ans)
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import groupby
    s=input()
    k=int(input())
    t=groupby(s)
    S=[]
    for tup in t: S.append((tup[0],len(list(tup[1]))))
    if len(S)==1:
        print(len(s)*k//2)
        return
    if s[0]==s[-1]:
        p=0 # s自体で変更する数
        for a,b in S[1:-1]:
            p+=b//2
        q=(S[0][1]+S[-1][1])//2
        ans=p*k+q*(k-1)
        ans+=S[0][1]//2+S[-1][1]//2
        print(ans)
    else:
        p=0
        for a,b in S:
            p+=b//2
        ans=p*k
        print(ans)
resolve()

Submission status: Accepted

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

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

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

    R = []
    lastLetter = None
    cnt = 0

    for c in S:
        if lastLetter == c:
            cnt += 1
        else:
            lastLetter = c
            if cnt > 0:
                R.append(cnt)

            cnt = 1
    
    R.append(cnt)

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

    S = 1 if len(R) > 1 and S[0] == S[-1] and R[0]%2 == 1 and R[-1]%2 == 1 else 0

    x = sum(r//2 for r in R)*K + (K-1)*S

    print(x)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()
K = int(input())
pre_s = ''
cnt = 1
ans = 0
flag = False
for s in S:
    if s == pre_s:
        cnt += 1
    else:
        ans += cnt // 2
        pre_s = s
        cnt = 1
ans += cnt // 2

if S[0] == S[-1]:
    pre_s = S[0]
    cnt = 1
    for s in S[1:]:
        if s == pre_s:
            cnt += 1
        else:
            break
    end_cnt = 1
    for s in reversed(S[:-1]):
        if s == pre_s:
            end_cnt += 1
        else:
            break
    if (end_cnt + cnt) % 2 == 0 and end_cnt % 2 == 1:
        ans += 1
        flag = True
        
if len(set(S)) == 1 and len(S) % 2 == 1:
    print((len(S)//2)*(K//2+K%2) + (len(S)//2+1)*(K//2))
elif flag:
    print(ans*K-1)
else:
    print(ans*K)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def Run_length_encoding(S: str):
    """AAABBC -> [(A, 3), (B, 2), (C, 1)]
    """
    ans = []
    curr = S[0]
    counter = 0
    for c in S:
        if c == curr:
            counter += 1
        else:
            ans.append((curr, counter))
            curr = c
            counter = 1
    ans.append((curr, counter))
    return ans


def solve(S: str, K: int):

    rle = Run_length_encoding(S)
    fchr, fcnt = rle[0]
    lchr, lcnt = rle[-1]
    if len(rle) == 1:
        print(fcnt*K//2)
        return

    ans = 0
    for c, cnt in rle:
        ans += cnt//2
    ans *= K

    if K > 1 and fchr == lchr:
        ans -= K*(fcnt//2)
        ans -= K*(lcnt//2)
        ans += (K-1)*((fcnt+lcnt)//2)
        ans += fcnt//2
        ans += lcnt//2

    print(ans)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    K = int(next(tokens))  # type: int
    solve(S, K)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(S, K):
    ans = 0
    S = [c for c in S]
    if K < 4:
        S = S * K
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                ans += 1
                S[i+1] = '-'
        return ans
    
    N = len(S)
    S = S * (4 if K % 2 == 0 else 5)
    a = [0] * (4 if K % 2 == 0 else 5)
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            a[(i+1)//N] += 1
            S[i+1] = '-'

    o = math.ceil((K-2) / 2)
    e = (K-2) // 2
    return a[0] + a[-1] + a[1] * o + a[2] * e


def main():
    S = read_str()
    K = read_int()
    print(slv(S, K))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ls = len(S)

moreWriteNeeded = False

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

    i = 1
    while i < ls and S[i] == S[0]:
        i += 1

    if i == ls:
        print((ls * K) // 2)
        exit()

    if i % 2 == 1:
        i = 1
        while S[ls - i - 1] == S[0]:
            i += 1

        if i % 2 == 1:
            moreWriteNeeded = True

write = 0

last = "Z"
for c in S:
    if last == c:
        last = "Z"
        write += 1
    else:
        last = c

ans = K * write if not moreWriteNeeded else K * write + K - 1

print(ans)

Submission status: Accepted

Solution code:
#gcdは3.4.3だとfractionsにある
from fractions import gcd

from math import factorial as f

from math import ceil,floor,sqrt

from sys import exit

from copy import deepcopy
import numpy as np

import bisect
import re


ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))



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

    s = list(s)
    ss = deepcopy(s)

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



    sc = deepcopy(s)

    cnt = 0
    for i in range(1,n):
        if sc[i] == sc[i-1]:
            sc[i] = '0'
            cnt+=1
    
    if sc[n-1] == sc[n-2] or sc[0] == sc[n-1]:
        flag = True
    else:
        flag = False

    ans = cnt*k+(flag*(k-1))

    cnt = 1
    s[0] = '0'
    for i in range(1,n):
        if s[i] == s[i-1]:
            s[i] = '0'
            cnt+=1
    
    if s[n-1] == s[n-2] or s[0] == s[n-1]:
        flag = True
    else:
        flag = False

    ans2 = cnt*k+(flag*k)



    print(min(ans,ans2))





main()

Submission status: Accepted

Solution code:
import sys, math; readline = sys.stdin.readline; from collections import deque;

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

a = []

_c = None
_count = 0
for c in S:
    if c == _c:
        a[-1] += 1
    else:
        a.append(1)
    _c = c
total = 0

if len(a) >= 2:
    for x in a[1:-1]:
        total += x // 2 * K

    if S[0] == S[-1]:
        total += (a[0] + a[-1]) // 2 * (K - 1)
        total += a[0] // 2 + a[-1] // 2
    else:
        total += (a[0] // 2 + a[-1] // 2) * K
else:
    total = (a[0] * K) // 2

print(total)

Submission status: Accepted

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

Submission status: Accepted

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

num = 0
tmp = 1
flg = True
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        num += tmp // 2
        if flg:
            fst = tmp   # 最初
            flg = False
        tmp = 1
num += tmp // 2

if flg: # 全部同じ
    ans = (len(S) * K) // 2
elif S[0] == S[-1] and tmp % 2 and fst % 2:
    ans = num * K + K - 1
else:
    ans = num * K 

print(ans)

Submission status: Accepted

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

head_count = 0
tail_count = 0
cont_count = 0
if s[0] == s[-1]:
    i = 1
    while i < len(s) and s[i] == s[0]:
        i += 1
    if i == len(s):
        print((k * len(s)) // 2)
        exit(0)
    j = len(s) - 2
    while j >= 0 and s[-1] == s[j]:
        j -= 1
    head_count = i // 2
    tail_count = (len(s) - j - 1) // 2
    cont_count = (i + len(s) - j - 1) // 2
    s = s[i:(j + 1)]
char = ''
local_cnt = 0
count = 0
for c in s:
    if c == char:
        local_cnt += 1
    else:
        count += local_cnt // 2
        local_cnt = 1
        char = c
if local_cnt >= 2:
    count += local_cnt // 2

print(head_count + count * k + cont_count * (k - 1) + tail_count)

Submission status: Accepted

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

ans = 0
if len(split_s) == 1:
    print(len(s)*k//2)
elif s[0] != s[-1]:
    # 繋がらないのでそれぞれの変換回数を求める
    for i in range(len(split_s)):
        ans += split_s[i]//2
    print(ans*k)
else:
    # 端がつながるので分割
    for i in range(1, len(split_s)-1):
        ans += split_s[i]//2
    ans += (split_s[-1]+split_s[0])//2
    ans *= k-1
    for i in range(len(split_s)):
        ans += split_s[i]//2
    print(ans)

Submission status: Accepted

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

last_changed = False
x = 0
for a,b in zip(S,S[1:]):
    if a == b and not last_changed:
        x += 1
        last_changed = True
    else:
        last_changed = False

if last_changed or S[0] != S[-1]:
    print(x*K)
    exit()

last_changed = True
y = 1
for a,b in zip(S,S[1:]):
    if a == b and not last_changed:
        y += 1
        last_changed = True
    else:
        last_changed = False

if last_changed:
    m = K//2
    print(y*m + x*(m+K%2))
else:
    print(x + y*(K-1))

Submission status: Accepted

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

L = []
temp = S[0]
c = 1
for i in range(1, len(S)):
    if S[i] == temp:
        c += 1
    else:
        L.append(c)
        c = 1
        temp = S[i]
    if i == len(S)-1:
        L.append(c)
        
ans = 0

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

Submission status: Accepted

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

cnt = 1
ans = 0
cnt2 = 1
flag = True
for i in range(1, len(S)):
  if S[i] == S[i - 1]:
    cnt += 1
    if flag:
      cnt2 += 1
  else:
    ans += cnt // 2
    cnt = 1
    flag = False

ans += cnt // 2
ans *= K
if flag:
  ans = (cnt * K) // 2
elif S[0] == S[-1] and cnt % 2 == 1 and cnt2 % 2 == 1:
  ans += K - 1

print(ans)

Submission status: Accepted

Solution code:
import collections

s = list(input())
k = int(input())
n = len(s)
ans = 0
if k == 1:
    for i in range(n - 1):
        if s[i] == s[i + 1]:
            ans += 1
            s[i + 1] = -1
    print(ans)
else:
    if collections.Counter(s).most_common()[0][1] == n:
        print(n * k // 2)
    else:
        ans1 = 0
        sd = s[:]
        for i in range(n - 1):
            if sd[i] == sd[i + 1]:
                ans1 += 1
                sd[i + 1] = -1
        s += s
        for i in range(n * 2 - 1):
            if s[i] == s[i + 1]:
                ans += 1
                s[i + 1] = -1
        ans -= ans1
        print(ans * (k - 1) + ans1)

Submission status: Accepted

Solution code:

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

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

if S[0] != S[-1]:
    f = 0
    t = 0
    ans = 0
    for k in range(1,len(S)):
        if S[k-1] == S[k] and f == 0:
            f = 1
            t = 2
        elif S[k-1] == S[k] and f == 1:
            t += 1
        else:
            ans += t//2
            t = 0
            f = 0
    ans += t//2
    print(ans*K)
else:
    t = set([str(x) for x in S])
    if len(t) == 1:
        print((len(S)*K)//2)
    else:
        mae = 0
        ushiro = 0
        for k in range(len(S)):
            if S[k] == S[0]:
                mae += 1
            else:
                break
        for k in range(len(S)):
            if S[-k-1] == S[0]:
                ushiro += 1
            else:
                break
        f = 0
        t = 0
        ans = 0
        for k in range(mae,len(S)-ushiro):
            if S[k-1] == S[k] and f == 0:
                f = 1
                t = 2
            elif S[k-1] == S[k] and f == 1:
                t += 1
            else:
                ans += t//2
                t = 0
                f = 0
        ans += t//2
        print(ans*K + mae//2 + ushiro//2 + (K-1)*((mae+ushiro)//2))

Submission status: Accepted

Solution code:
def f(s):

    ar = list(s)
    
    result = 0

    for i in range(1, len(ar)):
        if ar[i] == ar[i - 1]:
            ar[i] = '.'
            result += 1
    
    return result


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

if s[0] != s[-1]:
    print(f(s) * k)
else:

    l, r = 0, len(s) - 1

    while l < len(s) and s[l] == s[0]:
        l += 1
    
    while r >= 0 and s[r] == s[-1]:
        r -= 1
    
    l, r = l - 1, r + 1

    if r == 0:
        print((len(s) * k) // 2)
    else:
        print((l + 1) // 2 + (len(s) - r) // 2 + f(s[l + 1 : r]) * k + ((l + 1 + len(s) - r) // 2) * (k - 1))

Submission status: Accepted

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

len_array = [0] * 100

s_old = S[0]
count = 0
first_flag = 1
first_count = 0
last_count = 0
for s in S:
    if s_old == s:
        count += 1
    else:
        if first_flag:
            first_count = count
            first_flag = 0
        len_array[count - 1] += 1
        count = 1
    s_old = s
last_count = count
len_array[count - 1] += 1
if count == len(S):
    print(count * K // 2)
    exit()

if S[0] != S[-1]:
    ans = 0
    for i, l in enumerate(len_array):
        ans += (i + 1) // 2 * l
    print(ans * K)
    exit()
else:
    len_array[first_count - 1] -= 1
    len_array[last_count - 1] -= 1
    ans = 0
    for i, l in enumerate(len_array):
        ans += (i + 1) // 2 * l
    ans = ans * K + ((first_count + last_count) // 2) * (
        K - 1) + first_count // 2 + last_count // 2
    print(ans)
    exit()

Submission status: Accepted

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

counts = []
prev = S[0]
count = 1
for c in S[1:]:
	if prev == c:
		count += 1
	else:
		counts.append(count)
		count = 1
	prev = c
if count > 0:
	counts.append(count)

total = 0
if len(counts) == 1:
	total += (counts[0] * K) // 2
elif K > 1 and S[0] == S[-1]:
	for count in counts[1:-1]:
		total += (count // 2) * K
	total += counts[0] // 2
	total += counts[-1] // 2
	total += ((counts[0] + counts[-1]) // 2) * (K - 1)
else:
	for count in counts:
		if count > 1:
			total += (count // 2) * K
print(total)

Submission status: Accepted

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

cont = []
n = 1
t = S[0]
for s in S[1:]:
    if s == t:
        n += 1
    else:
        cont.append(n)
        n = 1
    t = s
else:
    cont.append(n)

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

print(ans)

Submission status: Accepted

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

C = []
c = 0
s = S[0]
for i in range(len(S)):
    if S[i] == s:
        c += 1
    else:
        C.append(c)
        c = 1
        s = S[i]
C.append(c)

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    k = I()
    n = len(s)
    for i in range(n):
        if s[i] != s[0]:
            break
    else:
        print((k*n)>>1)
        return
    s += s
    t = s[i-1]
    for j in range(n)[::-1]:
        if s[j] != t:
            break
    s = s[i:j+1]
    ans = (i>>1)+((n-1-j)>>1)+((n-1-j+i)>>1)*(k-1)
    i = 0
    n = len(s)
    while i < n:
        t = s[i]
        l = 0
        while i < n and t == s[i]:
            i += 1
            l += 1
        ans += (l >> 1)*k
    print(ans)
    return

#B
def B():

    return

#C
def C():

    return

#D
def D():

    return

#E
def E():

    return

#F
def F():

    return

#Solve
if __name__ == "__main__":
    A()

Submission status: Accepted

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

ans = [0] * 3
for i in (0, 1, 2):
    # print("#", ans)
    ans_tmp = 0
    S_tmp = S * (i + 1)
    cnt = 0
    for j, Sj in enumerate(S_tmp):
        if j == 0:
            cnt = 1
        else:
            if Sj == Sj_prev:
                cnt += 1
            else:
                ans_tmp += cnt // 2
                cnt = 1
        #
        Sj_prev = Sj
    else:
        ans_tmp += cnt // 2
    #
    ans[i] = ans_tmp

print(ans[0] + (K // 2) * (ans[1] - ans[0]) + ((K - 1) // 2) * (ans[2] - ans[1]))

Submission status: Accepted

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


def bf(s):
    last = None
    count = 0
    ans = 0
    for c in s:
        if c != last:
            ans += count // 2
            last = c
            count = 1
        else:
            count += 1

    return ans + count // 2


if k < 10:
    print(bf(s * k))
else:
    if len(set(list(s))) == 1:
        print(len(s) * k // 2)
    else:
        delta = bf(s * 3) - bf(s * 2)
        print(bf(s * 2) + delta * (k - 2))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

s = input().rstrip()
n = int(input())

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

s += "#"

k = 0
seq = []
for i, ch in enumerate(s):
    if i == 0:
        prev = ch
        k = 1
        continue
    if prev == ch:
        k += 1
    else:
        seq.append(k)
        k = 1
        prev = ch
    
if len(seq) == 1:
    ans = (seq[0] * n) // 2
    print(ans)
    exit()

if s[0] != s[-2]:
    ans = 0
    for item in seq:
        ans += item // 2
    print(ans * n)
else:
    mid = 0
    for item in seq[1:-1]:
        mid += item // 2
    ans = mid * n + seq[0] // 2 + seq[-1] // 2
    ans += ((seq[0] + seq[-1]) // 2) * (n - 1)
    print(ans)

Submission status: Accepted

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

def cnt(s):
    v = s[0]
    ans = 0
    c = 0
    for ss in s:
        if ss == v:
            c += 1
        else:
            ans += c // 2
            c = 1
            v = ss
    ans += c // 2
    return ans

if s[0] != s[-1]:
    c = cnt(s) * k
    print(c)
else:
    if len(set(s)) == 1:
        c = (len(s) * k) //2
        print(c)
    else:
        i_nxt = 0
        for i in range(len(s)):
            if s[i] != s[0]:
                i_nxt = i
                break
        s_new = s[i_nxt:] + s[0:i_nxt]
        c = cnt(s_new) * (k-1) + cnt(s[0:i_nxt]) + cnt(s[i_nxt:])
        print(c)

Submission status: Accepted

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

nums = []
num = 1
SPrev = Ss[0]
for S in Ss[1:]+'$':
    if S == SPrev:
        num += 1
    else:
        nums.append(num)
        num = 1
    SPrev = S
#print(nums)

if len(nums) == 1:
    ans = nums[0]*K//2
elif Ss[0] != Ss[-1]:
    ans = 0
    for num in nums:
        ans += num//2
    ans *= K
else:
    ans = 0
    for num in nums[1:-1]:
        ans += num//2
    ans *= K
    ans += nums[0]//2 + nums[-1]//2
    ans += (nums[0]+nums[-1])//2 * (K-1)

print(ans)

Submission status: Accepted

Solution code:

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

prev = S[0]
count_list = []
count=1
for i in range(1,N):
    if prev==S[i]:
        count+=1
    else:
        count_list.append(count)
        prev=S[i]
        count=1
count_list.append(count)

if len(count_list)==1:
    print((count_list[0]*K)//2)
else:
    if S[0]==S[N-1]:
        ans = 0
        for count in count_list[1:-1]:
            ans+=count//2
        ans *= K
        ans += count_list[0]//2
        ans += count_list[-1]//2
        ans += (K-1) * ((count_list[0]+count_list[-1])//2)
        print(ans)
    else:
        ans = 0
        for count in count_list:
            ans+=count//2
        ans *= K
        print(ans)

Submission status: Accepted

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

S = S + S + S
i = 1
count = 0
while i < len(S):
  if S[i] == S[i-1]:
    count += 1
    i += 2
    continue
  i += 1

if count % 3 == 0:
  print(count // 3 * K)
elif count % 3 == 1:
  print(count // 3 * K + K // 2)
elif count % 3 == 2:
  print((count+1) // 3 * K - 1)

Submission status: Accepted

Solution code:
import sys

S = input()

K = int(input())

now = 1
ans = 0

same = False
dic = {}
for i in S:
    if i not in dic:
        dic[i] = 1

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

for i in range(len(S)-1):

    if S[i+1] == S[i]:
        now += 1

    else:

        ans += now // 2
        now = 1

ans += now // 2



S2 = S + S

now = 1
ans2 = 0
for i in range(len(S2)-1):

    if S2[i+1] == S2[i]:
        now += 1

    else:

        ans2 += now // 2
        now = 1



ans2 += now // 2
sub = ans2 - ans

#print (ans,ans2)


print (ans + sub * (K-1))

Submission status: Accepted

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

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n,m = [int(i) for i in readline().split()]

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

l = len(s)

s0 = s[0]

if all(i==s0 for i in s):
    print(l*k//2)    
else:
    same = (s0 == s[-1])


    now = "AA"
    res = 0
    c = 0
    c0 = 0
    first = 2
    s0 = s+"W"
    for i in s0:
        if now == i:
            c += 1
        else:
            res += c//2
            c = 1
            now = i

    if not same: print(res*k)
    else:
        s = s+s+"W"
        
        res1 = res

        res = 0
        c = 0
        c0 = 0
        first = 2
        for i in s:
            if now == i:
                c += 1
            else:
                res += c//2
                c = 1
                now = i

        res2 = res
        
        print(res1 + (k-1)*(res2-res1))

Submission status: Accepted

Solution code:
string = input()
K = int(input())
from collections import defaultdict
if len(set(string)) ==1:
    print(K*len(string)//2)
else:
    if(string[0]==string[-1]):
        ## 両端が同じ文字なら…
        start = string[0]
        startFlag = True
        cnt = defaultdict(int)
        length = 0
        
        for char in string:
            #print(char,start,length)
            if char == start:
                length +=1
                
            else:
                if startFlag:
                    startlen = length
                    startFlag = False
                cnt[length] +=1
                start = char
                length = 1
        else:
            ## 両端をカウントしない
            cnt[startlen] -= 1
            cnt[startlen +length] += 1
        print(sum([(key//2)*value*K for key,value in cnt.items()])-(startlen+length)//2+startlen//2+length//2)
    else:
        ## 両端が同じ文字なら…
        start = string[0]
        cnt = defaultdict(int)
        length = 0
        for char in string:
            #print(char,start,length)
            if char == start:
                length +=1
                
            else:
                cnt[length] +=1
                start = char
                length = 1
        else:
            ## 両端をカウントしない
            cnt[length] += 1
        print(sum([(key//2)*value*K for key,value in cnt.items()]))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
num = [1]
p = S[0]
for i in range(1, len(S)):
    if S[i] == p:
        num[-1] += 1
    else:
        num.append(1)
        p = S[i]
if len(num) == 1:
    print(len(S)*K // 2)
elif S[0] != S[-1]:
    print( sum( [num[i]//2 for i in range(len(num))] ) * K )
else:
    num_s = num[1:-1] + [num[0] + num[-1]]
    ans = sum( [num_s[i]//2 for i in range(len(num_s))] ) * (K-1)
    ans += sum( [num[i]//2 for i in range(len(num))] )
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

i=0
a=0

while i<len(S):
  if i==len(S)-1:
    #open end
    break
  if S[i]==S[i+1]:
    a += 1
    i += 2
  else:
    i += 1
else:
  print(a*K)
  exit()
  
i=-1
b=0

while i<len(S):
  if i==len(S)-1:
    #open end
    print(a+b*(K-1))
    exit()
  if S[i]==S[i+1]:
    b += 1
    i += 2
  else:
    i += 1
else:
  print(a*((K+1)//2) + b*((K-1)//2))
  exit()

Submission status: Accepted

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

flag = True
for i in range(len(s) - 1):
    if s[i] != s[i + 1]:
        flag = False
        break
if flag == True:
    print(len(s) * k // 2)
else:
    seq = []
    num = 1
    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            num += 1
        else:
            seq.append(num)
            num = 1
    seq.append(num)
    count = 0
    if s[len(s) - 1] == s[0]:
        for i in range(len(seq) - 2):
            count += ((seq[i + 1] // 2) * k)
        count += (((seq[0] + seq[len(seq) - 1]) // 2) * (k - 1))
        count += (seq[0] // 2)
        count += (seq[len(seq) - 1] // 2)
    else:
        for i in seq:
            count += (i // 2) * k
    print(count)

Submission status: Accepted

Solution code:
s=input()
sng=len(s)
k=int(input())
answer=0

if s[0]!=s[-1]:
    swit=0
    for i in range(sng-1):
        if s[i]==s[i+1]:
            if swit==0:
                answer+=1
                swit=2
            else:
                swit+=1
                if swit%2==0:
                    answer+=1
        else:
            swit=0
    answer*=k
    print(answer)
else:
    now=0
    while s[now]==s[0]:
        now+=1
        if now==sng:
            break
    
    if now==sng:
        print((sng*k)//2)
    
    else:
        now2=sng-1
        while s[now2]==s[0]:
            now2-=1
            if now2==sng:
                break
        
        kaeshi=s[now:now2+1]
        swit=0
        for i in range(len(kaeshi)-1):
            if kaeshi[i]==kaeshi[i+1]:
                if swit==0:
                    answer+=1
                    swit=2
                else:
                    swit+=1
                    if swit%2==0:
                        answer+=1
            else:
                swit=0
        answer*=k
        
        kaeshi2=now+sng-now2-1
        #print(answer)
        answer+=((kaeshi2//2)*(k-1))
        answer+=(now//2)
        answer+=(sng-now2-1)//2
        print(answer)

Submission status: Accepted

Solution code:
def rle(s):
    n = len(s)
    if n == 0:
        return []
    res = []
    prev = s[0]
    count = 1
    index = 0
    while index < n:
        index += 1
        if index < n:
            if s[index] == prev:
                count += 1
                continue
        res.append([prev, count])
        if index < n:
            prev = s[index]
            count = 1
    return res


s = input().rstrip()
k = int(input())
counts = rle(s)
if len(counts) == 1:
    res = (counts[0][1] * k) // 2
else:
    res = counts[0][1] // 2
    res += counts[-1][1] // 2
    for c, v in counts[1:-1]:
        res += v // 2 * k
    if counts[0][0] == counts[-1][0]:
        res += (counts[0][1] + counts[-1][1]) // 2 * (k - 1)
    else:
        res += counts[0][1] // 2 * (k - 1) + counts[-1][1] // 2 * (k - 1)
print(res)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
p = s[0]
loop = 0
tmp = 1
first_tmp = 0
for c in s[1:]:
    if c == p:
        tmp += 1
    else:
        loop += tmp // 2
        if first_tmp == 0:
            first_tmp = tmp
        tmp = 1
    p = c
loop += tmp // 2
ans = loop * k
if first_tmp == 0 and tmp % 2 == 1:
    ans += k // 2
elif s[0] == s[-1] and first_tmp % 2 == 1 and tmp % 2 == 1:
    ans += k - 1
print(ans)