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

def string_changer(letter):
  ans = 0
  count = 0
  content = letter[0]
  for i in range(1, len(letter)):
    if letter[i] == content:
      count += 1
    else:
      ans += (count+1) // 2
      count = 0
      content = letter[i]
  ans += (count+1) // 2
  return ans

all_same = True
first_letter = s[0]
for i in range(1, len(s)-1):
  if s[i] != s[0]:
    all_same = False
    break
  else:
    pass

if all_same == True:
  ans = len(s)*k // 2
elif s[0] == s[len(s)-1] and k > 1:
  connected_s = s+s
  connection = string_changer(connected_s)
  if connection == string_changer(s)*2:
    ans = string_changer(s) * k
  else:
    ans = string_changer(s)*k + k-1
else:
  ans = string_changer(s) * k
print(ans)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
ls = [str for str in S]
if len(set(ls))==1:
  cnt=len(S)*K//2
  print(cnt)
  exit()

if not ls[0]==ls[-1]:
  i=0
  cnt=0
  while i<len(S)-1:
    if ls[i+1]==ls[i]:
      ls[i+1]="0"
      cnt+=1
      i+=1
    else:
      i+=1
    ans=cnt*K
else:
  j=0
  atama=0
  while j<len(S):
    if ls[j+1]==ls[j]:
      j+=1
      atama+=1
    else:
      break
  k=0
  oshiri=0
  while k<len(S):
    if ls[-k-2]==ls[-k-1]:
      k+=1
      oshiri+=1
    else:
      break
  es=ls[atama+1:]+ls[:atama+1]
  i=0
  cnt=0
  while i<len(S)-1:
    if es[i+1]==es[i]:
      es[i+1]="0"
      cnt+=1
      i+=1
    else:
      i+=1
  ans=cnt*K
  if (atama+1)*(oshiri+1)%2==1:
    ans-=1
print(ans)

Submission status: Accepted

Solution code:
##################################################################

if __name__ == "__main__":
    Input = [input() for i in range(2)]
    # = list(map(int,Input[0].split()))
    S = str(Input[0])
    K = int(Input[1])

    n = len(S)
    ren = 1
    bef = S[0]
    cont = []
    if n == 1:
        cont = [1]
    for s in range(1,n):
        if bef == S[s]:
            ren += 1
            bef = S[s]
            if s == n-1:
                cont.append(ren)                
        else:
            cont.append(ren)
            ren = 1
            bef = S[s]

    ans = (sum([c//2 for c in cont]))*K
    if n ==1:
        ans += K//2
    elif S[0] == S[-1]:
        if len(cont)>1 and cont[0]%2==1 and cont[-1]%2==1:
            ans += K-1
        elif len(cont) ==1:
            ans = (n*K)//2
    print(ans)

Submission status: Accepted

Solution code:
# 入力                                                                                   
S = input()
K = int(input())

# kindリストに同じ文字が続くものごとに区切ったものを格納                                 
kind = []
back = "0"
len_S = len(S)
i = 0
for moji in S:
    if i == 0:
        back = moji
        i += 1
        continue
    if moji == back[-1]:
        back = moji + back
        if i == len_S-1:
            kind.append(back)
    else:
        kind.append(back)
        back = moji
    i += 1

score = 0
# 文字列が1文字の時
if len(S) == 1:
  	print(K//2)
   
# 最初の文字と最後の文字が異なる時                                                       
elif S[0] != S[-1]:
    for word in kind:
        len_word = len(word)
        score += len_word // 2
    print(score*K)
# 最初の文字と最後の文字が一緒の時                                                       
else:
    if len(set(kind)) == 1:
      	if len(kind) == 1:
          	print(len(kind[0])*K//2)
      	else:
        	print((len(kind[0])//2)*K)
    else:
        # 最初と最後がつながることでできる文字列の長さ                                   
        len_sg = len(kind[0] + kind[-1])
        for word in kind[1:-1]:
            len_word = len(word)
            score += len_word // 2
        print(score*K + (len_sg//2)*(K-1) + (len(kind[0]))//2 + (len(kind[-1]))//2 )

Submission status: Accepted

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

# 先頭と後頭が一致していた場合の例外処理
Sfront = S[0]
Srear  = S[-1]
if Sfront == Srear and K > 1:
    Sleft = S.lstrip(Srear)
    kazu_left = len(S) - len(Sleft)
    ans += int(kazu_left/2)
#     print(ans)
    
    Sleftright = Sleft.rstrip(Srear)
    kazu_right = len(Sleft) - len(Sleftright)
    if kazu_right != 0: # 通常
        ans += int(kazu_right/2)

        kazu_wa = len(S) - len(Sleftright)
        ans += int(kazu_wa/2) * (K-1)

        S = Sleftright
         
    else: # 全部同じ文字の例外
        ans = int(kazu_left * K / 2)
        
        S = ""
else:
    pass

while len(S) > 0:
#     print(S)
    current = S[0]
    Snext = S.lstrip(current)
    kazu = len(S) - len(Snext)
    ans += int(kazu/2) * K
    S = Snext
print(ans)

Submission status: Accepted

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

l = len(S)
ans = 0
if l == 1:
    ans = K//2
elif l == 2 : 
    if S[0]==S[1]:
        ans = K
    else:
        ans = 0
else:
    if S == S[0]*l:
        ans = (l*K)//2
    else:
        if S[0] == S[-1]:
            s = S[-1]
            for i,c in enumerate(S):
                if s!=c : break
            T = S[i:] + S[:i]
        else:
            T = S
        cntT = 0
        p = T[0]
        tmp_cnt = 1
        for c in T[1:]:
            if c == p:
                tmp_cnt +=1
            else:
                cntT += tmp_cnt // 2
                tmp_cnt = 1
            p = c
        cntT += tmp_cnt//2
        cntS = 0
        p = S[0]
        tmp_cnt = 1
        for c in S[1:]:
            if c == p:
                tmp_cnt +=1
            else:
                cntS += tmp_cnt // 2
                tmp_cnt = 1
            p = c
        cntS += tmp_cnt//2
        ans = cntT*(K-1) + cntS
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() 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)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


s = S()
k = I()
L = []
cnt = 0
ret = 1
if len(s) == 1:
    print(k // 2)
else:
    for j in range(len(s) - 1):
        if s[j] != s[0]:
            break

    for l in range(len(s) - 1):
        if s[-1] != s[-1 - l]:
            break


    for i in range(len(s) - 1):
        if s[i] == s[i + 1]:
            ret += 1
        else:
            if ret > 1:
                cnt += ret // 2
            ret = 1


    cnt += ret // 2


    ans = cnt * k
    if len(set(s)) == 1 and k % 2:
        ans = k * len(s) // 2
    elif s[0] == s[-1] and j % 2 and l % 2:
        ans += k - 1
    print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
fi=0
se=0
t=s+s
n=len(s)
now=1
for i in range(1,n):
  if s[i]==s[i-1]:
    now+=1
  else:
    fi+=now//2
    now=1
fi+=now//2
now=1
for i in range(1,n*2):
  if t[i]==t[i-1]:
    now+=1
  else:
    se+=now//2
    now=1
se+=now//2
ans=(se-fi)*(k-1)+fi
#print(se,fi)
ok=True
for i in range(n-1):
  if s[i]!=s[i+1]:
    ok=False
    break
if ok:
  ans=n*k//2
print(ans)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
count = 0
count2 = 1
count3 = 1
flag = 0
flag2 = 0
if len(S) == 1:
    print(K // 2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flag == 0:
            count += 1
            flag = 1
        else:
            flag = 0
        if S[i] != S[i+1]:
            flag2 = 1
    num = count * K
    if flag2 == 0:
        print(len(S) * K //2)
    else:

        if S[0] == S[-1]:
            if len(S) == 2:
                print(num)
            else:
                flag3 = 0
                for i in range(len(S)-1):
                    if S[i] == S[i+1] and flag3 == 0:
                        count2 += 1
                    else:
                        flag3 = 1
                flag3 = 0
                for i in range(len(S)-1, 0, -1):
                    if S[i] == S[i-1] and flag3 == 0:
                        count3 += 1
                    else:
                        flag3 = 1
                if count2 % 2 != 0 and count3 % 2 != 0:
                    print(num + K - 1)
                else:
                    print(num)
        else:
            print(num)

Submission status: Accepted

Solution code:
import copy

s=list(str(input()))
n=int(input())
s_base = copy.deepcopy(s)
i0 = s[0]
count = 0
if len(s) == 1:
    ans = n//2
else:
    #r1 = n//2 + n%2
    #s

    for i, name in enumerate(s[1:]):
        if name == i0:
            i0 = -1
            count += 1
            s[i+1] = i0
        else:
            i0 = name
    if s[0] == s[-1]:
        al = n-1
        if s[1] == -1:
            s2 = copy.deepcopy(s)
            for i in s_base:
                s2.append(i)
            i0 = s2[0]
            count2 = copy.deepcopy(count)
            for i, name in enumerate(s2[1:]):
                if name == i0:
                    i0 = -1
                    count2 += 1
                    s2[i+1] = i0
                else:
                    i0 = name
            al = (count2 - count*2)*(n//2)
            if s2[0] == s2[-1]:
                al = (count2 - count * 2) * (n-1)
    else:
        al = 0
    ans = count*n + al
print(ans)

Submission status: Accepted

Solution code:
def read():
    S = str(input().strip())
    K = int(input().strip())
    return S, K

def rle_encode(S):
    S1 = S + '@'
    rle = list()
    head = 0
    for i in range(0, len(S1)):
        if S1[i] != S1[head]:
            rle.append((S1[head], i-head))
            head = i
    return rle

def solve(S, K):
    count = 0
    rle = rle_encode(S)
    if len(rle) == 1:
        return rle[0][1] * K // 2
    else:
        for c, n in rle:
            count += n // 2
        count *= K
        if rle[-1][0] == rle[0][0]:
            if rle[-1][1] % 2 == 1 and rle[0][1] % 2 == 1:
                count += (K-1)
    return count

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Accepted

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

if len(s) == 1:
    print(int((len(s) * k) / 2))
    sys.exit()


l = 0
while (True):
    if s[l] != s[l+1]:
        break
    if l == len(s) - 2:
        print(int((len(s) * k) / 2))
        sys.exit()
    l += 1



j = 0
counta = 1
while (j<=len(s)-2):
    if s[j] == s[j+1]:
        counta += 1
        j += 1
        continue
    else:
        break


if counta % 2 == 0:
    flaga = 0
else:
    flaga = 1
        


countb = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        s = s[:i+1] + "0" + s[i+2:]
        countb += 1


flagb = 0
if s[len(s)-1] == s[0]:
    if flaga == 1:
        countb += 1
        flagb = 1
        
    


if flagb == 0:
    print(countb * k)
else:
    print(countb * k - 1)

Submission status: Accepted

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

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

is_same = False
b = ''
count = 0
tail = False
for c in s:
  if c == b and not is_same:
    count += 1
    is_same = True
  else:
    is_same = False
  b = c
else:
  if s[0] == s[-1]:
    cnt= 0
    for c in s:
      if c == s[0]:
        cnt += 1
      else:
        break
    for c in s[::-1]:
      if c == s[0]:
        cnt+=1
      else:
        break
    if cnt %2 ==0:
      tail = True
ans = count * k
if tail:
  ans += k-1
print(ans)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
SS=S+S
SSS=SS+S
count_single=count_double=count_triple=0
flag=True
for i in range(1, len(S)):
  if not flag:
    flag=True
    continue
  if S[i-1]==S[i]:
    count_single+=1
    flag=False
flag=True
for i in range(1, len(SS)):
  if not flag:
    flag=True
    continue
  if SS[i-1]==SS[i]:
    count_double+=1
    flag=False
flag=True
for i in range(1, len(SSS)):
  if not flag:
    flag=True
    continue
  if SSS[i-1]==SSS[i]:
    count_triple+=1
    flag=False
if K==1:
  print(count_single)
elif K==2:
  print(count_double)
else:
  diff21=count_double-count_single
  diff32=count_triple-count_double
  print(count_single+(K//2)*diff21+((K-1)//2)*diff32)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
a = [sum(1 for _ in g) for _, g in groupby(s)]
if len(a) == 1:
    print(len(s) * k // 2)
else:
    x, y = a[0], a[-1]
    b = (x + y) // 2 if s[0] == s[-1] else x // 2 + y // 2
    print(x // 2 + y // 2 + sum(x // 2 for x in a[1:-1]) * k + b * (k - 1))

Submission status: Accepted

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

S += "1"
l = len(S)
pair = []
current = S[0]
seq = 1
for i in range(1, l):
    if(i == l):
        pair.append(seq)
    elif(S[i] == current):
        seq += 1
    else:
        pair.append(seq)
        seq = 1
        current = S[i]

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

print(ans)

Submission status: Accepted

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

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

d, l, r = s[0], 0, 0

if d == s[-1]:
    for i in range(1, sl):
        if s[i] != d:
            l = i
            break
    
    for i, c in enumerate(s[::-1]):
        if c != d:
            r = i
            break

ans = l//2 + r//2 + (l+r) // 2 * (k-1)
s = s[l:sl-r]
cnt = 0

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

ans += cnt * k

print(ans)

Submission status: Accepted

Solution code:
s=list(input())
k=int(input())
if len(s)==1:
    print(k//2)
    exit()
from collections import Counter
l=Counter(s)
if len(l)==1:
    print(k*len(s)//2)
    exit()
ans=0
f=1
for i in range(len(s)-1):
    if s[i]==s[i+1] and f:
        ans+=1
        f=0
    else:
        f=1
cou=1
for i in range(1,len(s)):
    if s[i]==s[0]:
        cou+=1
    else:
        break
if s[0]==s[-1]:
    if f==0:
        print(ans*k)
    else:
        if s[0]!=s[1]:
            print(ans*k+k-1)
        else:
            if cou%2==1:
                print(ans*k+k-1)
            else:
                print(ans*k)


else:
    print(ans*k)

Submission status: Accepted

Solution code:
def f_a():
    s, k = input(), int(input())
    l, c = [], 1
    for i in range(1, len(s)):
        if s[i-1]==s[i]:
            c += 1
        else:
            l.append(c)
            c = 1
    if len(l)==0 or c>1:
        l.append(c)
    if len(l)>1:
        o = sum(map(lambda x: x//2*k, l[1:-1]))
        fe = l[0]//2+l[-1]//2
        if s[0]==s[-1]:
            o += fe+(l[0]+l[-1])//2*(k-1)
        else:
            o += fe*k
    else:
        o = l[0]*k//2
    print(o)

if __name__ == "__main__":
    f_a()

Submission status: Accepted

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

n = len(s)
cnt = 1
arr = []

for i in range(n):
    try:
        if s[i] == s[i+1]: cnt += 1
        else: arr.append(cnt) ; cnt = 1
    except: arr.append(cnt)

if len(arr) == 1 : ans = (len(s)*k)//2
else:
    if s[0] == s[n-1] : arr[0],arr[len(arr)-1] = 0,arr[0]+arr[len(arr)-1]
    ans = 0
    for i in range(len(arr)): ans += arr[i]//2
    ans *= k
    if arr[0]==0 and arr[len(arr)-1]%2==0 : ans -= 1

print(ans)

Submission status: Accepted

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

count=0
end=True
first_count=1

if k==1:
  for i in range(1,len(s)):
    if s[i-1]==s[i]:
      s[i]=1
      count+=1
  print(count)
else:
  is_same_all=True
  for i in range(1,len(s)):
    if s[i]!=s[i-1]:
      is_same_all=False
  if is_same_all:
    print((len(s)*k)//2)
    exit()
  if s[0]==s[-1]:
    for i in range(1,len(s)):
      if s[i-1]==s[i]:
        first_count+=1
      else:
        break     
  
  for i in range(1,len(s)):
    if s[i-1]==s[i]:
      s[i]=1
      count+=1
      if i==len(s)-1:
        end=False

  if end and s[0]==s[-1]:
    if first_count%2 != 0:
    	count+=1
    	print(k*count-1)
    else:
      print(k*count)
  else:
    print(k*count)

Submission status: Accepted

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

if len(set(S)) == 1:
    print((len(S) * K) // 2)
else:
    cnt = 0
    last = 0
    renzoku = 1
    for i in range(1, len(S)):
        if S[i] == S[0]:
            renzoku += 1
        else:
            break
    for i, s in enumerate(S):
        if i == 0 and S[i] == s and renzoku % 2 == 0:
            S[0] = '_'
            cnt += 1
        elif S[(i + 1) % len(S)] == S[(i + 2) % len(S)] == s:
            S[(i + 1) % len(S)] = '_'
            cnt += 1
        elif S[(i + 1) % len(S)] == s:
            S[i] = '_'
            cnt += 1
            if i == len(S) - 1:
                last = 1
    print(cnt * K - last)

Submission status: Accepted

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

counter = 0
tmp = s[0]
counter_list = []

s_dict = dict()
for i,one in enumerate(s):
    s_dict[one] = i
    
if len(s_dict) == 1:
    print((len(s)*k)//2)
else:
    for one in s[1:]:
        if tmp == one:
            counter += 1
        else:
            #counter_list.append((counter+1)//2)
            counter_list.append(counter+1)
            counter = 0
            tmp = one
    #counter_list.append((counter+1)//2)
    counter_list.append(counter+1)

    sum_counter = sum(counter_list)
    if s[0] == s[-1]:
        tmp_counter_list = counter_list[:-1]
        tmp_counter_list.append(counter_list[-1]+counter_list[0])
        tmp_counter_list = [one//2 for one in tmp_counter_list]
        last_counter_list = [one//2 for one in counter_list]
        print(tmp_counter_list[0]+sum(tmp_counter_list[1:])*(k-1) + sum(last_counter_list[1:]))
    else:
        last_counter_list = [one//2 for one in counter_list]
        print(sum(last_counter_list)*k)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
S = str(input())
K = int(input())

if len(set(S)) == 1:
    print(len(S) * K // 2)
else:
    before_s = ""
    dup_list = []
    dup_num = 0
#     文字列そのままの変換数
    for s in S:
        if s == before_s:
            dup_num += 1
            if dup_num == 1:
                dup_list.append(dup_num)
            else:
                dup_list[-1] = dup_num
        else:
            before_s = s
            dup_num = 0
    single_ans = 0
    for dnum in dup_list:
        single_ans += (dnum + 1) // 2
# 文字列2回重ねた変換数
    before_s = ""
    dup_list = []
    dup_num = 0
    for s in S * 2:
        if s == before_s:
            dup_num += 1
            if dup_num == 1:
                dup_list.append(dup_num)
            else:
                dup_list[-1] = dup_num
        else:
            before_s = s
            dup_num = 0
    double_ans = 0
    for dnum in dup_list:
        double_ans += (dnum + 1) // 2
    
    ans = single_ans * K + (double_ans - 2 * single_ans) * (K - 1)
    print(ans)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
c=1
b=[]
l=1
r=0
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    c+=1
  else:
    b.append(c)
    l=0
    c=1
if s[0]==s[-1]:
  b.append(c)
  c+=b[0]
else:
  b.append(c)
  c=0
if l==1:
  print((len(s)*k)//2)
else:
  if s[0]==s[-1]:
    if len(b)>=2:
      for i in range(len(b)-2):
        r+=(b[i+1]//2)*k
    print(r+b[0]//2+b[-1]//2+(c//2)*(k-1))
  else:
    for i in range(len(b)):
      r+=((b[i]//2)*k)
    print(r)

Submission status: Accepted

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

if s[0] != s[-1]:
    for i in range(len(s)-1):
        if s[i] == s[i + 1]:
            s[i+1] = "OK"
            ans += 1
    print(ans * k)

else:
    if len(set(s)) == 1:
        if len(s) % 2 == 0:
            print((len(s) // 2) * k)
        else:
            print(len(s) * (k // 2) if k %
                  2 == 0 else len(s) * (k // 2) + (len(s) // 2))
    else:
        mae = 1
        for i in range(len(s) - 1):
            if s[i] == s[i + 1]:
                mae += 1
            else:
                break

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

        for i in range(len(s)-1):
            if s[i] == s[i + 1]:
                s[i+1] = "OK"
                ans += 1

        ans_ = 0
        for i in range(mae, len(s) - ushiro):
            if s[i] == "OK":
                ans_ += 1
        print(((mae + ushiro) // 2 * (k - 1)) + ans_ * (k - 1) + ans)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
L = len(S)
a = S[0]
i = 0
C = []
if L == 1:
    C = [1]
while i < L-1:
    count = 1
    b,c = S[i],S[i+1]
    i+= 1
    while b == c:
        count += 1
        if i == L-1:
            break
        b,c = S[i],S[i+1]
        i += 1
    C.append(count)
if a != S[L-1]:
    num = 0
    for co in C:
        num += co//2
    ans = int(num*K)
else:
    if len(C) == 1:
        ans  = L*K//2
    else:
        num1 = 0
        for co in C:
            num1 += co//2
        num2 = (C[len(C)-1]+C[0])//2
        for co in C[1:len(C)-1]:
            num2 += co//2
        ans = int(num1+num2*(K-1))
print(ans)

Submission status: Accepted

Solution code:
s_list = list(input())
k = int(input())
s_len_list = []
dbls_len_list = []
dbls_list = s_list * 2
temp_len = 1
ans = 0

for i in range(len(s_list)-1):
    if s_list[i+1] == s_list[i]:
        temp_len += 1
    else:
        s_len_list.append(temp_len)
        temp_len = 1
s_len_list.append(temp_len)

temp_len = 1
for i in range(len(dbls_list)-1):
    if dbls_list[i+1] == dbls_list[i]:
        temp_len += 1
    else:
        dbls_len_list.append(temp_len)
        temp_len = 1
dbls_len_list.append(temp_len)
# print(s_list)
# print(s_len_list)
# print(dbls_list)
# print(dbls_len_list)
if len(s_len_list) == 1:
    ans = (s_len_list[0] * k) // 2
elif s_len_list[len(s_len_list)-1] % 2 == 1 and dbls_len_list[len(s_len_list)-1] % 2 == 0:
    for i in s_len_list:
        ans += i // 2
    ans *= k
    ans += (k-1)
else:
    for i in s_len_list:
        ans += i // 2
    ans *= k
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
S=input().strip()
K=int(input())
C=Counter(S)

if S[0]!=S[-1]:
    A=0

    ind=1
    while ind<len(S):
        if S[ind]==S[ind-1]:
            A+=1
            ind+=2
        else:
            ind+=1

    print(A*K)

elif len(C)==1:
    print(len(S)*K//2)

else:
    front=1
    back=1

    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            front+=1
        else:
            frd=i
            break

    for i in range(len(S)-2,-1,-1):
        if S[i]==S[i+1]:
            back+=1
        else:
            bcd=i
            break

    #print(frd,bcd,front,back)

    ANS=(front+back)//2*(K-1)+front//2+back//2

    ind=frd+1
    A=0

    while ind<=bcd:
        if S[ind]==S[ind-1]:
            A+=1
            ind+=2
        else:
            ind+=1

    #print(A)

    print(ANS+A*K)

Submission status: Accepted

Solution code:
S=list(input())
K=int(input())
if K%2==0:
    A=S.copy()
    S=S+S
    ans=0
    x=0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            S[i+1]=":"
            ans+=1
    if S[0]==S[-1] and K>3:
        x=(1)*(K//2-1)
    print((K//2)*ans+x)
else:
    ans=0
    A=S.copy()
    S=S+S
    x=0
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            S[i+1]=":"
            ans+=1
    if S[i]!=":" and K!=1:
        A=list(S[-1])+A
    for i in range(len(A)-1):
        if A[i]==A[i+1]:
            A[i+1]=":"
            x+=1
    print((K//2)*ans+x)

Submission status: Accepted

Solution code:
def main():
    S=input()
    K=int(input())
    l=len(S)
    chars=[]
    bchar=S[0]
    echar=S[l-1]
    curChar=bchar
    seq=1
    idx=0
    for i in range(1,l-1):
        if S[i]==curChar:
            seq+=1
        else:
            chars.append((idx,seq))
            curChar=S[i]
            idx+=1
            seq=1

    if S[l-1]==curChar:
        chars.append((idx,seq+1))
    else:
        chars.append((idx,seq))
        chars.append((idx+1,1))
    ans=0;
    types=len(chars)
    if types==1:
        ans=(l*K)//2
    elif bchar!=echar:
        for c in chars:
            ans += c[1]//2
        ans*=K
    else:
        tmp=0
        ansb=chars[0][1]//2
        anse=chars[types-1][1]//2
        ansbe=(chars[0][1]+chars[types-1][1])//2
        for i in range(1,types-1):
            tmp+=chars[i][1]//2
        ans=ansb+(tmp*K)+(ansbe*(K-1))+anse
    print(ans)
main()

Submission status: Accepted

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

SL=list(S)
seq=0
for i in range(len(SL)-1):
  if SL[i]==SL[i+1]:
    SL[i+1]=SL[i+1].upper()
    seq+=1

if K==1 or SL[0]!=SL[-1]:
  print(seq*K)
else:
  SL2=list(S)
  SL2[0]="#"
  seq2=1
  for i in range(1,len(SL2)-1):
    if SL2[i]==SL2[i+1]:
      SL2[i+1]=SL2[i+1].upper()
      seq2+=1
  
  if SL2[-1]!=S[0]:
    if K%2==0:
      print((seq+seq2)*(K//2))
    else:
      print(seq*((K+1)//2)+seq2*(K//2))
  else:
    print(seq+seq2*(K-1))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
A = None
C = 1
D = []
E = False
if S[-1]==S[0]:
    E = True
for i in range(len(S)-1):
  if S[i]==S[i+1]:
    C += 1
  else:
    D.append(C)
    C = 1
D.append(C)
if len(D)==1:
  A = int(D[0]*K/2)
else:
  F = int(D[0]/2)+int(D[-1]/2)
  I = 0
  for j in range(1,len(D)-1):
    I += int(D[j]/2)
  if E:
    G = int((D[0]+D[-1])/2)
    A = F + G*(K-1) + I*K
  else:
    A = (F + I)*K
print(A)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
cnt = 0
cnt2 = 0
cnt3 = 0
streak = 1
s2 = s + s
s3 = s2 + s

# s3に対して
for i in range(3*len(s)-1):
    if s3[i + 1] == s3[i]:
        streak += 1
    else:
        if streak > 1:
            cnt3 += streak // 2
        streak = 1
if streak > 1:
    cnt3 += streak // 2


# s2 に対して
streak = 1
for i in range(2*len(s)-1):
    if s2[i + 1] == s2[i]:
        streak += 1
    else:
        if streak > 1:
            cnt2 += streak // 2
        streak = 1
if streak > 1:
    cnt2 += streak // 2

# sに対して
streak = 1
for i in range(len(s) - 1):
    if s[i + 1] == s[i]:
        streak += 1
    else:
        if streak > 1:
            cnt += streak // 2
        streak = 1
if streak > 1:
    cnt += streak // 2

if cnt3 - cnt2 == cnt2 - cnt:
    res = cnt + (cnt2 - cnt) * (k - 1)
else:
    if k > 2:
        res = cnt + ((cnt2 - cnt) + (cnt3 - cnt2)) * ((k - 1) // 2)
        if k % 2 == 0:
            res += (cnt2 - cnt)
    else:
        if k == 1:
            res = cnt
        else:
            res = cnt2
print(res)

Submission status: Accepted

Solution code:
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

s = ns()
k = ni()

w = s[0]
a = []
count = 1
for i in s[1:]:
    if w == i:
        count += 1
    else:
        a.append(count)
        count = 1
        w = i
if s.count(s[0]) == len(s):
    ans = (len(s) * k) // 2
elif w != s[0]:
    a.append(count)
    ans = 0
    for i in a:
        ans += i // 2
    ans = ans * k
else:
    al = a[0]
    ar = count
    ans = 0
    for i in a[1:]:
        ans += i // 2
    ans = ans * k
    ans += al //2 + ar //2
    ans += ((al + ar)//2) * (k -1)
print(ans)

Submission status: Accepted

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

count = 0
cont = 0

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

#すべて同じ文字の場合の処理
if len(S) == S.count(S[0]):
    count += (len(S) * K) // 2
    print(count)
    exit()

#最初のブロックの連続数える
firstword = 0
first_index = 0
if S[0] == S[-1]:
    for i in range(len(S) - 1):
        if S[i] == S[i+1]:
            cont += 1
        else:
            firstword = cont
            cont = 0
            first_index = i
            break 

#末尾からの連続を数える。
lastword = 0
last_index = 0
for i in reversed(range(len(S))):
    if i == 0:
        break
    if S[i] == S[i-1]:
        cont += 1
    else:
        lastword = cont
        cont = 0
        last_index = i
        break

#中間部分を数える。
middleword = 0
for i in range(first_index, last_index):
    if S[i] == S[i+1]:
        cont += 1
    else:
        middleword += (cont + 1) // 2
        cont = 0


if S[0] == S[-1]:
    count += middleword * K
    count += ((firstword + lastword + 2) // 2 ) * (K-1)
    count += (firstword + 1) // 2 + (lastword + 1) // 2
else:
    count += middleword * K
    count += ((firstword + 1) // 2 + (lastword + 1) // 2) * K

print(count)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
brob = 0
temp = 1
first = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        temp += 1
    else:
        if first == 0:
            first = temp
        brob += temp // 2
        temp = 1
        
brob += temp // 2
last = temp

if K == 1:
    print(brob)

else:
    if last == len(S):
        print(len(S) * K // 2)
    
    elif S[-1] != S[0]:
        print(brob * K)
    

    elif last % 2 and first % 2:
        print(brob * K + K - 1)

    else:
        print(brob * K)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    ss = str(input().rstrip('\n'))
    nn = int(input().rstrip('\n'))
    s = 0
    sb = True
    e = 0
    eb = True
    t = 0
    cnt = 0
    if len(ss) == 1:
        print(len(ss) * nn // 2)
        exit()
    else:
        if ss[0] == ss[-1]:
            for i in range(len(ss)):
                if ss[i] == ss[0]:
                    if sb:
                        s += 1
                else:
                    sb = False
                if ss[-i-1] == ss[0]:
                    if eb:
                        e += 1
                else:
                    eb = False
        else:
            sb = False
            eb = False
        if sb and eb:
            if s % 2 == 0:
                cnt += ((s + e) // 2 // 2)
                s = 0
                e = 0
            else:
                print(s * nn // 2)
                exit()
        else:
            for i in range(max(s, 1), min(len(ss) - e + 1, len(ss))):
                if ss[i] == ss[i-1]:
                    t += 1
                else:
                    t += 1
                    cnt += (t // 2)
                    t = 0

            cnt += ((t + 1) // 2)
    print(cnt * nn + s // 2 + e // 2 + (s + e) // 2 * (nn - 1))


if __name__ == '__main__':
    slove()