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:
def main():
    S = input()
    K = int(input())
    a = list(S)
    cnt = 0
    if len(set(a))==1:
        cnt = (len(a)*K)//2
    elif a[0] != a[-1]:
        for l in range(len(a)-1):
            if a[l+1] == a[l]:
                a[l+1] = str(l)
                cnt += 1
        cnt *= K
    else:
        temp = 1
        temp_2 = 1
        i = 0
        while a[i] == a[i+1]:
            temp += 1
            i += 1
        j = len(a) - 1
        while a[j] == a[j-1]:
            temp_2 += 1
            j -= 1
        for k in range(i+1,j-1):
            if a[k+1] == a[k]:
                a[k+1] = str(k)
                cnt += 1
        cnt *= K
        cnt += temp//2
        cnt += temp_2//2
        temp_3 = (temp + temp_2)//2
        cnt += temp_3 * (K-1)


    print(cnt)

    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
s=input()
t=int(input())
b=0
c=0
for i in range(c,len(s)-1):
  a=1
  for j in range(c+1,len(s)):
    if s[c]==s[j]:
      a+=1
    else:
      c=j
      break
  c=j
  b+=(a//2)
b=b*t
if s[0]==s[len(s)-1]:
  a=1
  c=1
  for k in range(len(s)-1):
    if s[k]==s[k+1]:
      c+=1
    else:
      break
  u=s[::-1]
  for l in range(len(s)-1):
    if u[l]==u[l+1]:
      a+=1
    else:
      break
  if a%2==1 and c%2==1:
    b+=t-1
w=s[::-1]
if w==s:
  if len(w)%2==1:
    b=t*len(w)//2
print(b)

Submission status: Accepted

Solution code:
S = input()

#K = 1
K = int(input())

mycnt = 0
mynum = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        mynum += 1
        
    else: 
        mycnt += mynum//2
        mynum = 1
    #print('mycnt=', mycnt)
if mynum > 1:
    mycnt += mynum//2 

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

elif S[0] != S[-1]:
    
    print(mycnt * K)

else:
    myleft = 1
    myright = 0
    mys = S[0]
    i = 1
    while S[i] == mys:
        myleft += 1
        i += 1
    
    myright = 0
    i = -1
    while S[i] == mys:
        myright += 1
        i -= 1
    #print(myleft, myright)
    print( (mycnt * K) - ( myleft//2 + myright//2 - (myright + myleft)//2 ) * ( K - 1 ) )

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = len(s)
if n == 1:
    if k == 1:
        print(0)
    else:
        print(k//2)
else:
    ans = []
    ss = 0
    i = 0
    while True:
        if i == n:
            break
        curr = s[i]
        temp = 1
        i += 1
        if i == n :
            break
        while s[i] == curr:
            temp += 1
            i += 1
            if i == n:
                break
        ans.append(temp)
    if len(ans) > 1:
        for i in ans:
            ss += (i//2)*k
        if s[0] == s[-1] and ans[0]%2 == ans[-1]%2 == 1:
            ss += k-1
    else:
        ss = n*k//2
    print(ss)

Submission status: Accepted

Solution code:
from itertools import*
s=input();k=int(input());x=0
g=[len(list(v))for k,v in groupby(s)]
for c in g:x+=c//2
x*=k
if s[-1]==s[0] and g[0]%2>0<g[-1]%2:
	if len(s)==g[0]:
		x+=k//2
	else:
		x+=k-1
print(x)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

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

li = []
point = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        point += 1
        if i == len(s)-2:
            li.append(point)
    else:
        li.append(point)
        point = 1

if s[-1] != s[-2]:
    li.append(1)

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

if s[0] != s[-1]:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

else:
    co = 0
    for i in range(len(li)):
        co += li[i]//2
    co *= k
    co -= ((li[0]//2+li[-1]//2)-(li[0]+li[-1])//2)*(k-1)
    print(co)

Submission status: Accepted

Solution code:
import sys

#文字列wordの文字の種類の構成の表現のためのリストを返す関数
def connection_count(word)->list:
    i=0
    a=[]
    count=1
    while i<(len(word)-1):
        while word[i]==word[i+1]:
            count+=1
            i+=1
            if i>=(len(word)-1):
                break
        a.append(count)
        count=1
        i+=1
    if a[-1]==1:
        a.append(count)
    return a

#S:文字列、K：繰り返す回数
S=input()
K=int(input())

#Sが一文字の場合(例外処理）
if len(S)==1:
    print(K//2)
    sys.exit()

#Sの文字の種類の構成を表現するためのリスト
connection=connection_count(S)

#すべての文字が同じ場合
if len(connection)==1:
    print(((connection[0]*K)//2))
#最初と最後の文字が異なる場合
elif S[0]!=S[-1]:
    connection2=list(map(lambda x:x//2,connection))
    print((sum(connection2)*K))
#最初と最後の文字が同じ場合（すべての文字が同じ場合を除く）
else:
    t=S*2
    connection2=list(map(lambda x:x//2,connection))
    connection3=list(connection_count(t))
    connection4=map(lambda x:x//2,connection3)
    a=sum(connection2)
    b=sum(connection4)
    ans=a+(b-a)*(K-1)
    print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

s = stdin.readline().rstrip()
k = int(stdin.readline().rstrip())

li = [len(tuple(s)) for _ , s in groupby(s)]

if s[0] == s[-1] and k>1:
    if len(li) == 1:
        print(li[0]*k//2)
        exit()
    else:
        co = 0
        for l in range(1,len(li)-1):
            co += li[l]//2
        co = co*k
        co += li[0]//2+li[-1]//2+((li[0]+li[-1])//2)*(k-1)
        print(co)
        exit()

else:
    co = 0
    for j in range(len(li)):
        co += li[j]//2
    print(co*k)
    exit()

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
cnt = 1
ans = 0
counter = []

if len(set(S)) == 1:
  print(len(S)*K//2)
  exit()
  
else:
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      cnt += 1
    else:
      counter.append(cnt)
      cnt = 1
  counter.append(cnt)
    
  for l in counter:
    ans += (l//2)*K

  if S[0] == S[-1]:
    a = counter[0]
    b = counter[-1]
    ans -= (a//2 + b//2 - (a+b)//2)*(K-1)
  
  print(ans)

Submission status: Accepted

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

if len(set(S)) == 1:
  print(len(S)*K//2)
  
else:
  cnt = 1
  counter = []
  for i in range(len(S)-1):
    if S[i] == S[i+1]:
      cnt += 1
    else:
      counter.append(cnt)
      cnt = 1
  counter.append(cnt)
  ans = 0
  for l in counter:
    ans += (l//2)*K

  if S[0] == S[-1]:
    a = counter[0]
    b = counter[-1]
    ans -= (a//2 + b//2 - (a+b)//2)*(K-1)
  
  print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import copy

# 入力 =stdin.readline().rstrip()
# 分割 = stdin.readline().rstrip().split()

'''
条件に合うものを絞り込む
data = [1, 2, 3]
s = [x for x in data if x % 3 == 0]
'''

'''
文字配列を数値に
l_si = ['-10', '0', '100']

l_si_i = [int(s) for s in l_si]
'''

'''
a = [2, 4, 6, 8]
b = [3, 6, 9]

print(list(set(a) | set(b)))  # 和集合
print(list(set(a) & set(b)))  # 積集合
'''

'''
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
count = 0
while len([x for x in A if x % 2 == 0]) == N:
    A = list(map(lambda y: y / 2, A))
    count += 1
print(count)
'''

'''
.append(要素)
.insert(場所,要素)
-1で末尾で末尾から一つ前
'''
'''
間に空白追加
' '.join(lis)
'''
T = stdin.readline().rstrip()
num = int(stdin.readline().rstrip())
midcount = 1
res = []
ch = ''
for t in range(len(T)):
    if (ch == T[t] and t != 0):
        midcount += 1
    elif (ch != T[t] and t != 0):
        res.append(midcount)
        midcount = 1
    if (ch == T[t] and t == len(T) - 1):
        res.append(midcount)
    ch = T[t]
if (len(T) == 1):
    res = [1]
if (len(res) == 1):
    print(str(int(num * len(T) / 2)))
elif (T[0] == T[len(T) - 1]):
    counter = 0
    tes = []
    tes.append(res[0] + res[len(res) - 1])
    tes += res[1: len(res) - 1]
    for x in tes:
        counter += int(x / 2)
    counter *= num - 1
    for x in res:
        counter += int(x / 2)
    print(counter)
else:
    counter = 0
    for x in res:
        counter += int(x / 2)
    counter *= num
    print(counter)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
s = input()
k = int(input())
# print (s,k)
n=len(s)
# print (s[0],s[n-1])

skip=0
if n == 1: 
    print((k-1) // 2 + (k-1) % 2 )
    sys.exit(0)

flag=0
for i in range(1,n):
    #print("loop",i)
   if s[i] != s[i-1]:
       flag =1
       break

if flag == 0: 
    print((k*n-1) // 2 + (k*n-1) % 2 )
    sys.exit(0)
p=0
q=0
if s[0] == s[n-1]:
    l=1 
    # Start 
    for i in range(1,n):
        #print("loop",i)
        if s[i] == s[i-1]:
            p +=1
        else:
            break
    if l < n:
        for i in range(n-2,0,-1):
            if s[i] == s[i+1]:
                q +=1
            else:
                break
    l = 1 + p + q
    # print("Ring ", l, p,q)
    op = ((l // 2) + (l % 2)) * (k-1)
    op += ((p // 2) + (p % 2)) 
    op += ((q // 2) + (q % 2))
else: 
    op = 0     

# print("OP",op)

if s[0] == s[n-1]:
    s2 = s[p+1:n-1-q] 
else:
    s2 = s
#print("s2",s2)
n=len(s2)
            

l = 0
for i in range(1,n):
    # print(s2[i],i,l,op)
    if s2[i-1] != s2[i]:
        op += (l // 2 + l % 2) * k
        l = 0 
    else:
        l += 1 
if l != 0:
    op += (l // 2 + l % 2) * k

        
print(op)

Submission status: Accepted

Solution code:
from collections import Counter
def main():
   s = input()
   k = int(input())
   li = []
   num = 1
   for i in range(len(s)-1):
       if s[i] != s[i+1]:
           li.append(num)
           num = 1
       else:
           num += 1
   li.append(num)
   temp = 0
   ans = 0
   if len(li) == 1:
       ans += len(s) * k // 2
   else:
       for i in li:
           temp += i//2
       if s[0] != s[-1]:
           ans = temp * k
       else:
           li[0] += li[-1]
           for i in li[:-1]:
               ans += i//2
           ans = ans * (k-1) + temp
   print(ans)
main()

Submission status: Accepted

Solution code:
from collections import Counter
def main():
   s = input()
   k = int(input())
   li = [1]
   for i in range(len(s)-1):
       if s[i] != s[i+1]:
           li.append(1)
       else:
           li[-1] += 1
   temp = 0
   ans = 0
   if len(li) == 1:
       ans += len(s) * k // 2
   else:
       for i in li:
           temp += i//2
       if s[0] != s[-1]:
           ans = temp * k
       else:
           li[0] += li[-1]
           for i in li[:-1]:
               ans += i//2
           ans = ans * (k-1) + temp
   print(ans)
main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5)


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

    c = S[0]
    count = [1]
    for s in S[1:]:
        if c == s:
            count[-1] += 1
        else:
            count.append(1)
            c = s

    if S[0] != S[-1]:
        ans = sum(map(lambda x: x // 2, count))
        ans *= K
        print(ans)
        return

    if len(count) == 1:
        ans = (count[0] * K) // 2
        print(ans)
        return

    ans = 0
    for c in count:
        ans += c // 2
    count[0] += count[-1]
    ans += sum(map(lambda x: x // 2, count[:-1])) * (K - 1)
    print(ans)


main()

Submission status: Accepted

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

    li = []
    num = 1
    for i in range(len(s)-1):
        if s[i] != s[i+1]:
            li.append(num)
            num = 1
        else:
            num += 1
    li.append(num)

    temp = 0
    ans = 0
    if len(li) == 1:
        ans += len(s) * k // 2
    else:
        for i in li:
            temp += i//2
        if s[0] != s[-1]:
            ans = temp * k
        else:
            li[0] += li[-1]
            for i in li[:-1]:
                ans += i//2
            ans = ans * (k-1) + temp
    print(ans)

main()

Submission status: Accepted

Solution code:
# AGC039A - Connection and Disconnection
from itertools import groupby


def main():
    S = input().rstrip()
    K = int(input())
    cnt = [len(tuple(s)) for _, s in groupby(S)]  # count of continuous characters
    if S[0] == S[-1] and K > 1:
        if len(cnt) == 1:  # one character
            ans = cnt[0] * K // 2
        else:  # first (= last) character in S + characters inside S
            ans = cnt[0] // 2 + cnt[-1] // 2 + (cnt[0] + cnt[-1]) // 2 * (K - 1)
            ans += sum(i // 2 for i in cnt[1:-1]) * K
    else:
        ans = sum(i // 2 for i in cnt) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import groupby


def solve(s, k):
    rle = [len(list(g)) for c, g in groupby(s)]
    if len(rle) == 1:
        return len(s) * k // 2
    ans = sum(l // 2 for l in rle) * k
    if s[0] == s[-1] and rle[0] % 2 == 1 and rle[-1] % 2 == 1:
        ans += k - 1
    return ans


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

Submission status: Accepted

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

def f(s):
    i = 0
    n = 0
    while i < len(s)-1:
        if s[i] == s[i+1]:
            n += 1
            i += 2
        else:
            i += 1
    return n

import collections

# 解説 AC のつもり
def main():
    S = input().strip()
    K = int(input())

    # S が全て同じ文字の場合
    if len(collections.Counter(S).keys()) == 1:
        print(len(S)*K//2)
        return

    # S について、変更が必要な回数
    n = f(S)

    # 末尾が異なる場合、n を K 倍すればよい
    if S[0] != S[-1]:
        print(n*K)
        return

    # 以降、末尾が同じ文字の場合

    # 末尾から連続して S[0] が現れる数
    a = 0
    for c in S[::-1]:
        if c == S[0]:
            a += 1
        else:
            break
    # 先頭から連続して S[0] が現れる数
    b = 0
    for c in S:
        if c == S[0]:
            b += 1
        else:
            break

    # まず（接続部を気にせず） n*K とした後、
    ans = n*K
    # K-1 個の S の接続部に対して計算し直す。
    # （S 単体で見たときの変更数を引き、S+S で見たときの変更数を足す）
    ans += (K-1) * (-(a//2) -(b//2) + (a+b)//2)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

import sys
if len(s) == 1:
    if k == 1:
        print(0)
    else:
        print(k // 2)
    sys.exit(0)

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

count = 0
seq_len = 1
seq_len_array = []
for i in range(0, len(s) - 1):
    if s[i] != s[i+1]:
        seq_len_array.append(seq_len)
        seq_len = 1
    else:
        seq_len += 1
seq_len_array.append(seq_len)
count = sum(s // 2 for s in seq_len_array)
count = count * k

if s[0] == s[-1]:
    count = count - \
            (seq_len_array[0] // 2 + seq_len_array[-1] // 2) * (k - 1) + \
            ((seq_len_array[0] + seq_len_array[-1]) // 2) * (k - 1)
print(count)

Submission status: Accepted

Solution code:
def main():
    S = input()
    K = int(input())
    N = len(S)
    ans = 0
    from collections import Counter
    if N == 1:
        return print(K//2)
    tmp = 1
    for i in range(1,N):
        if S[i-1] == S[i]:
            tmp += 1
        else:
            ans += tmp // 2
            tmp = 1
    else:
        if tmp == N:
            ans = N*K//2
        else:
            ans += (tmp // 2)
            ans *= K
            if S[0] == S[-1]:
                first = 1
                for i in range(1,N):
                    if S[i-1] != S[i]:
                        break
                    first += 1
                last = 1
                for i in range(N-1,0,-1):
                    if S[i-1] != S[i]:
                        break
                    last += 1
                revise = first//2 + last//2 - (first+last)//2
                revise *= (K-1)
                ans -= revise
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# AGC039A - Connection and Disconnection
from itertools import groupby


def main():
    S = input().rstrip()
    K = int(input())
    cnt = [len(tuple(s)) for _, s in groupby(S)]  # count of continuous characters
    if S[0] == S[-1] and K > 1:
        if len(cnt) == 1:  # one character
            ans = cnt[0] * K // 2
        else:  # first (= last) character in S + characters inside S
            ans = cnt[0] // 2 + cnt[-1] // 2 + (cnt[0] + cnt[-1]) // 2 * (K - 1)
            ans += sum(i // 2 for i in cnt[1:-1]) * K
    else:
        ans = sum(i // 2 for i in cnt) * K
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
S = list(input())[: -1]
ls = S * 2
K = int(input())
if len(S) == 1:
  print(K // 2)
  exit(0)
if S.count(S[0]) == len(S):
  print(len(S) * K // 2)
  exit(0)
left = 0
mid = 0
right = 0
last = 0
i = 0
while i < len(S):
  if ls[i] == ls[i + 1]:
    left += 1
    if i == len(S) - 1:
      last = 1
    i += 1
  i += 1
i = 0 + last
while i < len(S):
  if ls[i] == ls[i + 1]:
    mid += 1
    i += 1
  i += 1
i = 0 + last
while i < len(S) - 1:
  if ls[i] == ls[i + 1]:
    right += 1
    i += 1
  i += 1
if K == 1:
  i = 0
  res = 0
  while i < len(S) - 1:
    if ls[i] == ls[i + 1]:
      res += 1
      i += 1
    i += 1
  print(res)
else:
  print(left + mid * max(K - 2, 0) + right)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

from itertools import groupby
S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(K*len(S) // 2)
else:
    counts = [len(list(g)) for k, g in groupby(S)]
    if S[0] == S[-1] and counts[0] % 2 == counts[-1] % 2 == 1:
        t = 1
        counts[0] += counts.pop()
    else:
        t = 0
    print(K*sum(map(lambda x: x//2, counts)) - t)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

from itertools import groupby
S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(len(S)*K // 2)
else:
    counts = [len(list(g)) for k, g in groupby(S)]
    if S[0] != S[-1]:
        t = 0
    else:
        t = 1 if counts[0] % 2 == counts[-1] % 2 == 1 else 0
        counts[0] += counts.pop()
    print(K*sum(map(lambda x: x//2, counts)) - t)

Submission status: Accepted

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

def replacePoint(s):
    arr = [-1]
    for i in range(len(s)):
        if arr[-1] != i-1:
            if s[i] == s[i-1]:
                arr.append(i)
    arr.remove(-1)
    return arr

double = s*2
triple = s*3
arr = replacePoint(s)
darr = replacePoint(double)
tarr = replacePoint(triple)
a = len(arr)
b = len(darr) - len(arr)

if arr+[i+len(s) for i in arr] == darr:
    print(a*k)
elif darr+[i+len(s)*2 for i in arr] == tarr:
    print(a*((k+1)//2)+b*(k//2))
else:
    print(a+b*(k-1))

Submission status: Accepted

Solution code:
def compress(s):
    z = []
    cont = 1
    for ca, cb in zip(s, s[1:]):
        if ca == cb:
            cont += 1
        else:
            z.append(cont)
            cont = 1
    z.append(cont)
    # print(z)
    return z


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

    z1 = compress(s)
    if len(z1) == 1:
        # like 'aaa'
        return n * k // 2

    k1 = sum(map(lambda x: x // 2, z1))
    if s[0] != s[-1]:
        # like 'ab'
        return k1 * k
    else:
        # like 'aba' / 'abaa'
        z2 = compress(s * 2)
        k2 = sum(map(lambda x: x // 2, z2))
        d = k2 - k1
        return k1 + d * (k - 1)


if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

    if len(set(S)) == 1:
        print(len(S)*K//2)
    else:
        counter = []
        count = 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                count += 1
            else:
                counter.append(count)
                count = 1
        counter.append(count)
        ans = 0
        for c in counter:
            ans += c // 2
        ans *= K
        if S[0] == S[-1]:
            ans = ans - (counter[0] // 2 + counter[-1] // 2 - (counter[0] + counter[-1]) // 2) * (K - 1)
        print(ans)

if __name__ == '__main__': main()

Submission status: Accepted

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

if len(set(s))==1:
	print((len(s)*k)//2)
	sys.exit()
    
def counter(string):
    count = 0
    answer = 0
    prev = ""
    for index,sss in enumerate(string):
        if prev == "" or sss == prev:
            count+=1
            if count == 2: 
                answer+=1
                count=0
                string[index] = "1"
        else:
            count=1
        prev = sss
    return answer,string

answer, string = counter(s.copy())

if string[0] != string[-1]:
    print(answer*k)
else:
    count = 0
    l = len(s)
    for i in range(l):
        if s[i] == s[0]:
            count += 1
        else:
            break
    for i in range(l):
        if s[-1-i] == s[0]:
        	count += 1
        else:
        	break

    if count % 2 ==0:
        print(answer*k + k-1)
    else:
        print(answer*k)

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(len(S)*K // 2)
else:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    if S[0] != S[-1]:
        t = 0
    else:
        if counts[0] % 2 == counts[-1] % 2 == 1:
            t = 1
        else:
            t = 0
        counts[0] += counts.pop()
    print(K*sum(map(lambda x: x//2, counts)) - t)

Submission status: Accepted

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

import re
def nchars(s, n):
    """文字列 s に、同じ文字が n 個以上連続している部分文字列を見つける
    """

    assert n > 0
    reg = re.compile("(.)\\1{%d,}" % (n - 1))  # カンマを取ると n 個ちょうどになる
    while True:
        m = reg.search(s)
        if not m:
            break
        yield m.group(0)
        s = s[m.end():]
        
if len(s)==1:
    print(k//2)
elif len(s)==2:
    if len(set(s))==1:
        print(k)
    else:
        print(0)
elif len(s)==3:
    if len(set(s))==1:
        print(3*k//2)
    elif len(set(s))==2:
        if s[-1]==s[-(i+1)]:
            print(k)
        else:
            print(k-1)
elif len(set(s))==1 :
	print(len(s)*k//2)
else:
    if s[0]!=s[-1]:
        a=list(nchars(s, 2))
        b=0
        for i in a:
            b+=len(i)//2
        print(b*k)
    else:
        if s[0]!=s[1] and s[-1]!=s[-2]:
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            print(b*k+k-1)
        else:
            chain_hajime=0
            chian_owa=0
            for i in range(len(s)):
                if s[0]==s[i]:
                    chain_hajime+=1
                else: break
            for i in range(len(s)):
                if s[-1]==s[-(i+1)]:
                    chian_owa+=1
                else: break
            a=list(nchars(s, 2))
            b=0
            for i in a:
                b+=len(i)//2
            if chian_owa%2==1 and chain_hajime%2==1:
                print(b*k+k-1)
            else:
                print(b*k)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
if len(set(s))==1:
    print(len(s)*k//2)
    exit(0)
ans=0
tmp=0
before=s[0]
t=0
flag=0
tmp2=0
hoge=0
for i in range(len(s)-1):
    if before==s[i+1]:
        t+=1
    else:
        if flag==0:
            hoge+=t
            flag=1
            tmp+=-(-t//2)
        elif i==len(s)-2:
            hoge+=t
            tmp+=-(-t//2)
        else:
            tmp+=-(-t//2)
            tmp2+=-(-t//2)
        t=0
        before=s[i+1]
tmp+=-(-t//2)
hoge+=t
tmp2+=-(-(hoge+1)//2)
if s[0]==s[-1]:
    print(tmp2*(k-1)+tmp)
else:
    print(tmp*k)

Submission status: Accepted

Solution code:
from itertools import groupby

s = input()
k = int(input())
r = len(s) * k // 2
a = [sum(1 for _ in g) & 1 for _, g in groupby(s)]
if len(a) > 1:
  r -= sum(a) * k // 2
  if s[0] == s[-1] and a[0] & a[-1]:
    r += k - 1
print(r)

Submission status: Accepted

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

    if len(set(S)) is 1:
        print(len(S) * K // 2)
        return

    i = 0
    cnt = [1]
    while i < len(S) - 1:
        if S[i] is S[i + 1]:
            cnt[-1] += 1
        else:
            cnt.append(1)
        i += 1

    for v in cnt:
        ans += (v // 2) * K

    if S[0] is S[-1]:
        if cnt[0] % 2 == cnt[-1] % 2 == 1:
            ans += K - 1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

S = inputs[0].strip()
K = int(inputs[1])

if len(set(S)) == 1:
    print(len(S)*K // 2)
else:
    counts = []
    count = 1
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            count += 1
        else:
            counts.append(count)
            count = 1
    counts.append(count)
    if S[0] != S[-1]:
        print(K*sum(map(lambda x: x//2, counts)))
    else:
        if counts[0] % 2 == 1 and counts[-1] % 2 == 1:
            t = 1
        else:
            t = 0
        counts[0] += counts.pop()
        print(K*sum(map(lambda x: x//2, counts)) - t)

Submission status: Accepted

Solution code:
import copy

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

def Scount():
    cnt_S = 0
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = "1"
            cnt_S += 1
    return cnt_S

T = copy.copy(S)
T.insert(0, T[len(T)-1])
cnt_T = 0
for i in range(len(T)-1):
    if T[i] == T[i+1]:
        T[i+1] = "1"
        cnt_T += 1

if T[i+1] != "1":
    ans = Scount() + (cnt_T * (K-1))
else:
    TT = S * 2
    cnt_T = 0
    for i in range(len(TT)-1):
        if TT[i] == TT[i+1]:
            TT[i+1] = "1"
            cnt_T += 1
    if K % 2 == 0:
        ans = cnt_T * (K // 2)
    else:
        ans = (cnt_T * (K // 2)) + Scount()
print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    K = int(input())

    ans = 0
    if len(S) == 1:
        return K // 2
    
    if len(set(S)) == 1:
        return len(S) * K // 2
    
    l, r = 0, 0
    if S[0] == S[-1]:
        while len(set(S[0:l+1])) <= 1:
            l += 1
        while len(set(S[-r-1:])) <= 1:
            r += 1
    
    cnt = 0
    T = S[0]
    for s in S[1:]:
        if s == T[-1]:
            cnt += 1
            T += '_'
        else:
            T += s
    
    ans = cnt * K
    if max(l, r) >= 1:
        ans -= (l//2 + r//2 - (l+r)//2) * (K-1)
    
    return ans

if __name__ == '__main__':
    print(main())

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)]
#print(a)

ans = 0
chan = 0
if len(a) == 1:
  ans = (len(s)*k)//2
  print(ans)
else:
  for i in range (len(a)):
    ans = ans + a[i]//2
  if s[0] == s[-1]:
    chan = (a[0]+a[-1])//2 - a[0]//2 - a[-1]//2
  print(ans*k+chan*(k-1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
s = input().rstrip()
k = int(input())

def check_count(s):
    cnt_list = []
    cnt = 1
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            cnt += 1
        else:
            cnt_list.append(cnt)
            cnt = 1
    if cnt>1:
        cnt_list.append(cnt)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c//2
    return(_ans * k)

if len(s)==1:
    ans = k//2
else:
    cnt_list = check_count(s)
    if len(cnt_list)==1:
        ans = (len(s)*k)//2
    else:
        ans = calc_ans(cnt_list, k)
        if s[0]==s[-1]:
            if cnt_list[0]%2==1 and cnt_list[-1]%2==1:
                ans += k - 1

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
 
s = input().rstrip()
k = int(input())
    
def check_count(s):
    cnt_list = [1]
    cnt = 1
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            cnt_list[-1] += 1
        else:
            cnt_list.append(1)
    return cnt_list

def calc_ans(cnt_list, k):
    _ans = 0
    for c in cnt_list:
        _ans += c//2
    return(_ans * k)

if len(s)==1:
    ans = k//2
else:
    cnt_list = check_count(s)
    if len(cnt_list)==1:
        ans = (len(s)*k)//2
    else:
        ans = calc_ans(cnt_list, k)
        if s[0]==s[-1]:
            if cnt_list[0]%2==1 and cnt_list[-1]%2==1:
                ans += k - 1

print(ans)

Submission status: Accepted

Solution code:
ls=list(input())
n=int(input())

end_str=ls[-1]
divide_idx=0
for i in range(len(ls)-1,0,-1):
    if ls[i]!=end_str:
        divide_idx=i+1
        break
#print(divide_idx)
#print(ls[divide_idx:])
#print(ls[:divide_idx])

start_ls=ls[:divide_idx]
middle_ls=ls[divide_idx:]+ls[:divide_idx]
end_ls=ls[divide_idx:]

#print(start_ls,middle_ls,end_ls)

def get_count(req_list):
    ls=req_list[:]
    count=0
    for i in range(len(ls)-1):
        a,b=ls[i],ls[i+1]
        if a==b:
            ls[i+1]='!'
            count+=1
    return count

start_counts=get_count(start_ls)
middle_counts=get_count(middle_ls)*(n-1)
end_counts=get_count(end_ls)
#print(start_counts,middle_counts,end_counts)
if divide_idx==0:
    print(len(ls)*n//2)
else:
    print(start_counts+middle_counts+end_counts)

Submission status: Accepted

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

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

x = 1
for i in range(n):
    if i == n-1:
        l.append(x)
        continue
    
    if s[i] == s[i+1]:
        x += 1
    else:
        l.append(x)
        x = 1

ans = 0
for i in l:
    ans += i // 2
ans *= k
if l[0] % 2 and l[-1] % 2 and s[0] == s[-1]:
    ans += k-1

print(ans)