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())
n = len(s)
p = ['']*(4*n)

for i in range(n):
    p[i] = s[i]
    p[i+n] = s[i]
    p[i+2*n] = s[i]
    p[i+3*n] = s[i]

a = [0,0,0,0]

judge = 0
for i in range(1,4*n):
    if p[i] == p[i-1]:
        a[i//n] += 1
        p[i] = ''
        if i == n:
            judge = 1
        if judge == 1 and i == 2*n:
            judge = 2

if judge == 0:
    print(a[0]*k)
    
elif judge == 2:
    if k == 1:
        print(a[0])
    else:
        print(a[0]+a[1]*(k-1))
else:
    res = (a[0]+a[1])*(k//2)
    if k%2 == 1:
        res += a[0]
    print(res)

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:
    r = sum(x // 2 for x in a) * k
    if s[0] == s[-1]:
        r += ((a[0] + a[-1]) // 2 - a[0] // 2 - a[-1] // 2) * (k - 1)
    print(r)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC39 A

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
lst = deque()
while i < n:
    tmp = s[i]
    i += 1
    cnt = 1
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    lst.append(cnt)
if len(lst) >= 2:
    if s[0] == s[-1] and k >= 2:
        x = lst.popleft()
        y = lst.pop()
        res = 0
        for i in lst:
            res += (i//2)*k
        res += (x//2) + (y//2)
        res += ((x+y)//2)*(k-1)
        print(res)
    else:
        res = 0
        for i in lst:
            res += (i//2)*k
        print(res)
else:
    print((n*k)//2)

Submission status: Accepted

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

s1 = list(S)
c1 = 0
for i in range(1, len(s1)):
  if s1[i-1] == s1[i]:
    s1[i] = ''
    c1 += 1

if K == 1 or s1[-1] != s1[0]:
  print(c1 * K)
else:
  s2 = s1[-1:] + list(S)
  c2 = 0
  for i in range(1, len(s2)):
    if s2[i-1] == s2[i]:
      s2[i] = ''
      c2 += 1
  if s2[-1] == s1[0]:
    print(c1 + c2*(K-1))
  else:
    print(c1*(K - K//2) + c2*(K//2))

Submission status: Accepted

Solution code:
k=list(input())
l=k[::]
n=int(input())
for i in range(len(k)):
    if i==0:
        continue
    a,b=k[i-1],k[i]
    if a==b:
        k[i]="0"
l[0]="0"
for i in range(len(l)):
    if i==0:
        continue
    a,b=l[i-1],l[i]
    if a==b:
        l[i]="0"
if k[-1]!=k[0]:
    print(k.count("0")*n)
else:
    if k[0]!=l[-1]:
        print(k.count("0")*(n-n//2)+l.count("0")*(n//2))
    else:
        print(k.count("0")+l.count("0")*(n-1))

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
count = 0
s1 = list(set(s))
if len(s1) == 1:
    count = int((k * len(s)) / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    elif (count + 1) % 5 == 0:
        count = ((count + 1) / 5) * k - 1
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Submission status: Accepted

Solution code:
import numpy as np

S = raw_input()
K = int(raw_input())

S_twice = S + S

A = np.full(len(S_twice),0)
A_twice = np.full(len(S_twice),0)

count = 0
for i in range(1, len(S)):
    if S[i] == S[i-1]:
        count = count + 1
    else:
        A[count] = A[count] + 1
        count = 0

A[count] = A[count] + 1

count = 0
for i in range(1, len(S_twice)):
    if S_twice[i] == S_twice[i-1]:
        count = count + 1
    else:
        A_twice[count] = A_twice[count] + 1
        count = 0

A_twice[count] = A_twice[count] + 1

if count == len(S_twice)-1:
    print len(S)*K/2
    exit()

A_k = A + (K-1)*(A_twice-A)

ans = 0

for i in range(1, A_k.shape[0]):
    ans = ans + (i+1)/2*A_k[i]

print int(ans)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
same = []
tmp = 1
ans = 0
tsunagi = 0
for i in range(len(s)-1):
         if s[i] == s[i+1]:
                  tmp += 1
         else:
                  same.append(tmp)
                  tmp = 1
same.append(tmp)
if s[0] == s[len(s)-1]:
         tsunagi = 1
if tmp == len(s):
         print((len(s)*k)//2)
elif tsunagi == 0:
         for i in range(len(same)):
                  ans += (same[i]//2)*k
         print(ans)
else:
         for i in range(1,len(same)-1):
                  ans += (same[i]//2)*k
         ans += ((same[0]+same[len(same)-1])//2)*(k-1)
         ans += (same[0]//2)+((same[len(same)-1])//2)
         print(ans)

Submission status: Accepted

Solution code:
import math

S = input()
K = int(input())
cnt = 0
tmp = 1
if K == 1:
    for i in range(1, len(S)):
        if S[i - 1] == S[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1
    if tmp >= 2:
        cnt += tmp // 2
else:
    g1 = 0
    g2 = 0
    jlst = S + S
    for i in range(1, len(S) * 2):
        if jlst[i - 1] == jlst[i]:
            tmp += 1
        elif tmp >= 2:
            cnt += tmp // 2
            tmp = 1
        else:
            tmp = 1

    if tmp >= 2:
        cnt += tmp // 2

    if cnt % 2 == 0:
        cnt = K * (cnt // 2)
    else:
        g1 = cnt // 2
        g2 = cnt - g1
        v = math.ceil(K / 2)
        if tmp >= 3 and tmp % 2 != 0 and S[0] == S[-1]:
            cnt = g1 + (g2 * (K - 1))
        else:
            cnt = (g1 * v) + (g2 * (K - v))

print(cnt)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
S = input()
K = int(input())

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

cnt_one = 0
i = 0
while i < lenS-1:
    if S[i] == S[i+1]:
        cnt_one += 1
        i += 2
    else:
        i += 1

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

SS = S*2
lenSS = len(SS)

cnt_two = 0
i = 0
while i < lenSS-1:
    if SS[i] == SS[i+1]:
        cnt_two += 1
        i += 2
    else:
        i += 1

if len(set(S)) == 1:
    if lenS%2 == 1:
        if K%2 == 0:
            ans = (cnt_two) * (K//2)
        else:
            ans = (cnt_one * (K//2 + 1)) + ((cnt_two-cnt_one) * (K//2))
        print(ans)
        exit()

print((cnt_two - cnt_one)*(K-1) + cnt_one)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
S_list = [i for i in S]
answer = []
for i in range(1, 4):
    S_rep = S_list * i
    S_ans = 0
    for j in range(len(S_rep) - 1):
        if S_rep[j] == S_rep[j + 1]:
            S_ans += 1
            S_rep[j + 1] = "A"
    answer.append(S_ans)

def_S = [answer[1] - answer[0], answer[2] - answer[1]]

if K == 1:
    print(answer[0])
elif (K - 1) % 2 == 0:
    print(answer[0] + sum(def_S) * (K - 1) // 2)
else:
    print(answer[0] + sum(def_S) * (K - 2) // 2 + def_S[0])

Submission status: Accepted

Solution code:
s = input()
k = int(input())
if len(s)==1:
    print(k//2)
    exit()
import re


def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける        
    """
    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
lst = list(nchars(s,2))
count = 0
for i in lst:
    count += len(i)//2
count = count*k

if s[0]==s[-1]:

    first = s[0]
    f = 0
    e = 0
    end = s[-1]
    for i in s:
        if first == i:
            f += 1
        else:
            break
    for i in reversed(s):
        if end == i:
            e += 1
        else:
            break
    if f % 2 == 1 and e % 2 == 1:
        if f == len(s) and k % 2 ==1:
            count+=k//2
        else:
            count += k-1

print(count)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
count1 = 0
count2 = 1
f = s[0]
memo = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo[-1] and c1 == c2:
        count1 += 1
        memo.append(1)
    else:
        memo.append(0)
s[0] = "#"
memo2 = [0]
for c1, c2 in zip(s[:], s[1:]):
    if not memo2[-1] and c1 == c2:
        count2 += 1
        memo2.append(1)
    else:
        memo2.append(0)
if len(s) == 1:
    print(k // 2)
    exit()
if f == s[-1]:
    if memo[-1]:
        print(k * count1)
    else:
        if memo2[-1]:
            print(count1 * (k - k // 2) + count2 * (k // 2))
        else:
            print(count1 + count2 * (k-1))
else:
    print(k * count1)

Submission status: Accepted

Solution code:
s=input()
l=len(s)
k=int(input())
head=0
tail=l-1
for i in range(l-1):
  if s[i]==s[i+1]:
    head=i+1
  else:
    break
for i in range(l-1,0,-1):
  if s[i]==s[i-1]:
    tail=i-1
  else:
    break
head_chars=head+1
tail_chars=l-tail
if head_chars==l:
  ans=(l*k)//2
  print(ans)
elif head_chars+tail_chars==l:
  ans=(head_chars//2+tail_chars//2)*k
  print(ans)
else:
  body_text=s[head+1:tail]
  ans=0
  tmp=0
  cnt=1
  for i in range(1,len(body_text)):
    if body_text[i]==body_text[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  tmp+=cnt//2
  ans+=tmp*k
  headtail_text=s[tail:]+s[:head+1]
  tmp=0
  cnt=1
  for i in range(1,len(headtail_text)):
    if headtail_text[i]==headtail_text[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  tmp+=cnt//2
  ans+=tmp*(k-1)
  ans+=head_chars//2
  ans+=tail_chars//2
  print(ans)

Submission status: Accepted

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

S2 = S * 2

singleCnt = 0
doubleCnt = 0
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    S[i+1] = "1" # かぶらない文字に変換
    singleCnt += 1
if S[0] != S[-1]:
  ans = singleCnt * K
  
elif S[0] == S[-1]:
  for i in range(len(S2)-1):
    if S2[i] == S2[i+1]:
      S2[i+1] = "1" # かぶらない文字に変換
      doubleCnt += 1
  if S2[0] != S2[-1]:
    a, b = divmod(K, 2)
    if b == 0:
      ans = doubleCnt * (K // 2)
    else:  
      ans = doubleCnt * (K // 2) + singleCnt
  else:
    ans = (doubleCnt - singleCnt) * (K - 1) + singleCnt

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby


s = input()
k = int(input())
s_sub = [[h, len(list(g))] for h, g in groupby(s)]
cnt_s = 0
if s[0] == s[-1]:
    if len(s_sub) == 1:
        ans = s_sub[0][1] * k // 2
    else:
        for i in range(1, len(s_sub)-1):
            cnt_s += s_sub[i][1] // 2
        ans = cnt_s * k + s_sub[0][1] // 2 + s_sub[-1][1] // 2 + ((s_sub[0][1] + s_sub[-1][1]) // 2) * (k - 1)
else:
    for i in range(len(s_sub)):
        cnt_s += s_sub[i][1] // 2
    ans = cnt_s * k
print(ans)

Submission status: Accepted

Solution code:
S=input()
K=int(input())
flg=False
for s in S:
    if S[0] != s:
        flg=True
        break
if not flg:
    print(len(S)*K//2)
    exit()
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break

    if renzoku >1:
        ans+=renzoku//2
    i+=1

if S[0] == S[-1]:
    s=S[0]
    top=0
    bottom=0
    i=0
    while S[i] == s:
        top+=1
        i+=1
    i=-1
    while S[i] == s:
        bottom+=1
        i-=1
    if (top%2) + (bottom%2) == 2:
        print((ans+1)*K-1)
        exit()
    
print(ans*K)

Submission status: Accepted

Solution code:
import re

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

if len(s) == 1:
    print(int(k / 2))
elif re.search(r'^(.)\1+$', s):
    print(int(k * len(s) / 2))
else:
    series = [1,]
    for i in range(0, len(s)-1):
        if s[i] == s[i+1]:
            series[-1] += 1
        else:
            series.append(1)
    # print(series)
    changeCharCount = 0
    for sameCharBlockLen in series:
        changeCharCount += int(sameCharBlockLen / 2)
    changeCharCount *= k
    if s[0] == s[-1]:
        if (series[0] % 2 == 1) and (series[-1] % 2 == 1):
            changeCharCount += (k - 1)
    print(changeCharCount)

Submission status: Accepted

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

moji = []

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

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

else:
  for i in range(len(moji)):
    ans += (moji[i] // 2) * K
    
print(ans)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
n = len(S)
S = S + S[0]
samecount = 1
count = 0
pre = ''
for s in S:
    if s == pre:
        samecount += 1
    else:
        count += samecount//2
        samecount = 1
    pre = s

ans = count * K

if samecount == 1:
    pass
else:
    samecount -= 1
    right = samecount
    if samecount == n:
        if samecount % 2 == 0:
            ans += K*(samecount//2)
        else:
            ans += K*(samecount//2)
            ans += K//2
    else:
        left = 1
        pre = S[0]
        for s in S[1:-1]:
            if s == pre:
                left += 1
            else:
                break
        if right % 2 == 0 or left % 2 == 0:
            ans += K*(right//2)

        else:
            ans += K*(right//2)
            ans += K-1

print(ans)

Submission status: Accepted

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

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

cnt=0
i=1
while i <len(s):
  if i>0:
    if s[i]==s[i-1]:
      cnt+=1
      i+=2
    else:
      i+=1
  
if k==1:
  print(cnt)
  exit()
  
ss=s+s
cntcnt=0
i=1
while i <len(ss):
  if i>0:
    if ss[i]==ss[i-1]:
      cntcnt+=1
      i+=2
    else:
      i+=1
if k==2:
  print(cntcnt)
  exit()
  
sss=s+s+s
cntcntcnt=0
i=1
while i <len(sss):
  if i>0:
    if sss[i]==sss[i-1]:
      cntcntcnt+=1
      i+=2
    else:
      i+=1
if k==3:
  print(cntcntcnt)
  exit()
      

ssss=s+s+s+s
cntcntcntcnt=0
i=1
while i <len(ssss):
  if i>0:
    if ssss[i]==ssss[i-1]:
      cntcntcntcnt+=1
      i+=2
    else:
      i+=1
      
if k==4:
  print(cntcntcntcnt)
  exit()
  
if cntcntcntcnt-cntcntcnt==cntcntcnt-cntcnt:
  print(cntcnt+(cntcntcnt-cntcnt)*(k-2))
else:
  print(cnt-(cntcnt-cnt)*((1-k)//2)+(cntcntcnt-cntcnt)*(k-1)//2)

Submission status: Accepted

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

tmp_count = 0
continue_str = s[0]
cont_nums = []
for i, si in enumerate(s):
    if continue_str == si:
        tmp_count += 1
    else:
        cont_nums.append(tmp_count)
        tmp_count = 1
        continue_str = si
    if i == len(s) - 1:
        cont_nums.append(tmp_count)

count_in_loop = 0
half_count = 0
result = 0
if len(cont_nums) == 1:
    result = cont_nums[0] * k // 2
else:
    for i, cni in enumerate(cont_nums):
        count_in_loop += cni // 2
    if (s[0] == s[len(s)-1]) & (cont_nums[0] % 2 == 1) & (cont_nums[-1] % 2 == 1):
        half_count = 1
    result = count_in_loop * k + half_count * (k-1)
print(result)

Submission status: Accepted

Solution code:
s = list(input())
s2 = s[:]
k = int(input())

s1_count = 0
for i in range(1, len(s)):
    if s[i-1] == s[i]:
        s1_count += 1
        s[i] = '-'

if k == 1:
    print(s1_count)
elif s[0] != s[-1]:
    print(s1_count * k)
elif len(list(set(s2))) == 1:
    if len(s) % 2 == 0:
        print(s1_count * k)
    else:
        s2_count = s1_count + 1
        if k % 2 == 0:
            print(int((s1_count + s2_count) * (k // 2)))
        else:
            print(int((s1_count + s2_count) * (k // 2) + s1_count))
else:
    s2_count = 0
    if s[-1] == '-':
        print(s1_count * k)
    else:
        s2[0] = '-'
        s2_count += 1
        for i in range(1, len(s2)):
            if s2[i-1] == s2[i]:
                s2_count += 1
                s2[i] = '-'
        print(s1_count + s2_count * (k-1))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
pre_character = ''
S_list=list(S)
s_len = len(S)
def try_count(pre_character):
    S_list=list(S)
    count = 0
    for i in range(s_len):
        if pre_character == S_list[i]:
            count += 1
            if (s_len - 1) != i:
                S_list[i] = '' #get_abc(pre_character,S_list[i+1])
            else:
                S_list[i] = '' #get_abc(pre_character,S[0])
        pre_character = S_list[i]
    return count,S_list
first_count,fist_list = try_count('')
#print(first_count,fist_list)
second_count,second_list = try_count(fist_list[-1])
#print(second_count,second_list)
third_count,third_list = try_count(second_list[-1])
#print(third_count,third_list)

if second_count == third_count:
    print(first_count+ (second_count*(K-1)))
else:
    gusu = K//2
    kisu = K - gusu
    print((first_count*kisu)+ (second_count*gusu))

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)
    else:
        print(count+(count2+1)*(k-1))
else:
    print(count*k)

Submission status: Accepted

Solution code:
import numpy as np
import copy

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

A = np.zeros(1)
j = 0 

for i in range(len(S)-1):
    if S[i] == S[i+1]:
        A[j] += 1
    else:
        j += 1
        A = np.append(A, 0)
A += 1

if np.size(A) == 1:
    ans = (len(S) * K) // 2
    
else:
    B = copy.deepcopy(A)
    if S[0] == S[-1]:
        B[0] += A[-1]
        B = np.delete(B, np.size(A)-1)
        ans = np.sum(A // 2) + np.sum(B // 2) * (K-1)
    else:
        ans = np.sum(A // 2) * K

print(int(ans))

Submission status: Accepted

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

prev_c = S[0]
num_changes = 0
for c in S[1:]:
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

total_changes = 0
if len(S) == 1:
    if len(S) % 2 == 0:
        total_changes = int(K/2)
    else:
        total_changes = int((K-1)/2)
elif S[0] == prev_c and S[1] == S[0] and len(S) % 2 == 1:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes * int(K/2 + 1) + num_changes2 * int(K/2)
elif S[0] == prev_c:
    num_changes2 = 0
    for c in S:
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes + num_changes2 * (K-1)
else:
    total_changes = num_changes * K

print(total_changes)

Submission status: Accepted

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

n = len(s)
s2 = s + s

renzoku = False
taisho = 0
aida = False
for i in range(2*n-1):
    if s2[i] == s2[i+1]:
        if not renzoku:
            renzoku = True
            start_i = i
            count = 0
        count += 1

    elif renzoku:
        renzoku = False
        end_i = i
        count += 1
        #print(start_i, end_i)
        if count % 2 == 1:
            indexs = list(range(start_i+1, end_i, 2))
            taisho += len(indexs)
            #print(indexs)
            if (n-1 in indexs) or (n in indexs):
                aida = True
        elif count % 2 == 0:
            indexs1 = list(range(start_i, end_i, 2))
            indexs2 = list(range(start_i+1, end_i, 2))
            taisho += len(indexs1)
            #print(indexs1)
            all_indexs = indexs1 + indexs2
            if (n-1 in all_indexs) or (n in all_indexs):
                aida = True

last_renzoku = False
if renzoku:
    last_renzoku = True
    renzoku = False
    end_i = 2*n - 1
    count += 1
    if count % 2 == 1:
        indexs = list(range(start_i+1, end_i, 2))
        taisho += len(indexs)
        #print(indexs)
        if (n-1 in indexs) or (n in indexs):
            aida = True
        last_odd = True
    elif count % 2 == 0:
        indexs1 = list(range(start_i, end_i, 2))
        indexs2 = list(range(start_i+1, end_i, 2))
        taisho += len(indexs1)
        #print(indexs1)
        all_indexs = indexs1 + indexs2
        if (n-1 in all_indexs) or (n in all_indexs):
            aida = True
        last_odd = False


#if True:
if not aida:
    assert taisho % 2 == 0
    hitotsu = taisho // 2
    out = hitotsu * k
else:
    if taisho % 2 == 0: 
        hitotsu = taisho // 2
        out = hitotsu * k
    else:
        if not last_renzoku:
            hitotsu = (taisho+1) // 2
            out = hitotsu * k - 1
        else:
            x = (taisho - 1) // 2
            #print("last_odd", last_odd)
            if last_odd:
                out = x + (x+1) * (k-1)
            else:
                if k % 2 == 0:
                    out = taisho * (k // 2)
                else:
                    out = taisho * ((k-1) // 2) + x


print(out)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
c = 1
i = 1
l = []
c2 = 0
while i < len(s):
    if i == len(s)-1:
        if s[i] == s[i-1]:
            c = c+1
            l.append(c)
            i = i+1
        else:
            l.append(c)
            l.append(1)
            i = i+1
    elif s[i] == s[i-1]:
        c = c + 1
        i = i + 1
    else:
        l.append(c)
        c = 1
        i = i+1
if len(l) == 1:
    print((len(s)*k)//2)
elif s[0] == s[len(s)-1]:
    if len(s)==1:
        print(k//2)
    else:
        for i in range(1,len(l)-1):
            c2 = c2 + l[i]//2
        print(c2*k+((l[0]+l[len(l)-1])//2)*(k-1)+l[0]//2+l[len(l)-1]//2)
else:
    for i in range(0,len(l)):
        c2 = c2 + l[i]//2
    print(c2*k)

Submission status: Accepted

Solution code:
s=input()
s1=s
k=int(input())
ans1=0
ans2=0
for i in range(len(s)-1):
  if s1[i]==s1[i+1]:
    s1=s1[:(i+1)]+'0'+s1[(i+2):]
    ans1+=1
if s1[0]==s1[-1]:
  s1=s1+'0'+s[1:]
  ans2+=1
  k-=1
  for i in range(len(s),len(s1)-1):
    if s1[i]==s1[i+1]:
      s1=s1[:(i+1)]+'0'+s1[(i+2):]
      ans2+=1 
  if s1[0]==s1[-1]:
    ans=ans1+ans2*k
  else:
    ans=(ans1+ans2)*(k//2)+ans1
else:
  ans=ans1*k
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

S = input().strip()
K = int(input())

ans = []
start = S[0]
count = 1

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

flag = True
for i in range(len(S) - 1):
    if S[i] != S[i+1]:
        flag = False
if flag:
    print((len(S)*K) // 2)
    exit()

flag = True
T = S + S[0]
for i in range(len(T) - 1):
    if T[i] == T[i+1]:
        flag = False
if flag:
    print(0)
    exit()

#if S[0] == S[-1]:
    #S = S + S[0]
    #trigger = True

for i in range(1, len(S)):
    now = S[i]
    if start == now:
        count += 1
    else:
        if count >= 2:
            ans.append(count // 2)
        count = 1
    start = now

if count >= 2:
    ans.append(count // 2)

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

if (count + counter2) % 2 == 0:
    trigger = True
else:
    trigger = False

if S[0] == S[-1] and trigger:
    ans.append(1)
    trigger = True
else:
    trigger = False

out = sum(ans) * K
if trigger:
    out -= 1
print(out)

Submission status: Accepted

Solution code:
def ConnectionAndDisconnection(S, K):
	total = 0
	altS = ''
	minus1 = False
	if S[0] == S[-1]:
		char = S[0]
		S1 = ''
		for i in range(len(S)):
			if S[i] != char:
				S1 = S[i:]
				break

		if S1 == '':
			rslt = int(len(S) * K / 2)
			print(rslt)
			exit
		else:
			rev1 = S1[::-1]
			for i in range(len(rev1)):
				if rev1[i] != char:
					rev2 = rev1[i:]
					break
			altS = rev2[::-1]
			total += int((len(S) - len(altS)) / 2)
			if (len(S) - len(altS)) % 2 == 0:
				minus1 = True
	else:
		altS = S

	if altS != '':
		T = altS + '_'
		tmpcnt = 0
		for i in range(len(T) - 1):
			if T[i] == T[i+1]:
				tmpcnt += 1
			else:
				total += int((tmpcnt+1) / 2)
				tmpcnt = 0

		rslt = total * K
		if minus1:
			rslt -= 1
		print(rslt)

S = str(input())
K = int(input())
ConnectionAndDisconnection(S, K)

Submission status: Accepted

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

len_s=len(s)
ss=s+s
sss=s+s+s
#print(sss)
len_ss=len(ss)
len_sss=len(sss)
ans=0

if len(set(sss))==1:
	ans=k*len_s//2
		


else:
	if k==1:
		for i in range(len_s-1):
			if s[i]==s[i+1]:
				ans=ans+1
				s=list(s)
				s[i+1]="0"
				s="".join(s)
	if k==2:
		for i in range(len_ss-1):
			if ss[i]==ss[i+1]:
				ans=ans+1
				ss=list(ss)
				ss[i+1]="0"
				iss="".join(ss)
			
	if k>=3:
		for i in range(len_sss-1):
			if sss[i]==sss[i+1]:
				ans=ans+1
				sss=list(sss)
				sss[i+1]="0"
				sss="".join(sss)
		
		
		if ans%3==0:
				ans=ans/3*k
				
		else:
				ans=ans//3*k+k-1
	



print(int(ans))

Submission status: Accepted

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

lis = []
b = None
cont = -1
for c in s:
    if c != b:
        if b is not None:
            lis.append(cont)
        b = c
        cont = 1
    else:
        cont += 1
if b is not None:
    lis.append(cont)

# print(lis)

if k == 1 or s[-1] != s[0]:
    print(sum(map(lambda x: x // 2, lis)) * k)
else:
    if len(lis) == 1:
        odd = lis[0] // 2
        even = ((lis[0] % 2) + lis[0]) // 2
        # print(odd, even, k // 2)
        print(even * (k // 2) + odd * (k - k // 2))
    else:
        top = lis[0] // 2
        mid = sum(map(lambda x: x // 2, lis[1:-1]))
        bottom = lis[-1] // 2
        joint = (lis[0] + lis[-1]) // 2
        print(top + bottom + joint * (k - 1) + mid * k)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
ans_s2, ans_s3 = 0, 0

# 例外処理
if len(S) == 1:
    print(K // 2)
    exit()
elif len(set(S)) == 1:
    print(len(S) * K // 2)
    exit()

S_double = S + S
for i in range(1, len(S_double)):
    if S_double[i] == S_double[i - 1]:
        S_double = S_double[: i] + "?" + S_double[i + 1:]
        ans_s2 += 1

S_triple = S + S + S
for j in range(1, len(S_triple)):
    if S_triple[j] == S_triple[j - 1]:
        S_triple = S_triple[: j] + "?" + S_triple[j + 1:]
        ans_s3 += 1

ans_increase = ans_s3 - ans_s2  #K>=3のときの増分

ans = ans_s2 + max(0, ans_increase * (K - 2))
print(ans)

Submission status: Accepted

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

sl=list(s)

cnt0=0
cnt1=0
cnt2=0

for i in range(len(sl)-1):
    if sl[i]==sl[i+1]:
        cnt0+=1
        sl[i+1]="?"
ss=list("".join(sl)+s)

for i in range(len(sl)-1, len(ss)-1):
    if ss[i]==ss[i+1]:
        cnt1+=1
        ss[i+1]="?"

sss=list("".join(ss)+s)
for i in range(len(ss)-1, len(sss)-1):
    if sss[i]==sss[i+1]:
        cnt2+=1
        sss[i+1]="?"


if cnt0!=cnt1 and cnt1!=cnt2:
    print(k//2*(cnt0+cnt1)+cnt0)
elif cnt0!=cnt1 and cnt1==cnt2:
    print(cnt0+(k-1)*cnt1)
else:
    print(k*cnt0)

Submission status: Accepted

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

if len(s) == 1:
    print(k // 2)
else: 
    x = 0
    if s[0] == s[-1]:
        # head
        h = 1
        for i in range(1,len(s)):
            if s[0] == s[i]:
                h += 1
            else:
                break
        # tail
        t = 1
        for i in range(len(s)-2, -1, -1):
            if s[-1] == s[i]:
                t += 1
            else:
                break
        if h == len(s):
            # s only consists of the same letter
            x = (h * k) // 2
            k = 1
        else:
            #x = ((h + t) // 2) * ((k + 1) // 2)
            x = (h // 2) + (t // 2) + ((h + t) // 2) * (k - 1)
        #print(111,s,k,h,t,x)
    else:
        h = t = 0
    #print(222,s,k,h,t)
    y = 0
    if (h + t) < len(s):
        m = s[h]
        n = 1
        for i in range(h+1, len(s)-t):
            if m == s[i]:
                n += 1
                if n == 2:
                    y += 1
                    #print(234,n,y)
                    n = 0
            else:
                m = s[i]
                n = 1
    #print(333,m,n,y,k)
    y *= k
    print(x + y)

Submission status: Accepted

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

S = input().strip()
K = int(input())
f = 0
if len(set(S)) == 1:
    print(len(S)*K//2)
else:
    c = 0
    first = 0
    _s = ''
    for s in S:
        if s != _s:
            first += c // 2
            c = 1
            _s = s
        else:
            c += 1
    c = 0
    middle = 0
    f = False
    _s = S[0]
    for s in S:
        if s != _s:
            if not f:
                f = True
                c = 1
                _s = s
            else:
                middle += c // 2
                c = 1
                _s = s
        else:
            c += 1
    c = 0
    bit = ''
    _s = ''
    for s in S:
        if _s == '':
            bit = s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    _s = ''
    for s in S[::-1]:
        if _s == '':
            bit += s
            _s = s
        elif s == _s:
            bit += s
        else:
            break
    if len(set(bit)) == 1:
        bit = len(bit) // 2
    else:
        bit = bit.count(bit[0]) // 2 + bit.count(bit[-1]) // 2
    c = 0
    last = 0
    _s = ''
    for s in S[::-1]:
        if s != _s:
            last += c // 2
            c = 1
            _s = s
        else:
            c += 1

    print(first + bit * (K - 1) + middle * (K - 2) + last)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
lst = []

cnt = 1
ans = 0
if (len(S) != 1):
  for i in range(len(S)-1):
    if (S[i] == S[i+1]):
      cnt += 1
      if (i == len(S)-2):
        lst.append(cnt)
    else:
      lst.append(cnt)
      cnt = 1
      
  if (len(lst) == 1):
    ans = len(S)*K // 2
  else:
    for i in lst:
      ans += i // 2
    ans *= K
    
    if (S[0] == S[-1] and lst[0] % 2 != 0 and lst[-1] % 2 != 0):
      ans += K-1
else:
  ans = K // 2
print(ans)