Problem:


Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii.  For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997



Sample input:
issii
2


Sample output:
4

Submission status: Accepted

Solution code:
s=list(input())
K=int(input())
S=s.copy()

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

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

lc=0
if ju==0 and S[0]==S[-1]:
  for i in reversed(range(len(S))):
    if S[i]==S[0]:
      lc+=1
    else:
      break
  
if S[0]==S[1]:
  S[0]="#"
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    S[i]="#"

ans=S.count("#")
if pc%2==1 and pc>1:
  ans-=1
  
  
if ju==1 and len(S)%2==1:
  print((len(S)*K)//2)
  exit()
  
if s[-1]==s[0] and s[1]!=s[0] and s[-2]!=s[0]:
  print(K*ans+(K-1))
  exit()


if S[0]=="#" and pc%2==1 and lc%2==1 and ju==0:
  print(ans*K+K-1)
  exit()


print(ans*K)

Submission status: Accepted

Solution code:
from itertools import groupby

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

Submission status: Accepted

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

    c = []
    
    i = 0
    if S[0] == S[-1]:
        connected = True
    else:
        connected = False

    while i < len(S):

        char = S[i]

        cnt = 1
        cnt2 = 1
        for j in range(i + 1, len(S) * 2):
            i = j
            if char == S[j % len(S)]:
                if j >= len(S):
                    cnt2 += 1
                else:
                    cnt += 1
                    cnt2 +=1
            else:
                if j-1 < len(S):
                    cnt2 = 0
                    i = j
                else:
                    i = len(S)
                break
        
        if cnt >= 2:
            c.append([char, cnt, cnt2])
        else:
            c.append([char, 0, cnt2])
    
    #  print(c)

    ans = 0
    flg = True
    for elem in c:
        if connected:
            if flg and len(c) >= 2:
                flg = False
                continue
            if len(c) == 1:
                ans = elem[1] * K // 2 
            elif elem[2] >= 2:
                ans += (elem[2] // 2) * (K - 1)  + (elem[1] // 2) + c[0][1] // 2
            else:
                ans += (elem[1] // 2) * K

        else:
            ans += (elem[1] // 2) * K
    
    if len(S) == 1:
        ans = K // 2
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
s=list(input())
ss=s[:]
k=int(input())
if len(s)==1: 
    print(k//2)
elif len(set(ss))==1:
        print((k*len(s))//2)    
else:
    count1=0
    for i in range(len(s)-1):
        if s[i]==s[i+1]:
            s[i+1]='0'
            count1+=1
    count2=0
    ss=ss+ss
    for i in range(len(ss)-1):
        if ss[i]==ss[i+1]:
            ss[i+1]='0'
            count2+=1
    print(count1+(k-1)*(count2-count1))

Submission status: Accepted

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

# now_s = S[0]
# for i in range(1, len(S)):
#     if now_s == S[i]:
#         cnt += 1
#     now_s = S[i]

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


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

if len(ruisekiwa) == 1:
    if ruisekiwa[0] % 2 == 0:
        print(ruisekiwa[0]//2*K)
        exit()

    # 文字数が奇数
    if K % 2 == 0:
        cnt += (ruisekiwa[0]//2 + 1) * (K//2)
        cnt += (ruisekiwa[0]//2) * (K//2)
    else:
        cnt += (ruisekiwa[0]//2) * (K//2 + 1)
        cnt += (ruisekiwa[0]//2 + 1) * (K//2)
    print(cnt)
    exit()



cnt = 0
for i in range(len(ruisekiwa)):
    cnt += ruisekiwa[i]//2

# 先頭と末尾が違うなら問題ない
if S[0] != S[-1]:
    print(cnt*K)
    exit()




# 先頭と末尾が同じ場合を考える

# 先頭が偶数, 末尾も偶数回数
if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()
# 先頭が偶数, 末尾が奇数
if ruisekiwa[0] % 2 == 0 and ruisekiwa[-1] % 2 == 1:
    print(cnt * K)
    exit()
# 末尾が偶数なら末尾を変えれるから嬉しい
# 先頭が奇数, 末尾が偶数
if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 0:
    print(cnt * K)
    exit()

# 偶は先頭を変える必要があるから、嬉しい

# 先頭が奇数回数続いていて, 末尾も奇数回数
if ruisekiwa[0] % 2 == 1 and ruisekiwa[-1] % 2 == 1:
    # if K % 2 == 1:
    #     print(cnt * (K//2+1) + (cnt+1) * (K//2))
    # else:
    #     print(cnt * (K//2) + (cnt+1) * (K//2))
    # exit()
    print((cnt+1) * K -1)
    exit()
    # ここを直した, waが減った

Submission status: Accepted

Solution code:
def p(st):
    l=len(st)
    tc=1
    ac=0
    p=s[0]
    for i in range(1,l):
        if st[i]==p:
            tc+=1
        else:
            ac+=tc//2
            tc=1
            p=st[i]
    else:
        ac+=tc//2
    return ac

s=input()
k=int(input())
n=len(s)
if n==1:
    print(k//2)
    exit()
if n==2:
    if s[0]==s[1]:
        print(k)
    else:
        print(0)
    exit()
ac=p(s)
if k>1:
    ac4=p(s+s+s+s)
    if ac*4==ac4:
        print(ac*k)
    elif ac4-ac*4==3:
        print(ac*k+(k-1))
    elif ac4-ac*4==2:
        print(ac*k+(k//2))
else:
    print(ac)

Submission status: Accepted

Solution code:
s = list(x for x in input())
s2 = s + s
s_num = s.count(s[0])
count = 0
count2 = 0
k = int(input())

for i in range(len(s)-1):
  if s[i] == s[i+1]:
    s[i+1] = 0
    count += 1
for i in range(len(s2)-1):
  if s2[i] == s2[i+1]:
    s2[i+1] = 0
    count2 += 1

if len(s) == 1:
  print(k//2)
elif s_num == len(s):
  if k%2 == 0:
    print(count2*(k//2))
  else:
    print(count2*(k//2)+count)
else:
  print((count2-count)*(k-1) + count)

Submission status: Accepted

Solution code:
s = list(input())
k = int(input())
ln = len(s)
if ln == 1:
    ans = k//2
else:
    i = 0
    j = 0
    m = []
    for i in range(ln-1):
        if s[i] == s[i+1]:
            j += 1
        else:
            if j > 0:
                m.append(int(j+1))
                j = 0
    if j > 0:
        m.append(int(j+1))
    if j == ln-1:
        ans = (ln * k) // 2
    else:
        if m == []:
            if s[0] != s[ln-1]:
                ans = 0
            else:
                ans = k-1
        else:
            if k == 1:
                m = list(map(lambda x: x//2, m))
                ans = sum(m)*k
            else:
                if s[0] != s[ln-1]:
                    m = list(map(lambda x: x//2, m))
                    ans = sum(m)*k
                else:
                    if s[0] == s[1] and s[ln-2] == s[ln-1]:
                        ter = (m[0]+m[len(m)-1]) // 2
                        ter_sum = m[0] // 2 + m[len(m)-1] // 2 + ter * (k-1)
                        m = list(map(lambda x: x//2, m[1:len(m)-1]))
                        ans = k * sum(m) + ter_sum
                    elif s[0] == s[1]:
                        ter = (m[0]+1) // 2
                        ter_sum = m[0] // 2  + ter * (k-1)
                        m = list(map(lambda x: x//2, m[1:len(m)]))
                        ans = k * sum(m) + ter_sum
                    elif s[ln-2] == s[ln-1]:
                        ter = (1+m[len(m)-1]) // 2
                        ter_sum = m[len(m)-1] // 2 + ter * (k-1)
                        m = list(map(lambda x: x//2, m[0:len(m)-1]))
                        ans = k * sum(m) + ter_sum
                    else:
                        m = list(map(lambda x: x//2, m))
                        ans = k * sum(m) + k - 1
print(ans)

Submission status: Accepted

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

L = len(S)

if L == 1:
    print(K//2)
    exit()

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

L = len(S)
acc = [0] * L
tmp = 0
cnt1 = 0
for i in range(1, L):
    if S[i - 1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt1 += 1
    else:
        tmp = 0
    acc[i] = tmp

if K == 1:
    print(cnt1*K)


S += S
L = len(S)
acc = [0] * L
tmp = 0
cnt2 = 0

for i in range(1, L):
    if S[i - 1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt2 += 1
    else:
        tmp = 0
    acc[i] = tmp

diff = cnt2 - cnt1

print(cnt1 + diff*(K-1))

Submission status: Accepted

Solution code:
from itertools import groupby

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

Submission status: Accepted

Solution code:
s = list(map(str, input()))
k = int(input())
basic = 0
repeat = 0
counter = 0
s_a = s*2
for i in range(1,len(s)):
    if s[i] == s[i-1]:
        counter += 1
        basic += (counter % 2)
    else:
        counter = 0
if counter == len(s)-1:
    print(len(s)*k // 2)
else:
  counter = 0
  for i in range(1,len(s_a)):
    if s_a[i] == s_a[i-1]:
        counter += 1
        repeat += (counter % 2)
    else:
        counter = 0
  repeat -= basic
  print(basic + repeat * (k - 1))

Submission status: Accepted

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


if s.count(s[0]) == len(s):
  print((len(s) * k) // 2)
  sys.exit()

c_c = 0
c_e = 0
counter = 1

for i in range(-1, -len(s)-1, -1):
#  print(counter)
  if s[0] == s[i]:
    counter += 1
  else:
    break
if counter % 2 == 0:
  c_e = 1
#print(c_e)
#print()

for i in range(len(s) - 1):
  if s[i] != s[i+1]:
    counter = 1
  else:
    counter += 1
    if counter % 2 == 0:
      c_c += 1
#  print(s[i+1], counter, c_c)

counter = 1
c_f = 0
for i in range(len(s) - 1):
  if s[i] != s[i+1]:
    counter = 1
  else:
    counter += 1
    if counter % 2 == 0:
      c_f += 1
#  print(c_f)
#  print()

ans = (c_c + c_e) * (k-1) + c_f
print(ans)

Submission status: Accepted

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

import random
import string


def count(string):
    total = 0
    i = 1
    while i < len(string):
        if string[i-1] == string[i]:
            total += 1
            i += 1
        i += 1
    return total

if S == len(S) * S[0]:
    res = (K * len(S)) // 2      
elif S[0] == S[-1]:
    new = S.strip(S[0])
    start = len(S) - len(S.lstrip(S[0]))
    end = len(S) - len(S.rstrip(S[0]))
    res = start // 2 + end // 2 + K * count(new) + (K - 1) * ((start + end) // 2)
else:
    res = K * count(S)

print(res)

Submission status: Accepted

Solution code:
import math
S=list(input())
K=int(input())
count = 0
ans = 0
flag = False
firstCount = 0
firstChart = ""
for i in range(1,len(S)):
  if S[i-1] == S[i]:
    count += 1
  else:
    if flag == False:
      firstCount = count
      firstChart = S[i-1]
    else:
      ans += math.ceil(count/2)*K
    flag = True
    count = 0

if flag:
  if firstChart == S[len(S)-1]:
    ans += (firstCount + count + 2) // 2 * (K-1)
    ans += math.ceil(count/2)
    ans += math.ceil(firstCount/2)
  else:
    ans += math.ceil(count/2)*K
    ans += math.ceil(firstCount/2)*K
else:
  ans += (count+1)*K//2
if len(S) == 1:
  if K == 1:
    print("0")
  else:
    print(math.ceil(K//2))
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
  s = ns() + "0"
  k = ni()
  n = len(s) - 1

  if n * s[0] == s[:-1]:
    print((n * k) // 2)
    return

  isSame = True if s[0] == s[-2] else False

  cnt = 1
  w = []
  w_cnt = []
  pre = s[0]
  ans = 0
  tmp = 0
  for i in range(1, n + 1):
    if pre == s[i]:
      cnt += 1
    else:
      w_cnt.append(cnt)
      tmp += cnt // 2
      cnt = 1
      w.append(pre)
      pre = s[i]

  if isSame:
    joins = (w_cnt[0] + w_cnt[-1]) // 2
    ans = 0
    for c in w_cnt[1:-1]:
      ans += c // 2
    print(ans * k + joins * (k - 1) + w_cnt[0] // 2 + w_cnt[-1] // 2)
  else:
    ans = tmp
    print(ans * k)
  # print(w_cnt[1:-1])
  
  # print(isSame)

main()

Submission status: Accepted

Solution code:
def f(S, K):
    if K == 0: return 0
    cnt = 0
    p = ''
    for i, s in enumerate(S):
        if p == s:
            cnt += 1
            p = ''
        else:
            p = s
    cnt *= K
    if p == S[0]:
        cnt += K - 1
    return cnt


if __name__ == '__main__':
    S = input()
    K = int(input())
    cnt1 = 0
    cnt2 = 0

    if len(S) % 2 == 1 and K > 1:
        S2 = S * 2
        K2 = K // 2
        cnt1 = f(S2, K2)

        S2 = S[::-1] * 2
        cnt2 = f(S2, K2)

        K = K % 2
    cnt1 += f(S, K)
    cnt2 += f(S[::-1], K)

    print(min(cnt1, cnt2))

Submission status: Accepted

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


a = S[0]
c = 0
S2 = copy.deepcopy(S)
for i in range(1, len(S)):
    if S2[i] == S2[i-1]:
        S2[i] = '1'
        c += 1

a = S[-1]
last = 0
for i in range(2, len(S)+1):
    last += 1
    if S[-i] != a:
        break

if all([e == S[0] for e in S[1:]]) == True:
    if len(S)%2==0:
        print(int(len(S)/2*K))
    else:
        print(int(len(S)//2*((K+1)//2)+(len(S)//2+1)*(K//2)))
elif S[0] == S[-1] and last % 2 == 1:
    c3 = 0
    S3 = copy.deepcopy(S)
    for i in range(2, len(S)):
        if S3[i] == S3[i-1]:
            S3[i] = '1'
            c3 += 1
    print(c + (c3+1)*(K-1))
else:
    print(c*K)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
l=list(s)
r=list(s*2)
z=set(l)
x=0
y=0
if len(z)==1 :
    print(int((len(l)*k)/2))
else:
    for i in range(1,len(l)):
      if l[i-1]==l[i]:
        l[i]=0
        x+=1
    for i in range(1,len(r)):
       if r[i-1]==r[i]:
        r[i]=0
        y+=1
    if 2*x==y:
         print(k*x)
    else:
         print(k*x+k-1)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

S = list(input())
K = int(input())
N = len(S)
ans = 0
last_same = False
if N==1:
    print(K//2)
    exit()
for i in range(1,N):
    if S[i]!=S[i-1]:
        break
else:
    print(N*K//2)
    exit()
p = False

#前からと後ろからで連続しているかずをかぞえあげる.
pre_num = 0
l_num = 1
if S[0]==S[-1]:
    pre_num = 1
    for i in range(1,N-1):
        if S[i]==S[i-1]:
            pre_num += 1
        else:
            break
    l_num = 1
    for i in range(N-1,0,-1):
        if S[i]==S[i-1]:
            l_num += 1
        else:
            break

for i in range(pre_num,N-l_num):
    if p:
        p = False
        continue
    if S[i]==S[i+1]:
        ans += 1
        p = True

ANS = (pre_num//2) + ans*K + ((pre_num+l_num)//2)*(K-1) + (l_num//2)

print(ANS)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
ans = 0
cnt = 0
ans_fin = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        cnt = cnt +1
        if i == len(s)-2:
            if cnt%2 == 1:
                ans = ans + int((cnt+1)/2)
            else:
                ans = ans + int(cnt/2)
    else:
        if cnt%2 == 1:
            ans = ans + int((cnt+1)/2)
            cnt = 0
        elif (cnt%2 == 0 and not(cnt == 0)):
            ans = ans + int(cnt/2)
            cnt = 0
        else:
            pass
ans_fin = ans*k
#print(ans_fin)
if not(s[0] == s[len(s)-1]):
    pass
elif len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
elif len(set(s)) == 1:
    if (len(s)*k)%2 == 0:
        ans_fin = int((len(s)*k)/2)
    else:
        ans_fin = int((len(s)*k-1)/2)
else:
    cnt2 = 0
    cnt3 = 0
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            cnt2 = cnt2 + 1
        else:
            break
    for i in range(len(s)-1):
        if s[len(s)-1-i] == s[len(s)-i-2]:
            cnt3 = cnt3 + 1
        else:
            break
#    print(cnt2,cnt3)
    if (cnt2%2 ==0 and cnt2 >0) and(cnt3%2 == 0 and cnt3>0):
        ans_fin = ans_fin + (k-1)
    if cnt2 == 0 and cnt3 == 0:
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Submission status: Accepted

Solution code:
import sys,queue,copy
sys.setrecursionlimit(10**8)
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
INF = float('inf')

S = list(input())
K = int(input())
i = 0
ans = 0

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

while i+1 < len(S):
    tempc= 1
    while S[i] == S[i+1]:
        tempc += 1
        if tempc == len(S):
            print(tempc * K // 2)
            exit()
        if i+2 < len(S):
            i += 1
        else:
            break

    ans += tempc //2
    if i+2 >= len(S):
        break
    i += 1

j=2
frag = 0
if S[0] == S[-1] and K >1:


    last_num = 1
    while S[-1] == S[-j]:
        last_num += 1
        if (j+1) <= len(S):
            j += 1
        else:
            break
    i=0
    first_num = 1
    while S[i] == S[i+1]:
        first_num += 1
        if i+2 < len(S):
            i += 1
        else:
            break

    if last_num%2 + first_num%2 == 2:
        frag = 1


print((ans+frag)*K-frag)

Submission status: Accepted

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

if len(S) == 1:
    print(K//2)
    exit()
cons = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cons += 1
        S[i+1] = '_'
if K == 1:
    print(cons)
    exit()
if S[0] != S[-1]:
    print(cons * K)
    exit()
cons3 = 0
if len(set(S2)) == 1:
    S2 = S2 * 2 + ['']
    for i in range(len(S2) - 1):
        if S2[i] == S2[i+1]:
            cons3 += 1
            S2[i+1] = '_'
    print(cons3 * (K//2) + cons * (K%2))
    exit()
# 最初から何文字連続するか
# この時最後の連続する文字はすでに奇数でかつ最初の文字と一緒
cons2 = 1
j = 0
while True:
    if S2[j] == S2[j+1]:
        cons2 += 1
        j += 1
    else:
        break
if cons2 % 2 == 0:
    minus = 0
else:
    minus = 1
print(cons * K + minus * (K-1) )

Submission status: Accepted

Solution code:
def a(s):
    count = 0
    test = s[0]
    for i in range(len(s) - 1):
        if s[i] == s[i + 1] and test[i] != 'A':
            # s = s[:i + 1] + 'A' + s[i + 2:]
            test += 'A'
            count += 1
        else:
            test += s[i]
    return count


s = input()
k = int(input())
if s[0] != s[-1] or k == 1:
    print(a(s) * k)
elif k == 2:
    new_s = s * 2
    print(a(new_s))
elif len(s) == s.count(s[0]):
    new_s = s * 2
    count_2 = a(new_s)
    print(count_2 * (k // 2) + a(s) * (k % 2))
else:
    new_s = s * 2
    count_2 = a(new_s)
    count_3 = a(new_s + s)
    diff = count_3 - count_2
    print(count_3 + (k-3) * diff)

Submission status: Accepted

Solution code:
import collections
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        S = input().rstrip()
        K = int(input())

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

        ans = 0
        start = 0
        end = len(S)
        cnt_1 = 1
        cnt_2 = 1

        if len(collections.Counter(S)) == 1:
            print((len(S) * K) // 2)
            exit()

        if S[0] == S[-1]:
            for i in range(1, len(S) - 1):
                if S[i] != S[0]:
                    start = i
                    break
                else:
                    cnt_1 += 1

            for i in range(1, len(S) - 1)[::-1]:
                if S[i] != S[0]:
                    end = i + 1
                    break
                else:
                    cnt_2 += 1

            ans += (cnt_1 // 2) + ((cnt_1 + cnt_2) // 2) * (K - 1) + (cnt_2 // 2)

        before = S[start]
        length = 1
        changes = 0
        for i in range(start + 1, end):
            if S[i] == before:
                length += 1
            else:
                changes += length // 2
                length = 1
                before = S[i]

        changes += length // 2
        ans += changes * K
        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
S = str(input())
K = int(input())
ans = int(0)
count = int(0)
count_first = int(0)
onelen = int(0)
if S[0]==S[-1]:
    count_first += 1
#if count_first == 0:
for i in range(1,len(S)):
    if S[i] == S[i - 1] and i != len(S) - 1:
        count += 1
    elif S[i] == S[i - 1] and i == len(S) - 1:
        count += 1
        if count != len(S)-1:
            onelen += (count + 1) // 2
        else:
            ans = ((count+1) * K) // 2

    else:
        onelen += (count + 1) // 2
        count = 0
if ans == 0:
    ans = onelen * K
#    if S[0] == S[-1]:
#        ans -= 2
if len(S)==1:
    ans = K//2

#else:
if count_first==1 and count != len(S)-1:
    i = int(0)
    while S[i] == S[i + 1]:
        count_first += 1
        i += 1
    i = int(-1)
    while S[i] == S[i - 1]:
        count_first += 1
        i -= 1
    if count_first % 2 == 1:
        ans += K-1

print(ans)

Submission status: Accepted

Solution code:

s = input()
k = int(input())
ls = len(s)

word_list = []

a = ""
b = 1

for i, v in enumerate(s):
    if a != v:
        if a != "":
            word_list.append((a,b))
        a = v
        b = 1
    else:
        b += 1
    if i == ls-1:
        word_list.append((a,b))

ans = 0
if len(word_list) == 1:
    ans = (word_list[0][1] * k) // 2
else:
    if word_list[-1][0] != word_list[0][0]:
        for i in word_list:
            ans += (i[1])//2
        ans *= k
    else:
        head, *body, tail = word_list

        for i in body:
            ans += (i[1])//2
        ans *= k
        ans += head[1] // 2
        ans += tail[1] // 2
        headtail = (head[1]+tail[1]) //2 *(k-1)
        ans += headtail

print(ans)

Submission status: Accepted

Solution code:

from sys import stdin

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

l = len(S)
ans = 0

for i in range(l):
    mo = S[0]*l
    if mo == S:
        ans = l*K//2
        print(ans)
        exit()
    else:
        pass


if S[0] == S[l-1]:
    S2 = S*2
    l2 = len(S2)
    a = 0#位置
    ans2 = 0
    while a < l2-1:
        if S2[a] == S2[a+1]:
            t = S2[a]
            c = 1
            for i in range(1,l2-a):
                if t == S2[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans2 += j
            a += c
        else:
            a += 1
    a = 0#位置
    ans1 = 0
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans1 += j
            a += c
        else:
            a += 1
    differ = ans2-ans1*2
    ans = differ*(K-1) + ans1*K
else:
    a = 0#位置
    while a < l-1:
        if S[a] == S[a+1]:
            t = S[a]
            c = 1
            for i in range(1,l-a):
                if t == S[a+i]:
                    c += 1
                else:
                    break
            j = c//2
            ans += j
            a += c
        else:
            a += 1

    ans = ans*K

print(ans)

Submission status: Accepted

Solution code:
S = input()
K = int(input())
result = 0
if S[0] != S[-1]:
    t = None
    for i in range(len(S)):

        if i == 0:
            t = S[i]
            continue

        if t == S[i]:
            result += 1
            t = None
            continue

        t = S[i]

    print(result * K)
    exit()

else:

    t = S[0]
    count_a = 1
    for i in range(len(S)):
        if i == 0:
            continue

        if t == S[i]:
            count_a += 1
        else:
            break
    if count_a == len(S):
        print((len(S) * K) // 2)
        exit()
    count_b = 0

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

    T = S[count_a:-count_b]

    m = None
    for i in range(len(T)):

        if i == 0:
            m = T[i]
            continue

        if m == T[i]:
            result += 1
            m = None
            continue

        m = T[i]

    count = count_a + count_b
    change = count // 2
    change_a = count_a // 2
    change_b = count_b // 2

    print(change_a + change_b + (K - 1) * change + K * result)

Submission status: Accepted

Solution code:
s = input()
k = int(input())
n = 0
j = s[0]
l = 1
p = 0
fi,la = 0,0
c = 1
if len(set(list(s))) == 1:
  print(len(s)*k//2)
else:
  for i in range(1,len(s)):
    if s[i] == j:
      l += 1
    else:
      n += l//2
      if s[i-1] == s[0] and c == 1:
        fi = l
        c = 0
      l = 1
      j = s[i]
  n += l//2
  la = l
  if s[0] == s[-1] and fi%2 == 1 and la%2 == 1:
    p = k-1
  print(n*k+p)

Submission status: Accepted

Solution code:
S=list(input())
S_=[]
S__=[]
K=int(input())
ans=0
i=0
if S[0]==S[-1]:
    s=S[0]
    S_.append(S.pop(0))
    while len(S)>0:
        if s==S[0]:
            S_.append(S.pop(0))
        else:
            break
    while len(S)>0:
        if s==S[-1]:
            S__.append(S.pop(-1))
        else:
            break
if len(S)>0:
    while i<len(S)-1:
        if S[i+1]==S[i]:
            ans+=K
            i+=1
        i+=1
if len(S)>0:
    ans+=(K-1)*((len(S_)+len(S__))//2)
    ans+=len(S_)//2
    ans+=len(S__)//2
else:
    ans+=K*(len(S_)+len(S__))//2
print(ans)

Submission status: Accepted

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

ans1 = 0
old = ""
flag = 0
for i in range(N):
    if old == "":
        old = S[i]
    else:
        if old == S[i]:
            old = ""
            ans1+=1
        else:
            old = S[i]
            
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 1

if flag == 0:
    print(ans1*K)
else:
    ans2 = 0
    old = ""
    for i in range(N):
        if i == 0:
            if S[0] == S[-1]:
                ans2+=1
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    if i == N-1:
        if old == S[i] and S[0] == S[-1]:
            flag = 2
    
    k = K//2
    if flag == 2:
        print(ans1*1+ans2*(K-1))
    else:
        print(ans1*(K-k)+ans2*(k))

Submission status: Accepted

Solution code:
from itertools import groupby

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

Submission status: Accepted

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

if len(s) == 1:
  print(k // 2)
else:
  cnt = 0
  p = 1
  while p < len(s):
    if s[p-1] == s[p]:
      cnt += 1
      p += 1
    p += 1
      
  cnt2 = 0
  p2 = 1
  s2 = s + s
  while p2 < len(s2):
    if s2[p2-1] == s2[p2]:
      cnt2 += 1
      p2 += 1
    p2 += 1

  flg = 1
  for i in range(len(s)-1):
    if s[i] != s[i+1]:
      flg = 0
  
  if 2*cnt < cnt2:
    if flg == 1:
      print(k*cnt + (k//2))
    else:
      print(k*cnt + (k - 1))
  else:
    print(k*cnt)

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S = input()
N = len(S)
K = INT()

A = []
cur = S[0]
cnt = 1
for i in range(1, N):
    if S[i-1] == S[i]:
        cnt += 1
    else:
        A.append((S[i-1], cnt))
        cnt = 1
A.append((S[N-1], cnt))

if len(A) == 1:
    print(N*K//2)
    exit()

change = 0
for s, cnt in A:
    change += cnt // 2

if S[0] == S[N-1] and A[-1][1] % 2 == 1 and A[0][1] % 2 == 1:
    print((change+1)*K-1)
else:
    print(change*K)

Submission status: Accepted

Solution code:
s=input()
k=int(input())
lists=[]
flag=False
for i in range(len(s)):
    lists.append(s[i]) 
    
if len(s)>=2 and len(set(lists))>=2:
    lists.append("good")
    sums=0
    cnt=1
    ind=[]
    for j in range(len(s)):
        if lists[j]!=lists[j+1]:
            if cnt==1:
                cnt=1
            elif cnt>=2:
                ind.append(cnt)
                sums+=(cnt//2)
                cnt=1
        elif lists[j]==lists[j+1]:
            cnt+=1  
    del lists[-1]
    if lists[-1]==lists[-2] and ind[-1]%2==0:

        pass
         
    if lists[-1]==lists[-2] and ind[-1]%2!=0:

        if lists[-1]==lists[0] and (lists[0]!=lists[1]or( lists[0]==lists[1] and ind[0]%2!=0)) :
            sums+=1
            flag=True
        else:
            pass
    if lists[-1]!=lists[-1]:
        if lists[-1]==lists[0]:
            sums+=1
        else:
            pass
    if flag:
        print(sums*k-1)
    else:
        print(sums*k)
 
elif len(s)==1:
        print(k//2)
elif len(set(lists))==1:
    print(k*len(lists)//2)

Submission status: Accepted

Solution code:
def f():
    S = input()
    K = int(input())
    S = list(S)
    if K == 1 and len(S) == 1:
        print(0)
        return
    
    if K == 1 and len(S) != 1:
        ans = 0
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = '1'
                ans += 1
        print(ans)
        return
    
    if K != 1 and len(S) == 1:
        ans = K // 2
        print(ans)
        return
    
    if K != 1 and len(S) != 1:
        ORG_S = S[:]
        a = S[0]
        for s in S[1:]:
            if a[0] == s:
                a += s
            else:
                break
        b = S[-1]
        for s in reversed(S[:-1]):
            if b[0] == s:
                b += s
            else:
                break
        a = list(a)
        b = list(b)
        if len(a) == len(S):
            ans = len(S)*K//2
        else:
            c = list(a + b)
            S = S[len(a):-1*len(b)]
        
            ans_a = 0
            for i in range(len(S)-1):
                if S[i] == S[i+1]:
                    S[i+1] = str(i)
                    ans_a += 1
            ans_b = 0
            for i in range(len(c)-1):
                if c[i] == c[i+1]:
                    c[i+1] = str(i)
                    ans_b += 1
            ans_c = 0
            if len(a) > 1:
                for i in range(len(a)-1):
                    if a[i] == a[i+1]:
                        a[i+1] = str(i)
                        ans_c += 1
            ans_d = 0
            if len(b) > 1:
                for i in range(len(b)-1):
                    if b[i] == b[i+1]:
                        b[i+1] = str(i)
                        ans_d += 1 
            ans = ans_a*K + ans_b*(K-1)+ans_c+ans_d
        print(ans)
        return

f()