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 os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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


def solve(S, K):
    if len(set(list(S))) == 1:
        return K * len(S) // 2

    if K == 1:
        counts = [1] * len(S)
        cnt = 1
        for i, (c, d) in enumerate(zip(S, S[1:])):
            if c == d:
                cnt += 1
                counts[i] = 0
            else:
                counts[i] = cnt
                cnt = 1
        counts[-1] = cnt
        counts = np.array(counts, dtype=int)
        return (counts // 2).sum()

    S3 = S * 3
    counts = [1] * len(S3)
    cnt = 1
    for i, (c, d) in enumerate(zip(S3, S3[1:])):
        if c == d:
            cnt += 1
            counts[i] = 0
        else:
            counts[i] = cnt
            cnt = 1
    counts[-1] = cnt

    counts = np.array(counts, dtype=int)
    ans = (counts[:len(S)] // 2).sum()
    ans += (counts[len(S):len(S)*2] // 2).sum() * (K - 2)
    ans += (counts[len(S)*2:] // 2).sum()
    return ans


print(solve(S, K))

Submission status: Accepted

Solution code:
#import sys

#input = sys.stdin.readline

#import heapq
#import math
#import fractions

#def sosuhante(n):
#    for k in range(2, int(math.sqrt(n))+1):
#        if n% k ==0:
#            return False
#    return True

def main():
#    h,w,a,b = map(int, input().split())
    s = input()
    k= int(input())
    a= []
    b= []
    n=len(s)
    for i in range(n):
        a.append(s[i])
        b.append(s[i])

    a[0]="a"
    if n > 1:
        for i in range(1,n):
            if b[i]==b[i-1]:
                a[i]="a" if a[i-1]=="a" else "b"
            else:
                a[i]="b" if a[i-1]=="a" else "a"
#    print(a)
    temp=1
    henka=0
    if n > 1:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    henka+=temp//2
                    temp=1
            else:
                henka+=temp//2
                temp=1
    temp=1
    hasi=0
    mae=1
    usiro=1
    if k > 1 and n > 1 and b[0]==b[n-1]:
        for i in range(1,n):
            if a[i]==a[i-1]:
                temp+=1
                if i==n-1:
                    mae=temp
            else:
                mae=temp
                break
        temp=1
        for i in range(n-2,-1,-1):
            if a[i]==a[i+1]:
                temp+=1
                if i==0:
                    usiro=temp
            else:
                usiro=temp
                break
        if (mae+usiro)//2 > mae//2 + usiro//2:
            hasi=1
#    print(henka,"henka")
    count=0
    if k > 1 and n>1:
        count=henka*k + hasi*(k-1)
    if k == 1 and n>1:
        count=henka
    if n==1 and k==1:
        count=0
    if n==1 and k>1:
        count=k//2
    if mae==usiro and mae==n:
        count=(n*k)//2


    print(count)





if __name__ == "__main__":
    main()

Submission status: Accepted

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

A = ''
B = ''
C = ''

A_char = S[0]
C_char = S[-1]

for c in S:
    if c == A_char:
        A += c
    else:
        break

for c in S[::-1]:
    if c == C_char:
        C += c
    else:
        break

B = S[len(A):-len(C)]

# add End token
A += 'E'
B += 'E'
C += 'E'

def subst_counter(s):
    counter = 0
    tmp_counter = 1
    in_cont = False
    prev_char = ''
    for c in s:
        #print(prev_char)
        #print(tmp_counter)
        if c == prev_char:
            in_cont = True
            tmp_counter += 1
        elif c != prev_char and in_cont:
            counter += int(tmp_counter/2)
            tmp_counter = 1
            in_cont = False
            prev_char = c
        else:
            prev_char = c
    return counter

if B == 'E':
    if A == 'E': # ''
        print(0)
    elif A[0] == C[0]: # 'aaaa'
        N = (len(A) - 1)*K
        print(int(N/2))
    else: # 'aaaccc'
        N = subst_counter(A) + subst_counter(C)
        N = N * K
        print(N)
else:
    if A[0] == C[0]: # 'aaaxyzaaa'
        N_B = subst_counter(B)*K
        N_CA = int(((len(A)-1) + (len(C)-1))/2)*(K-1)
        print(N_B + N_CA + int((len(A)-1)/2) + int((len(C)-1)/2))
    else:
        N_A = subst_counter(A) * K
        N_B = subst_counter(B) * K
        N_C = subst_counter(C) * K
        print(N_A + N_B + N_C)

Submission status: Accepted

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

n = len(S)
start = 0
i = 0
while i < n and S[0] == S[i]:
    start += 1
    i += 1

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

lst = [start]
count = 1
tmp = i
i += 1
while i < n:
    if S[tmp] == S[i]:
        count += 1
        i += 1
    else:
        lst.append(count)
        count = 1
        tmp = i
        i += 1

lst.append(count)
# print (lst)
if S[0] != S[-1]:
    ans = 0
    for i in lst:
        ans += i // 2
    print (ans * K)
else:
    ans = (lst[0] + lst[-1]) // 2
    for i in lst[1:-1]:
        ans += i //2
    ans *= (K - 1)
    # print (ans)
    for i in lst:
        ans += i //2
    print (ans)

Submission status: Accepted

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

# 文字列分割
s = [[k, len(list(g))] for k, g in groupby(S)]

# 単一文字の場合は特別処理
if len(s) == 1:
#    print("単一文字")
    print(len(S) * K // 2)
    sys.exit()

# 単位文字列で置換する
s_count = sum(ss[1] // 2 for ss in s)
result = s_count * K

# 最初と最後の文字が同じで、かつ最初か最後の文字列が奇数だった場合の調整
# print(s[0], s[-1])
if s[0][0] == s[-1][0] and (s[0][1] % 2 == 1 and s[-1][1] % 2 == 1):
#    print("調整あり")
    result += K - 1

print(result)

Submission status: Accepted

Solution code:
import math
import copy 
S = list(input())
K = int(input())
S1 = copy.copy(S)

x = S[0]
c1 = 0
c2 = 0
for i in range(len(S)-1):
    if x == S[i+1]:
        c1 += 1
    else:
        c2 += math.ceil(c1/2)
        c1 = 0
    x = S[i+1]
c2 += math.ceil(c1/2)
a = c2

S.extend(S1)
x = S[0]
c1 = 0
c2 = 0
for i in range(len(S)-1):
    if x == S[i+1]:
        c1 += 1
    else:
        c2 += math.ceil(c1/2)
        c1 = 0
    x = S[i+1]

c2 += math.ceil(c1/2)
b = c2

S.extend(S1)

x = S[0]
c1 = 0
c2 = 0
for i in range(len(S)-1):
    if x == S[i+1]:
        c1 += 1
    else:
        c2 += math.ceil(c1/2)
        c1 = 0
    x = S[i+1]
c2 += math.ceil(c1/2)
c = c2

if K == 1:
    print(a)
elif b-a ==  c-b:
    print(a+(K-1)*(b-a))
else:
    print((K//2)*b+(K%2)*(c-b))

Submission status: Accepted

Solution code:
S=input()
K=int(input())
l=len(S)
a=0
s=S[0]
b,c=-1,-1
for i in range(1,l):
    if s!=S[i]:
        b=i
        a+=i//2
        break
s=S[-1]
if b!=-1:
    for i in range(l-2,-1,-1):
        if s!=S[i]:
            c=i
            a+=(l-i-1)//2
            break
if b==-1:
    print(l*K//2)
else:
    t=1
    s=S[b]
    for i in range(b+1,c+1):
        if S[i]==s:
            t+=1
        else:
            a+=t//2*K
            t=1
            s=S[i]
    a+=t//2*K
    if S[0]==S[-1]:
        a+=(b+l-c-1)//2*(K-1)
    else:
        a+=b//2*(K-1)
        a+=(l-i-1)//2*(K-1)
    print(a)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
s=list(input())
k=int(input())
cnt=1
cnt2=0
tmp=0
flg=0
flg2=0
flg3=0
for i in range(len(s)):
    if tmp==s[i]:
        cnt+=1
    else:
        cnt2+=cnt//2
        cnt=1
    tmp=s[i]
cnt2+=cnt//2

if len(s)==1:
    flg2=1
elif len(set(s))==1:
    flg3=1

elif s[0]==s[-1]:
    m=0
    cnt3=0
    while s[m]==s[0]:
        cnt3+=1
        m+=1
        if m==len(s)-1:
            cnt3+=1
            break
    n=1
    cnt4=0
    while s[-n]==s[0]:
        cnt4+=1
        n+=1
        if n==len(s):
            cnt4+=1
            break
    if cnt3%2==1 and cnt4%2==1:
        flg=1

if flg==1:
    ans=cnt2*k+k-1
elif flg2==1:
    ans=k//2
elif flg3==1:
    ans=(len(s)*k)//2
else:
    ans=cnt2*k
    
print(ans)

Submission status: Accepted

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

def a(SS):
    count = 0
    samestr = 0
    for i in range(len(SS)-1):
        if SS[i] == SS[i+1]:
            samestr += 1
        else:
            count += ((samestr+1)//2)
            samestr = 0
    count += ((samestr+1)//2)
    return count


if not len(set(S)) == 1:
    singlec = a(S)
    if K == 1:
        print(singlec)
    else:
        doublec = a(S+S)
        sac = doublec - singlec*2
        result = singlec*K + sac*(K-1)
        print(result)
else:
    print( (len(S)*K)//2 )

Submission status: Accepted

Solution code:
import math

s = list(input())
k =int(input())
cnt1 = 0
cnt2 = 0
ans = 0
i = 0
j = len(s)-1
p = False
n = 0
if len(s) == 1:
  answer = math.floor(k/2)
else:
  while s[0] == s[n]:
    n += 1
    if n == len(s)-1:
      p = True
      break
  if p == True:
    answer = math.floor(len(s)*k/2)
  if p == False:
    t = False
    while s[0] == s[i]:
      i+= 1
    cnt1 = i 
    while s[-1] == s[j]:
      j = j-1
    cnt2 = len(s)-j-1
    if s[0] == s[-1]:
      t = True
    new_s = s[cnt1:len(s)-cnt2]
    for i in range(len(new_s)-1):
        if new_s[i] == new_s[i+1]:
          new_s[i+1] = "0"
          ans += 1
    if t == True:
      ans_2 = math.floor((cnt1+cnt2)/2)
      answer = ans*k + ans_2*(k-1) + math.floor(cnt1/2) + math.floor(cnt2/2)
    if t == False:
      answer = ans*k + math.floor(cnt1/2)*k + math.floor(cnt2/2)*k
print(answer)

Submission status: Accepted

Solution code:
def repeted(S):
    for i in range(1,len(S)):
        if S[i]!=S[0]:
            return False
    return True

def repeted_first(S):
    res=1
    i=1
    while i<len(S) and S[i]==S[0]:
        res+=1
        i+=1
    return res

def repeted_last(S):
    res=1
    i=len(S)-2
    while i>=0 and S[i]==S[-1]:
        res+=1
        i-=1
    return res

def calc_t(S):
    res=0
    temp=1
    for i in range(1,len(S)):
        if S[i]==S[i-1]:
            temp+=1 # 連続する同じ文字の数
        else:
            res+=temp//2
            temp=1
    res+=temp//2
    return res # Sの最後まで見た後

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

if S[0]!=S[-1]:
    ans=calc_t(S)*K

elif repeted(S):
    ans=(len(S)*K)//2

else:
    rf=repeted_first(S)
    rl=repeted_last(S)
    ans=(calc_t(S)-rf//2-rl//2)*K+rf//2+((rf+rl)//2)*(K-1)+rl//2

print(ans)

Submission status: Accepted

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

first=0
for i in range(len(S)):
  if S[i]==S[0]:
    first+=1
  else:
    break
last=0
for i in range(len(S)):
  if S[-i-1]==S[0]:
    last+=1
  else:
    break
#print(first,last)
if first+last==2*len(S): #すべての文字が同じ
  num=(len(S)*K)//2
else:
  cont_n=1
  count=0
  char=S[0]
  for i in range(first, len(S)-last):
    #print(S[i],char,cont_n)
    if S[i]==char:
      cont_n+=1
    else:
      char = S[i]
      count += cont_n//2
      cont_n = 1
  count += cont_n//2
  num=K*count
  num += first//2
  num += last//2
  num += (K-1)*((first+last)//2)
print(num)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
s = input()
k = int(input())
c = 0
i = 0
if s[0]*len(s) == s:
    print(len(s)*k//2)
else:   
        
    while i<len(s)-1:
        if s[i] == s[i+1]:
            c += 1
            i += 2
        else:
            i += 1
    if s[0] == s[-1] and i==len(s)-1:
        s_ = s + s
        c0 = 0
        i = 0
        while i<len(s_)-1:
            if s_[i] == s_[i+1]:
                c0 += 1
                i += 2
            else:
                i += 1
        if 2*c == c0:
            print(k*c)
        else:
            c += 1
            print(k*c-1)
    else:
        print(k*c)

Submission status: Accepted

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

j = 1
while j<len(s) and s[j] == s[0]:
    j+=1
if j == len(s):
    print((len(s)*k)//2)
else:
    s += s[:j]
    s = s[j:]

    result = j//2

    i = 1
    while i < len(s):
        if s[i] == s[i-1]:
            result += k-1
            i += 1
        i += 1

    i = 1
    while i < len(s)-j:
        if s[i] == s[i-1]:
            result += 1
            i += 1
        i += 1

    print(result)

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

a = list(input())
n = int(input())
ans = 0
if n == 1:
    kkk = s(a)
    for i in range(len(kkk)):
        if kkk[i][1] >= 2:
            ans += kkk[i][1]//2
    print(ans)
else:
    aa = a+a
    kkk = s(aa)
    kk = s(a)
    if kkk[0][0] != kk[-1][0]:
        for i in range(1,len(kk)-1):
            ans +=kk[i][1]//2
        ans *= n
        ans += (kk[0][1]//2 + kk[-1][1]//2) * n
    else:
        if len(kkk) != 1:
            for i in range(1,len(kk)-1):
                ans +=kk[i][1]//2
            ans *= n
            ans += (kk[0][1] + kk[-1][1])//2*(n-1) + (kk[0][1]//2 + kk[-1][1]//2)
        else:
            ans = kk[0][1]*n//2


    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


repeated = input().strip()
num_repeat = int(input())

len_repeated = len(repeated)

if len_repeated == 1:
    print(num_repeat // 2)

elif len_repeated == 2:
    if repeated[0] == repeated[1]:
        print(num_repeat)
    else:
        print(0)

else:
    c = Counter(repeated)
    # characters are all same, like aaa
    if len(c) == 1:
        print((num_repeat * len_repeated) // 2)
    #
    else:
        arr = [1]
        for i in range(1, len_repeated):
            if repeated[i - 1] == repeated[i]:
                arr[-1] += 1
            else:
                arr.append(1)

        if repeated[0] == repeated[-1]:
            change_first = sum([i//2 for i in arr[:-1]])
            change_last = arr[-1] // 2
            elem = arr.pop(0) + arr.pop(-1)
            arr.append(elem)
            change_rep = sum([i//2 for i in arr])
            print(change_first + change_rep * (num_repeat-1) + change_last)

        else:
            print(sum([i//2 for i in arr]) * num_repeat)

exit(0)

Submission status: Accepted

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

# s='aaaabba'
# k=13

leng = len(s)
if(leng==1):
    print(k//2)
    exit()
elif(leng==2):
    if(s[0]==s[1]):
        print(k)
    else:
        print(0)
    exit()

if(s[:-1]==s[1:]):
    print(leng*k//2)
    exit()

ct_1 = 0
s1 = s
s2 = s+s
for i in range(1,leng):
    if(s1[i-1]==s1[i]):
        s1 = s1[:i] + '?' + s1[i+1:]
        ct_1 += 1

ct_2 = 0
for i in range(1,leng*2):
    if(s2[i-1]==s2[i]):
        s2 = s2[:i] + '?' + s2[i+1:]
        ct_2 += 1

ans = ct_1 + (ct_2 - ct_1)*(k-1)

print(ans)

Submission status: Accepted

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

num0 = 0
start = 0
end = len(S)
if S[0] == S[-1]:
    pre = S[0]
    for i, c in enumerate(S):
        if c != pre:
            start = i
            T = S[::-1]
            for j, d in enumerate(T):
                if d != pre:
                    end = len(S) - j
                    break
                num0 += 1
            break
        num0 += 1
    else:
        print(len(S) * K // 2)
        exit(0)

pre = ' '
num = 1
count = 0
for c in S[start: end]:
    if c == pre:
        num += 1
    else:
        count += num // 2
        pre = c
        num = 1
else:
    count += num // 2

ans = count * K + start // 2 + (num0 // 2) * (K - 1) + (len(S) - end) // 2
print(ans)

Submission status: Accepted

Solution code:
def check_subseq(s):
    subseq_list = []
    tmp_length = 1
    for i in range(1, len(s)):
        if s[i - 1] == s[i]:
            tmp_length += 1
        else:
            if tmp_length >= 2:
                subseq_list.append(tmp_length)
            tmp_length = 1
        
    if tmp_length >= 2:
        subseq_list.append(tmp_length)

    return subseq_list


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

if s[0] == s[-1]:
    if len(set(s)) == 1:
        ans = len(s) * k // 2
    else:
        i = 1
        while True:
            if s[i] != s[-1]:
                first_subseq_length = i
                break
            i += 1

        i = 1
        while True:
            if s[-(i + 1)] != s[0]:
                last_subseq_length = i
                break
            i += 1

        # print(first_subseq_length, last_subseq_length)
        s = s[first_subseq_length:] + s[:first_subseq_length]
        subseq_list = check_subseq(s)
        ans = sum(map(lambda x: x // 2, subseq_list)) * k
        ans += first_subseq_length // 2 + last_subseq_length // 2 - subseq_list[-1] // 2
else:
    subseq_list = check_subseq(s)
    ans = sum(map(lambda x: x // 2, subseq_list)) * k
print(ans)

Submission status: Accepted

Solution code:
s=input()
t=s*2
k=int(input())
c,d=1,0
if len(set(s))==1:
    print((len(s)*k)//2)
else:
    for i in range(1,len(s)):
        if s[i-1]==s[i]:
            c+=1
        else:
            d+=c//2
            c=1
    d+=c//2
    l,r,p,q=t[0],t[-1],0,0
    while s[0]==l:
        s=s[1:]
        p+=1
        if len(s)==0:
            break
    if len(s)!=0:
        while s[-1]==r:
            s=s[:-1]
            q+=1
            if len(s)==0:
                break
    if t[0]==t[-1]:
        print((k-1)*((p+q)//2)+d*k-(k-1)*(p//2+q//2))
    else:
        print(d*k)

Submission status: Accepted

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

def is_onaji(S):
    pre_moji = S[0]
    for i in range(1, len(S)):
        if S[i] == pre_moji:
            pre_moji = S[i]
            continue
        else:
            return False
    return True


def solve():
    S = input()
    K = int(input())
    N = len(S)

    if N == 1:
        print(K//2)
        return

    # 1回目
    base1 = 0
    pre_moji = S[0]
    for i in range(1, N):
        if S[i] == pre_moji:
            base1 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 1:
        print(base1)
        return

    # 2回目
    base2 = 0
    for i in range(0, N):
        if S[i] == pre_moji:
            base2 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 2:
        print(base1+base2)
        return

    # 3回目
    base3 = 0
    for i in range(0, N):
        if S[i] == pre_moji:
            base3 += 1
            pre_moji = "?"
        else:
            pre_moji = S[i]

    if K == 3:
        print(base1+base2+base3)
        return

    if base1 == base2 and base2 == base3:
        print(base1*K)
    elif base1 != base2 and base2 == base3:
        print(base1 + base2*(K-1))
    else:
        # base1とbase2が交互にくる
        print(base1*((K+1)//2) + base2*(K//2))


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
s = input()
k = int(input())
co1 = 0
co2 = 0
co3 = 0
ju = True

for i in range(len(s)-1):
    if s[i+1] == s[i] and ju:
        co1 += 1
        ju = False
    else:
        ju = True

s2=s+s
ju=True
for i in range(len(s2)-1):
    if s2[i+1] == s2[i] and ju:
        co2 += 1
        ju = False
    else:
        ju = True

s3 = s2+s
ju = True
for i in range(len(s3)-1):
    if s3[i+1] == s3[i] and ju:
        co3 += 1
        ju = False
    else:
        ju = True

addeven=co2-co1
addodd=co3-co2
print(co1+addeven*(k//2)+addodd*((k-1)//2))

Submission status: Accepted

Solution code:
from itertools import groupby

s = list(input())
ss = s * 2
k = int(input())
x = 0
y = 0
if len(s) == 1:
    print(k // 2)
elif s.count(s[0]) == len(s):
    print(len(s) * k //2)
else:  
    for key, value in groupby(s):
        x += len(list(value)) // 2
    for key, value in groupby(ss):
        y += len(list(value)) // 2
    print((y-x) * (k-1) + x)

Submission status: Accepted

Solution code:
# N, K = map(int, input().split())
S = input()
K = int(input())

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

# first one
pp = '#'
res1 = 0
for s in S:
    if pp == s:
        res1 += 1
        pp = '#'
    else:
        pp = s

if K >= 2:
    S = S + S
else:
    print(res1)
    exit(0)

# pp = S[-1] or '#'
res2 = 0
for s in S:
    if pp == s:
        res2 += 1
        pp = '#'
    else:
        pp = s

print(res2*(K-1)//2 + res1)

Submission status: Accepted

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

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

L = [i for i in S]


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

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

count2 = 0
if S[0] == S[-1]:
    L[0] = "A"
    count2 += 1
    tmp = L[0]
    for i in range(1, len(L)):
        if L[i] == tmp:
            count2 += 1
            L[i] = "A"
            tmp = L[i]
        else:
            tmp = L[i]
    if L[-1] == "A":
        print((K//2)*count2 + (K//2+K%2)*count)
    else:
        print(count + (K-1)*count2)
else:
    print(count * K)

Submission status: Accepted

Solution code:
def li():
    return list(map(input().split()))


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

    count = 0
    before = ""
    for si in s:
        if before == si:
            count += 1
            before = ""
        else:
            before = si

    count2 = 0
    before2 = ""
    for si in s + s:
        if before2 == si:
            count2 += 1
            before2 = ""
        else:
            before2 = si
    diff = count2 - count

    is_corner = before == s[0]

    all_same = True
    before2 = s[0]
    for si in s:
        if before2 != si:
            all_same = False
        before2 = si

    corner_count = k - 1 if is_corner else 0
    if len(s) == 1:
        print(k // 2)
    elif all_same:
        print(len(s) * k // 2)
    else:
        print(count + diff * (k - 1))
    # print(count, count2)

Submission status: Accepted

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

ans = 0

s = s+s
for i in range(1,len(s)):
  if s[i-1] == s[i]:
    s[i] = "0"
    if i>=len(s)//2:
      ans += k-1
    else:
      ans += 1
      
if len(set(list(t))) == 1 and len(t)%2 == 1:
  ans = len(t) * (k//2) + (k%2==1)*(len(t)//2)
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    s = input().strip()
    k = int(input())
    l = []
    text = s[0]
    for c in s[1:]:
        if text[-1] == c:
            text += c
        else:
            l.append(text)
            text = c
    l.append(text)
    cnt = 0
    if len(l) == 1:
        cnt = len(l[0]) * k // 2
    elif l[0][0] == l[-1][0]:
        cnt += len(l[0]) // 2 + len(l[-1]) // 2
        l[-1] += l[0]
        cnt += sum([len(t) // 2 for t in l[1:-1]]) * k
        cnt += len(l[-1]) // 2 * (k - 1)
    else:
        cnt = sum([len(t) // 2 for t in l]) * k
    print(cnt)
    # len(t) if i else i for i, j in enumerate(l)])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
	s = input()
	k = int(input())
	s_ = [[s[0],1]]
	ct = 0
	for i in s[1:-1]:
		if(s_[-1][0] == i):
			s_[-1][1] += 1
		else:
			s_.append([i,1])
	for i in s_:
		if(i[1] > 1):
			ct += math.ceil((i[1] - 1)/2)
	ct *= k
	if((len(s) - 1) % 2 == 1 and len(s_ ) == 1):
		print((len(s) - 1) * k // 2)
	elif(s[0] == s[-2] and s_[0][1] == s_[-1][1] and len(s_) != 1):
		print(ct + k - 1)
	else:
		print(ct)


def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

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

check = ""
check_cnt = 0
ans1 = 0
for i in range(len(s)):
    if check_cnt == 0:
        check = s[i]
        check_cnt += 1
        continue
    if check == s[i]:
        check_cnt += 1
        continue
    else:
        check = s[i]
        if check_cnt == 1:
            pass
        elif check_cnt%2 == 0:
            ans1 += check_cnt//2
        else:
            ans1 += (check_cnt - 1)//2
    check_cnt = 1
else:
    if check_cnt == 1:
        pass
    elif check_cnt%2 == 0:
        ans1 += check_cnt//2
    else:
        ans1 += (check_cnt - 1)//2
    
cnt2 = 0
cnt3 = 0

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

add = 0
if cnt2%2 == 1 and cnt3%2 == 1:
    add = 1
    
same = s[0]
for i in range(len(s)):
    if same == s[i]:
        continue
    else:
        break
else:
    t = len(s) * k
    if t%2 == 0:
        print(t//2)
    else:
        print((t-1)//2)
    exit()
    
print(ans1 * k + add * (k-1))

Submission status: Accepted

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

a = S[0]
c = 0
g = []

for i in range(len(S)):
    if a == S[i]:
        c += 1
    else:
        g.append(c)
        a = S[i]
        c = 1

g.append(c)

if len(g) == 1:
    c = len(S) * K / 2
elif S[0] == S[-1]:
    c = int(g[0] / 2)
    c += int(g[-1] / 2)
    c += int((g[0]+g[-1]) / 2) * (K-1)

    for i in range(1, len(g) - 1):
        c += int(g[i] / 2) * K
else:
    c = 0
    for i in g:
        c += int(i / 2) * K


print(int(c))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

s = ns()
n = len(s)
kind = len(set(list(s)))
k = ni()

if n == 1:
    print(k // 2)
elif kind == 1:
    print(n*k // 2)
else:
    # 2個つなげる
    s2 = s + s

    # 前半、後半の変える場所を記録
    prev = ""
    flag = True
    fst = 0
    lat = 0
    for i, si in enumerate(s2):
        if si == prev:
            if flag:
                if i < n:
                    fst += 1
                else:
                    lat += 1
                flag = False

            else:
                flag = True
        else:
            flag = True

        prev = si

    print(fst + lat * (k-1))

Submission status: Accepted

Solution code:
s=input()
k=int(input())
ds=0
dt=0
cnt=0
cnt2=0
flag=0
st=s+s
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    cnt2+=1
    cnt+=1
    if cnt==1:
      ds+=1
    if cnt==2:
      cnt=0
  else:
    cnt=0
cnt=0
if cnt2==len(s)-1:
  flag=1
for i in range(len(st)-1):
  if st[i]==st[i+1]:
    cnt+=1
    #print("ifi",i,cnt)
    if cnt==1:
      #print("i",i)
      dt+=1
    if cnt==2:
      cnt=0
  else:
    cnt=0
d=dt-ds
ans=ds+d*(k-1)
#print("ds",ds)
#print("dt",dt,st)
if flag==0:
  print(ans)
else:
  if k==1:
    print(ds)
  if k==2:
    print(dt)
  if k>=3 and k%2==0:
    print(dt*(k//2))
  if k>=3 and k%2==1:
    print(dt*(k//2)+ds)

Submission status: Accepted

Solution code:
import sys

S = input()
K = int(input())
N = len(S)
Ans = 0
i = 0
wcount = 0
Headcount = 1
Lastcount = 1
a = 0
b = N - 1

if N == 1:
  KK = K // 2
  Ans = Ans + KK
  print(Ans)
  sys.exit()

for p in range(0,N-1):
  if S[p] != S[p+1]:
    wcount = wcount + 1
    
if wcount == 0:
  KK = N * K // 2
  Ans = Ans + KK
  print(Ans)
  sys.exit()
  
while i < N-1:
  if S[i] == S[i+1]:
    Ans = Ans + 1
    i = i + 2
  else:
    i = i + 1

Ans = Ans * K

if S[N-1] == S[0]:
  while S[a] == S[a + 1]:
    Headcount = Headcount + 1
    a = a + 1
  while S[b] == S[b - 1]:
    Lastcount = Lastcount + 1
    b = b - 1  
    
  if Lastcount % 2 + Headcount % 2 == 2:
    Ans = Ans + K - 1

print(Ans)