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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
# import math
# import numpy as np
# from functools import lru_cache
import string

args = sys.argv
if len(args) == 2:
  from IPython import embed
  em = True
else:
  em = False

# sys.setrecursionlimit(10000)

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

def calc(S, K):
  if len(S) == 1:
    if K == 1:
      print(0)
      exit()
    else:
      S = S * K
      K = 1
  # a, b = map(int, input().split())
  # a = list(map(int, input().split()))
  # a = list(map(lambda x: int(x)*(-1), a))  # 各要素を-1倍


  # n = int(input())
  # t = [int(input()) for i in range(n)]
  if em: sys.stdin = open('/dev/tty')

  #### function
  # @lru_cache(maxsize=10000)

  #### process
  # if em: embed()
  last_chk = 0
  letters = string.ascii_lowercase
  score = 0
  for i in range(0, len(S) - 2):
    # print(S)
    if S[i] == S[i + 1]:
      S[i + 1] = letters.strip(S[i]).strip(S[i + 2])[0]
      score = score + 1

  if S[-2] == S[-1]:
    S[-1] = letters.strip(S[-2]).strip(S[0])[0]
    score = score + 1
    # last_chk = 1
  else:
    if S[-1] == S[0]:
      S[-1] = letters.strip(S[-2]).strip(S[0])[0]
      score = score + 1
      last_chk = 1
  return score, last_chk, S

if K % 2 == 0:
  S = S * 2
  K = int(K / 2)
  score, last_chk, S2 = calc(S,K)
  total = score * K
  if last_chk == 1: total = total - 1
  print(total)
else:
  if K == 1:
    score, last_chk, S2 = calc(S,K)
    total = score * K
    if last_chk == 1: total = total - 1
    print(total)
  else:
    S_orig = S
    S = S * 2
    K = int((K - 1) / 2)
    score, last_chk, S2 = calc(S,K)
    total = score * K
    if last_chk == 1: total = total - 1
    if em: embed()
    S3 = [S2[-1]] + S_orig
    score3, last_chk3, S4 = calc(S3,1)
    if last_chk3 == 1: score3 = score3 - 1
    print(total + score3)


# score = 0
# i = 0
# last_chk = 0
# while True:
#   if S[i] == S[i + 1]:    # 次と同じなら
#     if i == len(S) - 2:    # 末尾だったらスコア増えない
#       score = score + 1
#       break
#     elif i == len(S) - 3:
#       if S[i + 1] == S[i + 2]:
#         score = score + 1
#         if S[0] == S[-1]:
#           score = score + 1
#         break
#     else:
#       score = score + 1
#       i = i + 2
#   else:
#     i = i + 1

# print(score*K)


## heapsort, O(logN)
# import heapq
# heapq.heapify(A)
# min = heapq.heappop(A)
# heapq.heappush(A, new_element)

Submission status: Wrong Answer

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

def func(s):
    ans = 0
    n = len(s)
    s_list = []
    for i in range(n):
        s_list.append(s[i])
    for i in range(n - 1):
        if s_list[i] == s_list[i + 1]:
            ans += 1
            s_list[i + 1] = str(i + 1)
    ans *= k
    if s_list[0] == s_list[n - 1]:
        ans += k - 1
    return ans

ans1 = func(s)
s_inv = ''.join(list(reversed(s)))
ans2 = func(s_inv)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

#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: Wrong Answer

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

memc = ""
resnum = 0
t = s + s[0]
a = []
for c in t:
    if c == memc:
        resnum +=1
    else:
        a += [int((resnum+1)/2)]
        memc = c
        resnum = 0
a += [int((resnum+1)/2)]
res = sum(a) * k
print(res)

Submission status: Wrong Answer

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

box = ""
count = 0
     
for mozi in S:
  if mozi == box:
    count += 1
    box = ""
  else:
    box = mozi

box = S[len(S)-1]
count_fin = 0
for mozi in reversed(S):
  if box != mozi:
  	break
  count_fin +=1

if S[0] == S[len(S)-1] and count_fin%2 != 0:
  print((count+1)*K-1)
else:
  print(count*K)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
if s[0] == s[-1] and n==0:
    n += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

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

s *= 2
first_char = s[0]
pre_char = ''
char = ''
ans = 0
last_char_change = False
single_cost = 0

for i in range(len(s)):
    char = s[i]
    if pre_char == char:
        ans += 1
        pre_char = ''
    else:
        pre_char = char
        if i == len(s) - 1:
            if first_char == char:
                last_char_change = True
    
    if i == (len(s) // 2) - 1:
        single_cost = ans
    

if n % 2 == 0:
    if last_char_change:
        ans = (n//2) * (ans + 1) - 1
    else:
        ans = (n//2) * (ans)
else:
    if last_char_change:
        ans = ans * (n // 2) + single_cost +  (n // 2)
    else:
        ans = ans * (n // 2) + single_cost


print(ans)

Submission status: Wrong Answer

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()

S += S[0]

L = len(S)

acc = [0] * L
tmp = 0
cnt = 0
first = False
last = False
for i in range(1, L):
    if S[i-1] == S[i]:
        tmp += 1
        if tmp % 2 != 0:
            cnt += 1
            if i == 1:
                first = True
            if i == L - 1:
                last = True
    else:
        tmp = 0
    acc[i] = tmp

if first and last:
    cnt -= 1
cnt *= K
if not first and last:
    cnt -= 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
import os
S = input()
K = int(input())
if len(S) == 1:
    print(0)
    sys.exit()
cnt0 = 1
cntL = 1
cnt = 1
ans = 0
i = 0
while i < len(S)-1:
    for j in range(i+1,len(S)):
        if S[i] == S[j]:
            cnt +=1
        else:
            if i == 0:
                cnt0 = cnt
            i = j
            break
    if cnt > 1:
        ans += cnt//2
        cnt = 1
        i = j

for i in range(len(S)-1,0,-1):
    if S[i] == S[i-1]:
        cntL +=1
    else:
        break
ans = K*ans
if S[0] == S[-1] and (cnt0 + cntL) % 2 == 0:
    ans += K-1

print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
d = []
for i in range(len(a)):
  d.append(a[i])
b = 0
for i in range(len(d)-2):
  if d[i] == d[i+1]:
    d[i+1] = 'S'
    b += 1
if d[0] == d[-1]:
  b += 1
c = int(input())
print(b*c)

Submission status: Wrong Answer

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)

                    if len(c) >= 1:
                        c[0][1] = 0
                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 flg and len(c) >= 2:
            flg = False
            continue
        if connected:
            if len(c) == 1:
                ans = (elem[1] //2) * K
            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
        
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
 
count = 0
i=0
join_flag =0
join_n = 0

if len(s)==1:
  print(k//2)
    
elif len(s)==2:
  if s[0]==s[1]:
    print(k)
  else:
    print(0)
  
elif len(set(s))==1:
  print(len(s)*k //2)
  
else:
  while i<len(s)-1:
    if s[i] == s[i+1]:
      count+=1
      i+=2
    else:
      i+=1
  
  if (s[-1] == s[0]) :
    join_flag =1
    
  if join_flag:  
    count_len = 1
    for i in range(len(s)-1):
      if s[-1-i]==s[-2-i]:
        count_len += 1
      else:
        break
    if count_len%2 ==1 and len(s)>2:
      join_n = k//2
 
  print(count*k + join_n)

Submission status: Wrong Answer

Solution code:
S = list(input())
K = int(input())
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        S[i+1] = "1"
        count += 1
if count == 0 and S[0] == S[-1] and K != 1:
    count = 1 * K - 1
else:
    count *= K
print(count)

Submission status: Wrong Answer

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


def s_change(S):
    s_henkan = 0
    L = len(S) -1
    if S[0] == S[L]:
        if S[0] == S[1] and S[L-1] != S[L]:
            S[0] = '2'
            s_henkan += 1
#             print(S)
    for i in range(1,len(S)):
#         print(S[i])
        if S[i] == S[i-1]:
#             print(S[i-1],S[i])
            S[i] = '1'
            s_henkan += 1
    return(S,s_henkan)

        
henkan = 0
    
if len(S) == 1:
    henkan = K // 2
elif len(S) == 2:
    if S[0] == S[1]:
        henkan = K
    else:
        henkan = 0
else:
    SSSS = s_change(S)
    henS = SSSS[0]
    s_henkan = SSSS[1]
    
    if henS[0] == henS[len(henS)-1]:
        henkan = s_henkan * K + (K-1)
    else:
        henkan = s_henkan * K

print(henkan)

Submission status: Wrong Answer

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

ans = 0
now = 0
flag = 1
onaji = False

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

for i in range(len(s)):
    #print(i,now)
    if now >= len(s) - 1:
        break
    if s[now] == s[now+1]:
       ans += 1
       now += 2
       if now+1 == len(s) -1:
           flag = 0
    else:
        now += 1

if len(s) == 2:
    print(int(ans*k))
    exit()

for i in range(len(s)-1):
    if s[i] != s[i+1]:
        break
    if i == len(s) - 2:
        onaji = True

#print(flag)

if flag == 1:
    if s[0] == s[-1]:
        if s[0] == s[1]:
            flag = 2
        else:
            flag = 1
    else:
        flag = 0
#print(flag)

if flag != 2:
    print(int(ans*k+flag*(k-1)))
else:
    if onaji == True:
        print(int(ans*k+k//2))
    else:
        print(int(ans*k+1*(k-1)))
#print(ans,ans*k,ans*k+flag*(k-1))

Submission status: Wrong Answer

Solution code:
S_list = [s for s in input()]
S_list.append(S_list[0])
K = int(input())
count = 0
for idx, s in enumerate(S_list):
    if idx == len(S_list) - 1:
        break
    if S_list[idx + 1] == s:
        count += 1
        S_list[idx + 1] = "*"
print(count * K)

Submission status: Wrong Answer

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

S2 = S.copy()
S2.reverse()

T = S * 2
T2 =  S * 2
T2.reverse()

G = S * 3
G2 = S * 3
G2.reverse()

countA1 = 0
for i in range(len(S) - 1):
    if S[i] == S[i + 1]:
        countA1 += 1
        S[i + 1] = 'A'

countA2 = 0
for i in range(len(S2) - 1):
    if S2[i] == S2[i + 1]:
        countA2 += 1
        S2[i + 1] = 'A'

# 2bai
countB1 = 0
for i in range(len(T) - 1):
    if T[i] == T[i + 1]:
        countB1 += 1
        T[i + 1] = 'A'

countB2 = 0
for i in range(len(T2) - 1):
    if T2[i] == T2[i + 1]:
        countB2 += 1
        T2[i + 1] = 'A'

# 3bai
countC1 = 0
for i in range(len(G) - 1):
    if G[i] == G[i + 1]:
        countC1 += 1
        G[i + 1] = 'A'

countC2 = 0
for i in range(len(G2) - 1):
    if G2[i] == G2[i + 1]:
        countC2 += 1
        G2[i + 1] = 'A'

if K > 1:
    if K == 2:
        print(min(countB1, countB2))
    elif K == 3:
        print(min(countC1, countC2))
    else:
        if K % 2 == 1:
            print(min((K//2-1)*countB1+countC1, (K//2-1)*countB2+countC2))
        else:
            print(min(K//2*countB1, K//2*countB2))
else:
    print(min(countA1,countA2))

Submission status: Wrong Answer

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


flag=True
for x in range(len(S)-1):
    if S[x]!=S[x+1]:
        flag=False
        break

if flag:
    #print("a")
    print(int(len(S)*K/2))

else:
    S+="*"

    count=1
    result=0
    for x in range(len(S)-1):
        if S[x]==S[x+1]:# and S[x+1]!="*":
            count+=1
        else:
            if count>1:
                result+=int(count/2)
                count=1
        #print(S[x],":",S[x+1])
        #print(count)
        #print(result)
    result*=K
    """print(S[0])
    print(S[1])
    print(S[-1])
    print(S[-2])"""
    if S[0]==S[-2] and S[0]!=S[1] and S[-2] != S[-3]:
        result+=K-1
    #print("b")
    print(result)

Submission status: Wrong Answer

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

x = 1
for i in range(n):
    if i == n-1:
        if s[i] == s[0]:
            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] + l[-1]) % 2 == 0:
    ans += k-1

print(ans)

Submission status: Wrong Answer

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

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

flg = 0
if S[-1] == S[0]:
    flg = 1

for i in range(1, len(S)):
    if S[i] == S[i-1] and S[i] != None:
        ans += 1
        S[i] = None
ans *= K

if flg:
    ans += (K-1)

print(ans)

Submission status: Wrong Answer

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:
    ac2=p(s+s)
    if ac==ac2:
        print(ac*k)
    elif ac2-ac==1:
        print(ac*k+(k-1))
    elif ac2-ac==2:
        print(ac*k+(k//2))
else:
    print(ac)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
sum_count = 0
count = 1
key = s[0]
flag = False
if s[0] == s[len(s)-1] and s[len(s)-2] != s[len(s)-1] and len(s)>=3:
    flag = True
    for i in range(1,len(s)):
        if s[i] == key:
            count += 1
        else:
            sum_count += count//2
            key = s[i]
            count = 1
elif s.count(s[0]) == len(s) and len(s) != 1:
    sum_count = int((len(s)/2)*k)
    k = 1
else:
    flag = False
    for i in range(1,len(s)):
        if s[i] == key:
            count += 1
        else:
            sum_count += count//2
            key = s[i]
            count = 1

sum_count += count//2

if flag == True:
    print((sum_count)*k+1)
else:
    print(sum_count*k)

Submission status: Wrong Answer

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

if LS==1:
    if K==1:
        print(0)
        exit()
    else:
        print(K//2)
        exit()

cnt=1
ans1=0
c=[]
for i in range(1,LS-1):
    if S[i-1]==S[i]:
        cnt+=1
    elif cnt!=1:
        ans1+=cnt//2
        c.append(cnt)
        cnt=1

if K==1:
    if S[-2]==S[-1]:
        cnt+=1
        ans1+=cnt//2
else:
    if S[-2]==S[-1]:
        cnt+=1
        c.append(cnt)
        if cnt%2==0:
            ans1+=cnt//2
            ans1=ans1*K
        elif cnt%2==1 and S[0]==S[-1]:
            if c[0]%2==0:
                ans1+=cnt//2
                ans1=ans1*K
            else:
                ans1+=cnt//2+1
                ans1=ans1*K-1
        elif cnt%2==1 and S[0]!=S[-1]:
            ans1+=cnt//2
            ans1=ans1*K
    else:
        ans1+=cnt//2
        c.append(cnt)
        c.append(1)
        if S[0]==S[-1]:
            if ans1!=0:
                ans1=ans1*K+K-1
            else:
                ans1=K-1
        else:
            ans1=ans1*K
print(ans1)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
p=s[0]
cnt=1
q=[]
for i in range(len(s)-1):
	if s[i+1]==p:
		cnt+=1
	else:
		q.append(cnt)
		cnt=1
		p=s[i+1]
q.append(cnt)
if q[-1]%2==0:
	cnt=True
else:
	cnt=False
ans=0
if cnt==True:
	for i in range(len(q)):
		ans+=q[i]//2
	print(ans*k)
	exit()

Submission status: Wrong Answer

Solution code:
a=list(input())
k=int(input())
ans=0
if len(a)==1:
  print(k//2)
elif len(list(set(a)))==1:
  print((len(a)*k)//2)
else:  
  for i in range(len(a)-1):
    if a[i]==a[i+1]:
      a[i+1]='1'
      ans+=1
  if a[0]==a[-1] and a[1]!="1":
    print(ans*k+k-1)
  else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 0
flag = 0
for i in range(len(s)-1):
  if s[i] == s[i+1]:
    cnt += 1
    flag = 1
    if flag == 0:
      cnt -= 1
  else:
    flag = 0
print(cnt*k)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
X = S.copy()
S += T
ans = 0
cnt = 0

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

for i in range(1, N):
    if S[i - 1] == S[i]:
        cnt += 1
        S[i] = "-1"
if T[0] == T[-1]:
    # print(cnt)
    # cnt2 = 0
    # for i in range(1, N):
    #     if T[i] == T[i - 1]:
    #         cnt2 += 1
    #         T[i] = "-1"
    # ans = cnt + cnt2 * (K - 1)
    # print(cnt2)
    cnt1 = 0
    if X[0] == X[1]:
        cnt1 += 1
    for i in range(1, N-1):
        if X[i + 1] == X[i]:
            cnt1 += 1
            X[i + 1] = "-1"
    ans = min(cnt + cnt1 * (K -1), cnt1 + cnt * (K - 1))
else:
    ans = cnt * K
print(ans)

Submission status: Wrong Answer

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


def s_change(S):
    s_henkan = 0
    L = len(S) -1
    if S[0] == S[L]:
        if S[0] == S[1] and S[L-1] != S[L]:
            S[0] = '2'
            s_henkan += 1
            print(S)
    for i in range(1,len(S)):
#         print(S[i])
        if S[i] == S[i-1]:
#             print(S[i-1],S[i])
            S[i] = '1'
            s_henkan += 1
    return(S,s_henkan)

        
henkan = 0
    
if len(S) == 1:
    henkan = K // 2
elif len(S) == 2:
    if S[0] == S[1]:
        henkan = K
    else:
        henkan = 0
else:
    SSSS = s_change(S)
    henS = SSSS[0]
    s_henkan = SSSS[1]
    
    if henS[0] == henS[len(henS)-1]:
        henkan = s_henkan * K + (K-1)
    else:
        henkan = s_henkan * K

print(henkan)

Submission status: Wrong Answer

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

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

print(0)
exit()
ans = 0
T = list(S)

if S[0] == S[-1]:
    ans += K - 1
    if S[-1] == S[-2]:
        T[-1] = '.'
        ans += 1
    else:
        if S[0] == S[1]:
            ans += 1
        T[0] = '.'

change = 0
for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1] and T[i - 1] != '.':
        change += 1
        T[i] = '.'

print(ans + change * K)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
# 一番効率が良いのは3連続で同じ文字列が続くときに真ん中を別の文字に変換する場合
# 2連続で同じ文字が続く場合は後ろの文字を変える.次の文字からまた数え直す
# 最後の文字列については無視すれば良い

# S=''.join([S for _ in range(K)])
same=False
count = 0
flag=False
flag_odd=False
for i in range(1,len(S)):
    if not same:
        if S[i]==S[i-1]:
            same=True
            count+=1
        else:
            pass
    else:
        same=False
    if not same and i==K and S[0] == S[-1]:
        flag=True
        

same=False
count_odd = 0
for i in range(0,len(S)-1):
    if not same:
        if S[i]==S[i+1]:
            same=True
            count_odd+=1
        else:
            pass
    else:
        same=False
    if not S[0]==S[1] and i==0 and S[0] == S[-1]:
        same=True
        flag_odd=True
    # 文字列の最後尾が変更されずに、かつ前の文字列と同じであった場合は最後尾もしくは、最前を変える必要がある
min_count = min(count*K+flag*(K-1), count_odd*K+flag_odd*(K-1))
print(min_count)

# countは一つの文字列中に存在する必要変換回数.ただし、(文末が変化しない場合)文末の文字が文頭のものとかぶる場合があるので、その場合については、
# 文頭を変化させる

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
m = 0
n = 0
j = 0
for i in range(len(s)):
    if s.count(s[0]) == len(s):
        print(len(s) * k // 2)
        exit()
    if len(s) <= j + 1:
        if s[0]==s[-1]:
            n+=1
        break
    if s[j] == s[j + 1]:
        if j == len(s) - 1:
            n += 1
        else:
            m += 1
        j += 1
    j += 1
print(m * k + n * (k - 1))

Submission status: Wrong Answer

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

s = str(input()).replace('\n', '')
k = int(input())

s = s + s[0]

lst = []
last = ''
cnt = 0
for i in range(len(s)):
  if s[i] == last or last == '':
    cnt += 1
  else:
    lst.append(cnt)
    cnt = 1
  last = s[i]
lst.append(cnt)

    
n_lst = [i // 2 for i in lst]
answer = sum(n_lst)*k
if lst[-1]%2 == 0:
  answer -=  1
  
if len(lst) == 1:
  answer = (len(s)-1)*k//2

if s[0] == s[-2] and (lst[0]+lst[-1])%2 == 0:
  answer -= k//2
  

print(answer)

Submission status: Wrong Answer

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

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: Wrong Answer

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


before = ""
count = 0
if len(S) == 1:
    out = K // 2
else:
    for s in S:
        if before == s:
            count += 1
            before = ""
        else:
            before = s
    ### If before is empty at the end of the loop, the last character should be replaced ->
    ### Can aboid to be same as the first character of the string
    if before == S[0]:
        first_same_num = 1
        before2 = S[0]
        for s in S[1:]:
            if before2 == s:
                first_same_num += 1
            else:
                break
        if first_same_num % 2 == 0:
            first_last_replace = 0      # Even -> the first one could be replaced anyway
        else:
            first_last_replace = K - 1
    else:
        first_last_replace = 0

    out = count * K + first_last_replace

print(out)

Submission status: Wrong Answer

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

if len(set(S)) == 1 or len(S) == 1:
    ans = K * len(S) // 2
    print(0)
    exit()

ans = 0
T = list(S)

if S[0] == S[-1]:
    ans += K - 1
    if S[-1] == S[-2]:
        T[-1] = '.'
        ans += 1
    else:
        if S[0] == S[1]:
            ans += 1
        T[0] = '.'

change = 0
for i, t in enumerate(T[1:], start=1):
    if t == T[i - 1] and T[i - 1] != '.':
        change += 1
        T[i] = '.'

print(ans + change * K)

Submission status: Wrong Answer

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

def makerawlist(S):
    sigma = 0
    counter = 1
    for i in range(len(S)-1):
        if S[i]==S[i+1]:
            counter += 1
        else:
            sigma += counter//2
            counter = 1
    sigma += counter//2
    return sigma

sigma = makerawlist(S)
S.sort()
if S[0]==S[len(S)-1]:
    if (len(S)*K)%2==1:
        print((len(S)*K)//2)
    elif len(S)%2==1 and (len(S)*K)%2==0:
        print((len(S)*K)//2)
elif S[0]==S[len(S)-1] and S[0]!=S[1] and S[len(S)-2]!=S[len(S)-1]:
    print(sigma*K+K-1)
else:
    print(sigma*K)

Submission status: Wrong Answer

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
if len(s) == 1:
    if k%2 ==0:
        ans_fin = int(k/2)
    else:
        ans_fin = int((k-1)/2)
if 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[-i] == s[-i-1]:
            cnt3 = cnt3 + 1
    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 == 1:
        ans_fin = ans_fin + (k-1)
print(ans_fin)

Submission status: Wrong Answer

Solution code:
X=input()
a=int(input())
count=0
z=list(X)
b=0
ans=0
k=0
if len(z)==1:
    ans=int(a/2)
else:
    for k in range(len(z)-1):
        if z[k]==z[len(z)-1]:
            b+=1
    for i in range(len(z)-1):
        if z[i]==z[i+1]:
            z[i+1]=0
            count=count+1
    ans=int(a*count+(a-1)*(((b+1)/2)-1))
print(ans)

Submission status: Wrong Answer

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


# for s in S:
#     if s.isupper():
#         exit()
#     try:
#         float(s)
#     except ValueError:
#         pass
#     else:
#         exit()
# try:
#     float(K)
# except ValueError:
#     exit()
# else:
#     if not float(K).is_integer():
#         exit()

# if not (1 <= len(S) and len(S) <= 100):
#     exit()
# if not (1 <= int(K) and int(K) <= 10**9):
#     exit()
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)
# print(L)

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

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
if len(S) == 1:
  print((len(S) * K) // 2)
  exit()
if len(S) == 2:
  if S[0] == S[1]:
    ans = K
  else:
    ans = 0
  print(ans)
  exit()
SS = S + S
SSS = S + S + S
SSSS = S + S + S + S
cntSS = 0
flg = True
for i in range(1,len(SS)):
  if SS[i - 1] == SS[i] and flg:
    cntSS += 1
    flg = False
  elif flg == False:
    flg = True
    
cntSSS = 0
flg = True
for i in range(1,len(SSS)):
  if SSS[i - 1] == SSS[i] and flg:
    cntSSS += 1
    flg = False
  elif flg == False:
    flg = True

f = (cntSS * 2 - cntSSS)
e = (cntSSS - cntSS)
m = (cntSSS - f - e)
if K < 3:
  mv = 0
else:
  mv = K - 2
if K == 1:
  me = 0
else:
  me = 1
print(f + e * me  + m * mv)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
c = 0
if len(s)==1:
    print(k//2)
elif len(s)==3:
    if s[0]==s[1] and s[0]!=s[2]:
        print(k)
    elif s[0]==s[1] and s[0]==s[2]:
        if k>1:
            print((3*k)//2)
        else:
            print(1)
    elif s[1]==s[2]:
        print(k)
    elif s[0]==s[2]:
        print(k-1)
    else:
        print(0)
else:
    i = 0
    q = False
    while i < len(s)-1:
        if s[i]==s[i+1]:
            c+=1
            i+=1
        i+=1
    if s[len(s)-1]==s[0]:
        if s[len(s)-1]!=s[len(s)-2]:
            c+=1
            q = True
    if q:
        if c==1:
            print(k-1)
        else:
            if s[0]!=s[1]:
                print((c-1)*k+k-1)
            else:
                print((c-1)*k)
    else:
        print(c*k)

Submission status: Wrong Answer

Solution code:
s=input()
s=[i for i in s]
k=int(input())
a=0
for i in range(len(s)-1):
    if s[i]==s[i+1]:
        a+=1
        s[i+1]='1'
if s[0]==s[-1]:
    a+=1
    print(a*k-1)
else:
    print(a*k)

Submission status: Wrong Answer

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:
  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: Wrong Answer

Solution code:
from sys import stdin
S = (stdin.readline().rstrip())
K = int(stdin.readline().rstrip())
ans = 0
N=len(S)
cnt = 1
G = S+"@"
flag = False
for i in range(0,N):
    if G[i] == G[i+1]:
        cnt += 1
        flag = True
    else:
        if cnt != 1:
            #print(cnt)
            ans += (cnt//2)
            cnt = 1
        #print(ans,flag)
if flag != True:
  ans = 0
for_eq = 1
rev_eq = 1
revS = S[::-1]
for j in range(0,N-1):
    if S[j] == S[j+1]:
        for_eq += 1
    else:
        break
for k in range(0,N-1):
    if revS[k] == revS[k+1]:
        rev_eq += 1
    else:
        break
#print(ans)
ans = ans*K
#print(rev_eq,for_eq)
if K != 1:
    if S[0] == S[-1]:
        if (rev_eq % 2 != 0) and (for_eq % 2 != 0):
            ans += ((K+1)//2)
        if rev_eq == 1 and for_eq != 1:
            ans -= (K//2)
        if rev_eq != 1 and for_eq == 1:
            ans -= (K//2)
        if (flag != True) and K == 1:
            ans = 0
if len(set(S))==1:
    ans = (len(S)*K)//2

print(ans)