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:
from math import floor

word = input()
num = int(input())
length=len(word)

char_before= ''
sum=0
subsum =0
count= 0
stack=[]

for index in range(length):
    char_now=word[index]
    if index == 0 :
        char_before = char_now
        char_first=char_now
        count = 1
        if length==1:
            char_last=char_now
            stack.append(count)
        continue
    
    if char_before == char_now :
        count = count + 1
    else:
        char_before = char_now
        stack.append(count)
        count = 1
    
    if index == length-1:
        char_last=char_now
        stack.append(count)

for i in stack:
    subsum= subsum + floor(i/2)
    
if (stack[0]%2)==1 and (stack[-1]%2)==1 and char_last==char_first and len(stack)>=2:
    sum =(subsum+1)*num -1
elif len(stack)==1:
    sum = floor(length*num/2)
else:
    sum =(subsum)*num

print(sum)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools, pdb
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf, eps, mod = 10 ** 20, 1.0 / 10**10, 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)
def _cumsum(l): return list(itertools.accumulate(l)) 
"""
なかのものと端のもの
両端が一緒かどうか
一緒なら、K-1個変える
中を変える/2切り下げ
まず、端が一緒か
"""
S = input()
K = _I()
ans = 0

if len(set(S)) == 1:
    print(int(math.floor(len(S)*K/2)))
    exit()

S2 = S*2
S3 = S*3

torn_list_1 = []
previous = 'hoge'
for s in S2:
    if s == previous:
        torn_list_1[-1] += 1
    else:
        torn_list_1.append(1)
        previous = s
torn_list_2 = []
previous = 'hoge'
for s in S3:
    if s == previous:
        torn_list_2[-1] += 1
    else:
        torn_list_2.append(1)
        previous = s

ans2 = 0
ans3 = 0
for t in torn_list_1:
    if t>=2:
        ans2+=int(math.floor(t/2))

for t in torn_list_2:
    if t>=2:
        ans3+=int(math.floor(t/2))

diff = ans3-ans2

ans = ans2 + (K-2)*diff
print(ans)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
l = [1]
for i in range(len(S) - 1):
  if S[i] == S[i+1]:
    l[-1] += 1
  else:
    l.append(1)
if len(l) == 1:
  ans = len(S) * K // 2
else:
  l_2 = []
  for i in range(len(l)):
    l_2.append(l[i] // 2)
  if S[-1] == S[0]:
    LtoF = (l[0] + l[-1]) // 2
    ans = l_2.pop(0)
    ans += l_2.pop(-1)
    ans += sum(l_2) * K
    ans += LtoF * (K - 1)
  else:
    ans = sum(l_2) * K
print(ans)

Submission status: Accepted

Solution code:
txt, n = [input() for i in range(2)]
n = int(n)

first_flg = True
present_char = txt[0]
present_cnt = 0
numlen_list = []

for x in txt:
  if x == present_char:
    present_cnt += 1
  else:
    first_flg = False
    numlen_list.append(present_cnt)
    present_cnt = 1
    present_char = x

numlen_list.append(present_cnt)
guruguru_flg = (txt[0] == txt[-1])


if len(numlen_list) == 1:
  print(numlen_list[0] * n // 2)
elif guruguru_flg:
  print(sum([x // 2 for x in numlen_list[1:-1]]) * n
        + (numlen_list[0] + numlen_list[-1]) // 2 * (n-1)
        + numlen_list[0] // 2
        + numlen_list[-1] // 2)
else:
  print(sum([x // 2 for x in numlen_list]) * n)

Submission status: Accepted

Solution code:
import math

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

if len(set(S)) == 1:
  print(math.floor(len(S)*K/2))
  quit()
    
first = S[0]

a = 0
for l in S:
  if l == first:
    a+=1
  else:
    break

last = S[-1]
b = 0
for l in S[::-1]:
  if l == last:
    b+=1
  else:
    break

prev = S[a]
c = 0

for l in S[a+1:-b]:
  if prev == l:
    c += 1
    prev = ""
  else:
    prev = l

d = 0
if first == last:
  d =  math.floor((a+b)/2)
else:
  d = math.floor(a/2) +  math.floor(b/2)
ans = math.floor(a/2) + d*(K-1) + c*K + math.floor(b/2)
print(ans)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
S = s.copy()
S_3 =s.copy()
cnt = 0
cnt_2 = 0
cnt_3 = 0


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

# --------------------------

for i in range(len(s)):
    S.append(S[i])

for i in range(1, len(S)):
    if S[i-1] == S[i]:
        cnt_2 += 1
        S[i] = "A"

# if k == 1:
#     print(cnt)
# else:
#     print(cnt + (cnt_2 - cnt) * (k-1))

# ---------------------------------------

for i in range(len(S)):
    S_3.append(S_3[i])

for i in range(1, len(S_3)):
    if S_3[i-1] == S_3[i]:
        cnt_3 += 1
        S_3[i] = "A"

# --------------------------------------

if k == 1:
    print(cnt)
elif k == 2:
    print(cnt_2)
elif k == 3:
    print(cnt_3)
elif cnt_2 - cnt == cnt_3 - cnt_2:
    print(cnt + (cnt_2 - cnt) * (k-1))
else:
    if k % 2 == 0:
        print(int(cnt + (cnt_2 - cnt) * (k / 2) + (cnt_3 - cnt_2) * (k / 2 - 1)))
    else:
        print(int(cnt + (cnt_2 - cnt) * ((k - 1) / 2) + (cnt_3 - cnt_2) * ((k - 1) / 2)))

Submission status: Accepted

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

def counter(s):
    count = 0
    for i,letter in enumerate(s):
        if i != 0:
            if letter == letter_prev:
                count += 1
                letter_prev = '0'
            else:
                letter_prev = letter
        else:
            letter_prev = letter
    return count    

if s.count(s[0]) == len(s):
    print(len(s)*k//2)
elif s[0] != s[-1]:
    print(counter(s)*k)
else:
    a = 0
    b = 0
    i = 0
    while s[i] == s[0]:
        i += 1
        a += 1
    i = 1
    while s[(-1)*i] == s[0]:
        i += 1
        b += 1
    print(counter(s)*k + (k-1)*((a+b)//2 - a//2 - b//2))

Submission status: Accepted

Solution code:

def func():
    if len(S) == 1:
        return int(K/2)
    
    cnt = 1
    pre_s = S[0]
    tup_list = []
    for s in S[1:]:
        if pre_s == s:
            cnt += 1
        else:
            tup_list.append([pre_s, cnt])
            cnt = 1
        pre_s = s
    else:
        tup_list.append([pre_s, cnt])

    if len(tup_list) == 1:
        return int(cnt* K/2)
    
    ext = 0
    if tup_list[0][0] == tup_list[-1][0]:
        a = tup_list.pop()[1]
        b = tup_list[0][1]
        ext = int(a/2) + int(b/2) - int((a+b)/2)
        tup_list[0][1] += a
        
    res = 0
    for _,cnt in tup_list:
        res += int(cnt/2) * K
    return res + ext
    
S = input()
K = int(input())
print(func())

Submission status: Accepted

Solution code:
s = [c for c in input()]
k = int(input())

import copy

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

elif len(s) == len(set(s)):
    print(0)
    
elif s[0] != s[-1]:
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    print(c*k)

else: # s[0]==s[-1]
    s2 = copy.copy(s)
    c = 0
    for i in range(1, len(s)):
        if s[i-1] == s[i]:
            c = c + 1
            s[i] = 'Z'
    if s[-1] == 'Z':
        print(c*k)
    else:
        c2 = 1
        s2[0] = 'Z'
        for i in range(1, len(s)):
            if s2[i-1] == s2[i]:
                s2[i] = 'Z'
                c2 = c2 + 1
        if s2[-1] == 'Z':
            if k % 2 == 0:
                print(int((c+c2)*(k/2)))
            else:
                print(int((c+c2)*((k-1)/2)+c))
        else:
            print(c + c2*(k-1))

Submission status: Accepted

Solution code:
import copy


def count(s):
    s = list(s)
    cnt = 0
    for i in range(1, len(s)):
        if s[i] == s[i-1]:
            s[i] = '*'
            cnt += 1
    return cnt


def main():
    S = input()
    K = int(input())
    if len(set(S)) == 1:
        return (len(S) * K) // 2

    else:
        n1 = count(S)
        n2 = count(S + S)
        return (K - 1) * (n2 - n1) + n1
            

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

Submission status: Accepted

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

sum = 0
x = 1
if L == 1:
  print(K//2)
elif L == 2 and S[0] == S[1]:
  print(K)
elif S[0] != S[-1]:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)*K
      x = 1
  sum += (x // 2)*K
  print(sum)
else:
  for i in range(0, L-1):
    if S[i] == S[i+1]:
      x += 1
    else:
      sum += (x // 2)
      break
  if x == L:
    print((L * K) // 2)
  else:
    y = x
    x = 1
    for i in range(y, L-1):
      if S[i] == S[i+1]:
        x += 1
      else:
        sum += (x // 2) * K
        x = 1
    sum += (x // 2)
    sum += ((x+y) // 2) * (K-1)
    print(sum)

Submission status: Accepted

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

a = 1
data = []
for i in range(n-1):
    if s[i] == s[i+1]:
        a += 1
    else:
        data.append(a)
        a = 1
data.append(a)

ans = 0

if s[0] != s[-1]:
    for d in data:
        ans += d//2
    ans *= k
elif len(data) == 1:
    ans = n*k//2
else:
    for i in range(1,len(data)-1):
        ans += data[i]//2
    ans *= k
    ans += data[0]//2 + data[-1]//2 + (data[0] + data[-1])//2 * (k-1)

print(ans)

Submission status: Accepted

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

#(方針)Sの「先頭(の連続)」と「末尾(の連続)」が同じ文字ならそこも連続に
#2連続→1回で置換できる,3連続→1回で置換できる,　「N連続→N//2回で置換できる」

A = [] #連続でちぎるようのリスト
count = 1
for i in range(n): 
 if(i != 0):
  if(S[i]== tmp): count += 1
  if((S[i] != tmp)or(i == n-1)):
    A.append([tmp,count])
    count = 1
 tmp = S[i]
if(n == 1): A.append([S[0],1])

ans = 0
for i in range(len(A)):
  if((i != 0)and(i != len(A)-1)):
   ans += (A[i][1]//2)*K
     
if(len(A) == 1):
   ans += (A[0][1]*K)//2  
elif(A[0][0] == A[-1][0]):
   ans += (A[0][1]//2) + (A[-1][1]//2) + ((A[0][1]+A[-1][1])//2)*(K-1) #最初の二項 : 結合後の先頭と末尾分 
elif(A[0][0] != A[-1][0]):
   ans += (A[0][1]//2)*K  
   ans += (A[-1][1]//2)*K
print(ans)

Submission status: Accepted

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

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

def nasu(L):
  ans = 0
  N = len(L)
  t = L[0]
  cnt = 1
  for i in range(1, N):
    if t == L[i]:
      cnt += 1
    else:
      ans += cnt // 2
      cnt = 1
    t = L[i]
  ans += cnt // 2
  return ans 

a = nasu(S)
b = nasu(S + S)

print(a + (b - a) * (K - 1))

Submission status: Accepted

Solution code:
from fractions import gcd
import math

mod = 1000000007


def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def cnt(s, k):
    s = s * k
    ans = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            s[i+1] = ' '
            ans += 1
    return ans


def main():
    s = [i for i in input()]
    k = int(input())
    if len(set(s)) == 1:
        print(k * len(s) // 2)
        return
    if k <= 100:
        ans = cnt(s, k)
        print(ans)
        return
    cnt50 = cnt(s, 50)
    cnt51 = cnt(s, 51)
    dif = cnt51 - cnt50
    print(cnt50 + (k - 50) * dif)

main()

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
H=S[0]

flag=0
if(S[0]==S[len(S)-1]):
    flag=1
count=0
if(flag==0):
    for i in range(len(S)-1):
        if(S[i]==S[i+1]):
            S[i+1]="#"
            count+=1        
    print(count*K)
    exit()

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

S1=S[:]
S2=S[:]+S[:]

count1=0
for i in range(len(S1)-1):
    if(S1[i]==S1[i+1]):
        S1[i+1]="#"
        count1+=1
count2=0
for i in range(len(S2)-1):
    if(S2[i]==S2[i+1]):
        S2[i+1]="#"
        count2+=1

print(count1+(K-1)*(count2-count1))

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
H=S[0]

flag=0
if(S[0]==S[len(S)-1]):
    flag=1
count=0
if(flag==0):
    for i in range(len(S)-1):
        if(S[i]==S[i+1]):
            S[i+1]="#"
            count+=1        
    print(count*K)
    exit()

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

S1=S[:]
S2=S[:]+S[:]
S3=S[:]+S[:]+S[:]
RS=S[:]

count1=0
for i in range(len(S1)-1):
    if(S1[i]==S1[i+1]):
        S1[i+1]="#"
        count1+=1
count2=0
for i in range(len(S2)-1):
    if(S2[i]==S2[i+1]):
        S2[i+1]="#"
        count2+=1
count3=0
for i in range(len(S3)-1):
    if(S3[i]==S3[i+1]):
        S3[i+1]="#"
        count3+=1

N=count3-count2
E=count2-N

while(True):
    if(RS[0]==H):
        RS.pop(0)
    elif(RS[len(RS)-1]==H):
        RS.pop(len(RS)-1)
    else:
        break
rcount=0
for i in range(len(RS)-1):
    if(RS[i]==RS[i+1]):
        RS[i+1]="#"
        rcount+=1

print(count1+(K-1)*(count2-count1))

Submission status: Accepted

Solution code:
from itertools import groupby
s = input()
k = int(input())
l = [[k, len(list(g))] for k, g in groupby(s)]
ans = 0
ans2 = 0
for i in range(len(l)):
  if l[i][1] > 1:
    ans += l[i][1] // 2
if len(l) > 1:
  if l[0][0] == l[-1][0]:
    if l[0][1] % 2 == 1 and  l[-1][1] % 2 == 1 :
        ans2 = k - 1
if len(s) == 1:
  print(k // 2)
elif len(l) == 1:
  #if l[0][1] % 2 == 1:
  #  print(((l[0][1] // 2) * k + (k - 1)))
  #else:
  print(len(s) * k // 2)
else:
  print(ans * k + ans2)

Submission status: Accepted

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

L = []
tmp = []
pre = ""
for s in S:
    if len(tmp) == 0:
        pre = s
        tmp.append(s)
    elif pre == s:
        tmp.append(s)
    else:
        L.append(tmp)
        pre = s
        tmp = [s]
L.append(tmp)

count = 0
for l in L:
    count += int(len(l) / 2)
    # print(l)
count *= int(K)
if len(L) == 1:
    if len(L[0]) == 1:
        count = int(int(K) / 2)
    elif len(L[0]) % 2 == 1:
        count += int(int(K) / 2)

elif L[0][0] == L[len(L)-1][0]:
    if len(L[0]) % 2 == 1 and len(L[len(L)-1]) % 2 == 1:
        count = count + int(K) - 1
print(count)

Submission status: Accepted

Solution code:
def main():
    s = str(input())
    N = int(input())
    st = []
    moji = s[0]
    a = len(s)
    count = 1
    for i in range(a-1):
        if moji == s[i+1]:
            count += 1
        else:
            st.append(count)
            moji = s[i+1]
            count = 1
    st.append(count)
    ans = 0
    if len(st) == 1:
        ans = count*N//2
    else:
        for i in st:
            ans += i//2
        ans *= N
        if s[0] == s[-1]:
            ans += ((st[0]+st[-1])//2-st[0]//2-st[-1]//2)*(N-1)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    for i in a:
        ans += (i//2)*K

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

    print(ans)

Submission status: Accepted

Solution code:
s = input()
S = list(s)
K = int(input())
cnt1 = 0
cnt2 = 0
if len(set(S)) == 1:
  print(len(S) * K // 2)
  exit()
for i in range(len(S)-1):
  if S[i] == S[i+1]:
    S[i+1] = "#"
    cnt1 += 1
SS = list(s + s)
for i in range(len(SS)-1):
  if SS[i] == SS[i+1]:
    SS[i+1] = "#"
    cnt2 += 1
print(cnt1 + (cnt2 - cnt1) * (K - 1))

Submission status: Accepted

Solution code:
S = input()
K = int(input())
lenS = len(S)
ans = 0
if S.count(S[0]) == lenS:
    ans = (lenS*K)//2
else:
    if S[0] == S[lenS - 1] and lenS > 1:
        if S[0] == S[1]:
            ind = 1
            while(S[ind+1] == S[0]):
                ind += 1
            if ind % 2 == 1:
                S = "*"+S[1:]
                ans += K

        else:
            ans += K-1
            if S[lenS-1] == S[lenS-2]:
                ans += 1
            S = S[:lenS-1]+"*"

    for i in range(lenS - 1):
        if S[i] == S[i + 1]:
            S = S[:i+1] + '*'+S[i+2:]
            ans += K
            # print(S)
            # print(i)
    if S[0] == S[lenS - 1]:
        ans += K-1

print(ans)

Submission status: Accepted

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

sumarr = list()
cnt = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        cnt += 1
    else:
        sumarr.append(cnt)
        cnt = 1
sumarr.append(cnt)

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


def to_cnt(num):
    if num < 2:
        return 0
    else:
        return int(num / 2)


ans = 0
if S[0] == S[len(S) - 1]:
    ans += to_cnt(sumarr[0])
    ans += to_cnt(sumarr[len(sumarr) - 1])
    connection = to_cnt(sumarr[0] + sumarr[len(sumarr) - 1])
    cnt = sum(map(to_cnt, sumarr[1: len(sumarr) - 1]))
    ans += cnt * K
    ans += connection * (K - 1)
else:
    ans += sum(map(to_cnt, sumarr)) * K

print(ans)

Submission status: Accepted

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

ans = 0
len_S = len(S)
a = b = 0
c = 1
same_letter = True
for i in range(len_S - 1):
    if S[i] == S[i + 1]:
        c += 1
    else:
        same_letter = False
        ans += c // 2
        if i == c - 1:
            a = c
        c = 1
b = c
ans += c // 2
if same_letter:
    ans = len_S * K // 2
else:
    ans *= K
    if S[0] == S[-1] and K >= 2 and (a % 2) * (b % 2) == 1:
        ans += K - 1
print(ans)

Submission status: Accepted

Solution code:
s,k=input(),int(input())
c = 0
r = 0
ss = 0
l = 1
for c1, c2 in zip(s, s[1:]):
    if c1 == c2:
        c += 1
    else:
        r += (c+1)//2
        if ss == 0:
            ss = c+1
        l = c+1
        c = 0
if c:
    r += (c+1)//2
    l = c+1
    if ss == 0:
        ss = c+1
r *= k
if len(s) == 1:
    r = k//2
elif ss == len(s):
    r = (k*ss)//2
elif s[0] == s[-1] and (ss*l) % 2:
    r+=(k-1)
print(r)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- Coding: utf-8 -*-

import sys


def main():
    S = input()
    K = int(input())
    ary = []
    ans = 0
    n = 1

    for i, s in enumerate(S[1:]):
        if s == S[i]:
            n += 1
        else:
            ary.append(n)
            n = 1
    if n != 1:
        ary.append(n)

    if S[0] == S[-1]:
        for num in ary[1:-1]:
            ans += num//2
        ans *= K
        if len(ary) == 1:
            ans += ary[0]*K//2
        elif len(ary) < 1:
            ans = K//2
        else:
            ans += (ary[0]+ary[-1])//2*(K-1)
            ans += ary[0]//2+ary[-1]//2
    else:
        for num in ary:
            ans += num//2
        ans *= K
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#A
s = input()
k = int(input())
s_sum = 0

s_f = 1
s_l = 1


if s[0] == s[-1]:
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                s_f += 1
            else:
                break
                
    
    for i in range(len(s)-1 , -1, -1):
        if i != len(s)-1:
            if s[i] == s[i+1]:
                s_l += 1
            else:
                break
                
    
                
                
    if s_f == len(s):
        print((k * len(s)) // 2)
        
    else:
        n = 1
        is_not_first = False
        for i in range(len(s)):
            if i != 0:
                if s[i] == s[i-1]:
                    n += 1
                else:
                    if not is_not_first:
                        is_not_first = True
                        n = 1
                    else:
                        if n != 1:
                            if n % 2 == 0:
                                s_sum += (n / 2)
                            else:
                                s_sum += ((n - 1)/ 2)
                            n = 1

        x1 = (s_f // 2 + s_l // 2) + s_sum * k
        x2 = ((s_f + s_l)//2) * (k-1)
        print(int(x1 + x2))
        
else:   
    n = 1
    for i in range(len(s)):
        if i != 0:
            if s[i] == s[i-1]:
                n += 1
            else:
                if n != 1:
                    if n % 2 == 0:
                        s_sum += (n / 2)
                    else:
                        s_sum += ((n - 1)/ 2)
                    n = 1
                    

        if i == (len(s) - 1):
            if n != 1:
                if n % 2 == 0:
                    s_sum += (n / 2)
                else:
                    s_sum += ((n - 1)/ 2)
                    

    print(int(s_sum * k))

Submission status: Accepted

Solution code:
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():]

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

con = list(nchars(S, 1))
#print(con)
if len(con)==1:
    print(int(len(S)*K/2))

else:
    cons = []
    for coni in con:
        cons.append(int(len(coni)/2))

    c1 = cons[0]

    c2 = 0
    for i in range(len(cons)):
        if i != 0 and i != len(cons)-1:
            c2 += cons[i]

    if con[0][0]==con[-1][0]:
        c3 = int(len(con[0]+con[-1])/2)
    else:
        c3 = int(len(con[0])/2) + int(len(con[-1])/2)

    c4 = int(cons[-1])

    print(int(c1 + c2*K + c3 *(K-1) + c4))

Submission status: Accepted

Solution code:
#from itertools import groupby

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2


s1=input()
k=int(input())

#s2=[[h, len(list(g))] for h, g in groupby(s1)]
s2=groupby(s1)

c=0
if len(s2)!=1:
    for i in range(len(s2)):
        if i!=0 and i!=len(s2)-1:
            c+=(s2[i][1]//2)*k

    if s2[0][0]==s2[-1][0]:
        c+=((s2[0][1]+s2[-1][1])//2)*(k-1)
        c+=(s2[0][1]//2+s2[-1][1]//2)
    else:
        c+=(s2[0][1]//2+s2[-1][1]//2)*k
else:
    c+=(s2[0][1]*k)//2

print(c)

Submission status: Accepted

Solution code:
s = input()
s = s + "1"
k = int(input())
ans = 0
lis = []
tem = 1
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    tem += 1
  else:
    lis.append(tem)
    tem = 1
if s[0] == s[-2]:
  t = lis[0]+lis[-1]
  for i in range(1,len(lis)-1):
    if lis[i] % 2 == 0:
      ans += lis[i]//2
    else:
      ans += (lis[i]-1)//2
  ans*= k
  if t%2 == 0:
    ans += (k-1)*t//2
  else:
    ans += (k-1)*(t-1)//2
  if lis[0]%2 == 0:
    ans += lis[0]//2
  else:
    ans += (lis[0]-1)//2
  if lis[-1]%2 == 0:
    ans += lis[-1]//2
  else:
    ans += (lis[-1]-1)//2
else:
  for item in lis:
    if item % 2 == 0:
      ans += item//2
    else:
      ans += (item-1)//2
  ans *= k
if len(lis) == 1:
  anse = (len(s)-1)*k
  if anse%2 == 0:
    ans = anse//2
  else:
    ans =(anse-1)//2
print(ans)

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-2<g[0])))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((g[0]*g[-2]%2*s[0]==s[-2])*~-k>>(len(s)-1==g[0])))

Submission status: Accepted

Solution code:
import re
 
s = input()
k = int(input())
 
frag = False
 
def nchars(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():]
        
s_ = list(nchars(s,1))
 
for i in s_[0]:
  s1 = i
for i in s_[-1]:
  s2 = i
 
if s1 == s2:
  frag = True
 
l = [len(i) for i in s_]
 
c = l[0]//2
 
c1 = 0
c2 = 0
c3 = 0

for i in range(len(l)):
  if i != len(l)-1:
    c1 += l[i]//2
  else:
    if not frag:
      c1 += l[i] // 2
    else:
      c2 = (l[0]+l[i])//2
      c3 = l[i]//2

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

elif not frag:
  print(c1*k)
else:
  print(c + (c1-c)*k + c2*(k-1) + c3)

Submission status: Accepted

Solution code:
import copy

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

#両端が異なる
def listCount(S,K):
    box = "" #１つ前の文字を格納
    count=0 #f(0):重複ペアをカウント

    for mozi in S:
        if box != mozi:
            box = mozi
        else:
            count += 1
            box=""
            
    return count
  
#両端が同じ全部同じ文字じゃない
def listCount_dash(S,K):
    box =""
    S_origin = copy.deepcopy(S)
    
    #Sの後ろから前まで試行
    for k in range(0,len(S)-1):
        #もし先頭と最後尾が同じならque
        if S[0] == S[len(S)-1]:
            S.append(S[0])
            S.pop(0)
        else:
            break
        
    return listCount(S,K)*(K-1) + listCount(S_origin,K)
  
#両端が全部同じ
def listCount_dou(S,K):
    S_origin = copy.deepcopy(S)
    S.extend(S)
    return listCount(S,K)*int(K/2) + listCount(S_origin,K)*(K%2)
  
flag = 1
box=S[0]
for mozi in S:
    if box != mozi:
        flag = 0
        break
    box = mozi

if flag==0 and S[0] != S[len(S)-1]:
    #両端が異なる場合
    ans = listCount(S, K)*K
elif flag==0:
    #両端が同じ場合
    ans = listCount_dash(S, K)
else:
    #文字が全部同じ
    ans = listCount_dou(S,K)
    
print(ans)

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+(g[0]*g[-2]%2*s[0]==s[-2])*~-k//2**(len(s)-1==g[0]))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+(g[0]*g[-2]%2*s[0]==s[-2])*(~-k//2**(len(s)-1==g[0])))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-(g[0]*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-(g[0]%2*g[-2]%2*s[0]==s[-2])*(-k//2**(len(s)-1==g[0])+1))

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k-((s[0]==s[-2])*g[0]%2&g[-2]%2)*(-k//2**(len(s)-1==g[0])+1))

Submission status: Accepted

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

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

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

    if S[0] == S[-1]:
        a, i =1, 1
        while S[0]==S[i]:
            a += 1
            i += 1
        b, i =1, 1
        while S[-1]==S[-1- i]:
            b += 1
            i += 1
        ans = ans - (a//2 +b//2 -(a+b)//2)*(K-1)        
    

print(ans)

Submission status: Accepted

Solution code:
from itertools import*
s,k=open(0)
k=int(k)
g=[len(list(v))for _,v in groupby(s)]
print(sum(c//2for c in g)*k+((s[0]==s[-2])*g[0]%2&g[-2]%2)*(k//2*(-len(s)==~g[0])or k-1))

Submission status: Accepted

Solution code:
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7

s = input()
k = INT()
n = len(s)

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

l=[]
tmp=1
ans=0
for i in range(1,n):
    if s[i-1]==s[i]:
        tmp+=1
    else:
        l.append(tmp)
        tmp=1
l.append(tmp)
for i in l:
    ans+=i//2
ans*=k

if s[0]==s[-1]:
    a = l[0]
    b = l[-1]
    res = (a//2) + (b//2) - ((a+b)//2)
    print(ans-(res*(k-1)))
else:
    print(ans)

Submission status: Accepted

Solution code:
S = list(input())
K = int(input())
# S = list("ccccc")
# K = 3
# S = list("aabca")
# K = 2
if S.count(S[0]) == len(S):
    print(len(S) * K // 2)
    exit()

cnt = 0
continuous = 0
chr = S[0]
for i in range(1, len(S)):
    if chr == S[i]:
        cnt += 1
    if chr != S[i] or i == len(S) - 1:
        continuous += (cnt + 1) // 2
        cnt = 0
    chr = S[i]
chr = S[0]
left = 0
idx = 0
while idx < len(S) and S[idx] == chr:
    left += 1
    idx += 1
idx = -1
right = 0
while idx < len(S) and S[idx] == chr:
    right += 1
    idx -= 1
same = right + left
if continuous == 0 and S[0] == S[-1]:
    print(K - 1)
else:
    ans = K * continuous
    if right % 2 == 1 and left % 2 == 1:
        ans += K - 1
    print(ans)

Submission status: Accepted

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

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

i = 0
while s[i] == s[i+1]:
    i += 1
    if i == n-1:
        print(n * k // 2)
        exit()

mode = 0
count = 0
ans = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        if mode == 0:
            count = 2
            mode = 1
        else:
            count += 1
    elif mode == 1:
        mode = 0
        ans += count // 2
        count = 0

if mode == 1:
    ans += count // 2

ans *= k

if s[0] == s[-1]:
    a = 1
    while s[a-1] == s[a]:
        a += 1
    b = n-1
    while s[b-1] == s[b]:
        b -= 1
    b = n - b
    ans -= ((a//2) + (b//2) - ((a+b) // 2))*(k-1)

print(ans)

Submission status: Accepted

Solution code:
def f(s, k):
    
    k = int(k)
    
    cnt = 0
    ss = s
    for i in range(len(ss)):
        if i == 0:
            continue

        if ss[i-1] == ss[i]:
            ss = ss[:i] + '\\' + ss[i+1:]
            cnt += 1
        
    cnt = cnt * k
    
    if s.count(s[0]) == len(s):
        if len(s) % 2 == 1:
            cnt = cnt + (k//2)
    else:    
        cntchk1 = 0
        cntchk2 = 0
        if s[0] == s[len(s)-1]:
            for i in range(len(s)):
                if s[len(s)-1] == s[len(s)-1 - i]:
                    cntchk1 += 1
#                 else:
#                     break
                if i < len(s)-1 and s[0] == s[i+1]:
                    cntchk2 += 1
                    
            if cntchk1 % 2 == 0 and cntchk1 % 2 == 0:
                cnt = cnt + (k -1)
                
    return str(cnt)

## Submit
print(f(input(),input()))