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/python
# -*- coding: utf-8 -*-
import sys
import math
import collections


def simple_count(S, changed=False):
    count = 0
    for i in range(len(S) - 1):
        if changed:
            changed = False
        else:
            if S[i] == S[i + 1]:
                changed = True
                count += 1
    return count, changed


def solve(S, K):
    if S[0] != S[-1]:
        return simple_count(S)[0] * K
    else:
        count2, changed = simple_count(S*2)
        return count2 * (K//2) + simple_count(S, changed=(not changed))[0] * K%2
        # return simple_count(S + S[0])[0] * K


assert solve("cooooooooonteeeeeeeeeest", 999993333) == 8999939997
assert solve("aqqq", 2) == simple_count("aqqq" * 2)[0]
# print(solve("qq", 81), simple_count("qq" * 81)[0])
assert solve("qq", 81) == simple_count("qq" * 81)[0]
# print(solve("qqq", 2), simple_count("qqq" * 2)[0])
assert solve("qqq", 2) == simple_count("qqq" * 2)[0]
# print(solve("qqq", 3), simple_count("qqq" * 3)[0])
assert solve("qqq", 3) == simple_count("qqq" * 3)[0]
assert solve("issii", 2) == simple_count("issii" * 2)[0]

S = sys.stdin.readline().rstrip()
K, = map(int, sys.stdin.readline().rstrip().split(" "))
print(solve(S, K))

exit(0)

Submission status: Wrong Answer

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]:
            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]:
                if flag == 1:
                    ans2+=1
                else:
                    old = S[0]
        elif old == "":
            old = S[i]
        else:
            if old == S[i]:
                old = ""
                ans2+=1
            else:
                old = S[i]
    
    if N == 1:
        k = K//2
        print(ans1*(K-k)+ans2*k)
    else:
        k = K//2
        print(ans1*k+ans2*(K-k))

Submission status: Wrong Answer

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

import copy

if len(s) == 1:
    print(k-1)

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

Submission status: Wrong Answer

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

N=len(S)
t=S[0]
cnt=0
arr=[]
for s in S[1:N]:
    if t==s:
        cnt+=1
    else:
        arr.append(cnt+1)
        cnt=0
    t=s
arr.append(cnt+1)
ans=sum(map(lambda x: x//2, arr))
if S[0]==S[-1] and arr[0]%2==1 and arr[-1]%2==1:
    print((ans+1)*K)
elif len(arr)==1 and N%2==1:
    print((ans+1)*K)
else:
    print((ans)*K)

Submission status: Wrong Answer

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

length = len(S)
if length == 1:
    print(K)
    exit(0)
if length == 2:
    if S[0] == S[-1]:
        print(K)
    else:
        print(0)
    exit(0)

count = 0
start_end_same = S[0] == S[-1]
i = 2
while i < length:
    if S[i-2] == S[i-1] == S[i]:
        S[i-1] = '-'
        count += 1
        i += 2
    elif S[i-2] == S[i-1]:
        S[i-1] = '-'
        count += 1
        i += 1
    elif S[i-1] == S[i]:
        if i == length-1:
            count += 1
            S[i] = '-'
        i += 1
    else:
        i = min(i+2, length)
ans = count*K
if start_end_same and S[1] != '-' and S[-1] != '-':
    ans += (K-1)
print(ans)

Submission status: Wrong Answer

Solution code:
def count(S):
    ans = 0
    i = 1
    while i < len(S):
        if S[i-1] == S[i]:
            i += 1
            ans += 1
        i += 1
    return ans

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

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

    if K == 1:
        print(count(S))
        exit()

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def counter_func(string,x):
  try:
    if string[x] == string[x+1]:
      return 1
    else:
      return 0
  except:
    return 0
  
counter = []
count = 0
j = 0
while j < length:
  if counter_func(s,j) == 1:
    count += 1
  else:
    counter.append(count)
    count = 0
  j += 1
if s[-1] == s[0] and k > 1:
  counter[-1] += 1
sum = 0
if len(counter) != 1:
  for i in range(len(counter)-1):
    if counter[i] != 0 :
      sum += int((counter[i]+1)/2)*k
  if counter[-1] == 1:
    sum += int((counter[-1]+1)/2)*(k-1)
  elif counter[-1] != 0:
    sum += int((counter[-1]+1)/2)*k
else:
  sum = int((length*k)/2)
print(sum)

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

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'

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

Submission status: Wrong Answer

Solution code:
import re
import sys
import copy

def get_count(s):
  count02=0
  pattern = "a+|b+|c+|d+|e+|f+|g+|h+|i+|j+|k+|l+|m+|n+|o+|p+|q+|r+|s+|t+|u+|v+|w+|x+"
  result = re.finditer(pattern, s)
  for res in result:
    l = res.end()-res.start()
    if l>1:
      #切り捨て
      count02 += l//2
  return count02

s = input()
k = int(input())
count01=0
count02=0

#case01
if len(s)==1:
  print(k//2)
  sys.exit(0)
  
#case02
tmp = s[0]
flag=True
for i in range(len(s)):
  if s[i]!=tmp:
    flag=False
if flag==True:
  print((len(s)*k)//2)
  sys.exit(0)

#case03
s_org = copy.deepcopy(s)
s2 = copy.deepcopy(s)
if s[0]==s[-1]:
  tmp = s[0]

  offset01=0
  while s[0]==tmp:
    offset01+=1
    s = s[1:] + s[0]
    #print(s)
  offset02=0
  while s2[-1]==tmp:
    offset02+=1
    s2 = s2[-1] + s2[:-1]
    #print(s2)
  k -= 1
  print(get_count(s_org[:offset01]) + get_count(s)*k +get_count(s_org[offset01:]))
  sys.exit(0)

  
#case04
print(get_count(s)*k)

Submission status: Wrong Answer

Solution code:
# 入力                                                                                   
S = input()
K = int(input())

# kindリストに同じ文字が続くものごとに区切ったものを格納                                 
kind = []
back = "0"
len_S = len(S)
i = 0
for moji in S:
    if i == 0:
        back = moji
        i += 1
        continue
    if moji == back[-1]:
        back = moji + back
        if i == len_S-1:
            kind.append(back)
    else:
        kind.append(back)
        back = moji
    i += 1

score = 0
if len(S) == 1:
  	print(K//2)
# 最初の文字と最後の文字が異なる時                                                       
elif S[0] != S[-1]:
    for word in kind:
        len_word = len(word)
        score += len_word // 2
    print(score*K)
# 最初の文字と最後の文字が一緒の時                                                       
else:
    if len(set(kind)) == 1:
        print((len(kind[0])//2)*K)
    else:
        # 最初と最後がつながることでできる文字列の長さ                                   
        len_sg = len(kind[0] + kind[-1])
        for word in kind[1:-1]:
            len_word = len(word)
            score += len_word // 2
        print(score*K + (len_sg//2)*(K-1) + (len(kind[0]))//2 + (len(kind[-1]))//2 )

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
s.append('-1')
ran = []
counter = 0
while counter < len(s)-1:
    j = counter +1
    while s[counter] == s[j]:
        j += 1
    ran.append([s[counter], j - counter])
    counter = j
counter = 0
result = 0
if k >= 0:
    if len(ran) >= 2:
        if ran[0][0] == ran[-1][0]:
            result += ((ran[0][1]+ran[-1][1])//2)*(k-1)
            if ran[-1][1] != 1:
                result += ran[-1][1]//2
            if ran[0][1] != 1:
                result += ran[0][1]//2
        else:
            result += (ran[0][1]//2 + ran[-1][1]//2)*k
    else:
        if ran[0][1]%2 == 0:
            result += (ran[0][1]//2)*k
        else:
            result += ((ran[0][1]*2))*(k//2) + ran[0][1]*(k%2)
    for  i in range(1, len(ran)-1):
        result += ((ran[i][1])//2)*k
    print(result)

Submission status: Wrong Answer

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

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

count = 0
    
ngo = 1
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        ngo+=1
    else:
        count += ngo//2
        ngo=1
count += ngo//2

ngongo = 0
while ngongo < len(s) and s[len(s)-ngongo-1] == s[-1]:
    ngongo+=1

ngongongo = 0
while ngongongo < len(s) and s[ngongongo] == s[0]:
    ngongongo += 1
    
f = 0
if ngongo %2 == 1 and ngongongo %2 == 1 and s[0] == s[-1]:
    count += 1
    f += 1

print(count*k-f)

Submission status: Wrong Answer

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

n = len(s)
cnt = 1
arr = []

# 配列全体のループ
for i in range(n):
    try:
        if s[i] == s[i+1]: cnt += 1
        else: arr.append(cnt) ; cnt = 1
    except: arr.append(cnt)

if len(arr) == 1 : ans = (len(s)*k)%2
else:
    if s[0] == s[n-1] : arr[0],arr[len(arr)-1] = 0,arr[0]+arr[len(arr)-1]
    ans = 0
    for i in range(len(arr)): ans += arr[i]//2
    ans *= k
    if arr[0]==0 and arr[len(arr)-1]%2==0 : ans -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
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()
tc=1
ac=0
p=s[0]
for i in range(1,n):
    if s[i]==p:
        tc+=1
    else:
        ac+=tc//2
        tc=1
        p=s[i]
else:
    ac+=tc//2
ans=ac*k
if s[0]==s[-1]:
    if s[0]!=s[1] and s[-1]!=s[-2]:
        ans+=k-1
    else:
        ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
ls = [str for str in S]
if len(set(ls))==1:
  cnt=len(S)*K//2
  print(cnt)
  exit()

if not ls[0]==ls[-1]:
  i=0
  cnt=0
  while i<len(S)-1:
    if ls[i+1]==ls[i]:
      ls[i+1]="0"
      cnt+=1
      i+=1
    else:
      i+=1
    ans=cnt*K
else:
  j=0
  atama=0
  while j<len(S):
    if ls[j+1]==ls[j]:
      j+=1
      atama+=1
    else:
      break
  k=0
  oshiri=0
  while k<len(S):
    if ls[-k-2]==ls[-k-1]:
      k+=1
      oshiri+=1
    else:
      break
  E=ls[atama+1:]+ls[:atama]
  i=0
  cnt=0
  while i<len(S)-1:
    if ls[i+1]==ls[i]:
      ls[i+1]="0"
      cnt+=1
      i+=1
    else:
      i+=1
  ans=cnt*K
  if (atama+1)*(oshiri+1)%2==1:
    ans-=1
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
N = len(S)
cnt = 0
flg1 = False
flg2 = False
flg3 = False
flg4 = False
for i in range(N-1):
    if S[i] != S[i+1]:
        flg3 = True
        break
if (flg3 == False) and N % 2 == 1:
    print((N*K)//2)
    exit()
if S[0] == S[N-1]:
    cnt += 1
    flg1 = True
if S[N-2] == S[N-1]:
    flg4 = True
for i in range(N-1):
    if flg2:
        flg2 = False
        continue
    if (i == N - 2) and flg1:
        break
    if S[i] == S[i+1]:
        cnt += 1
        flg2 = True
# print(cnt)
if flg1 and flg4 == False:
    print(cnt*K-1)
else:
    print(cnt*K)

Submission status: Wrong Answer

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

count=0

isOdd = False
if K%2 != 0: isOdd = True

if K==1:
	for i in range(len(S)-1):
		if S[i] == S[i+1]:
			count += 1

else:
	S2 = S+S

	for i in range(len(S2)-1):
		if S2[i] == S2[i+1]:
			S2[i+1] = '?'
			count += 1
	
	if isOdd:
		K -= 1
	
	count *= K//2

	if S2[0] == S2[-1]:
		count += (K/2)-1

	if isOdd:
		S.insert(0, S2[-1])

		for i in range(len(S)-1):
			if S[i] == S[i+1]:
				S[i+1] = '?'
				count += 1

print(int(count))

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0
answer = 0
prev = ""
for index,sss in enumerate(s):
    if prev == "" or sss == prev:
        count+=1
        if count == 2: 
            answer+=1
            count=0
            s[index] = "　"
    else:
        count=1
    prev = sss

if s[0] == s[-1]:
    print(answer*k+k-1)
else:
    print(answer*k)

Submission status: Wrong Answer

Solution code:

# import sys
# input = sys.stdin.readline
from collections import Counter
import string


def main():
    # while True:
        S = input()
        K = int(input())

        count_1st = 0
        S1 = list(S * 1)
        for i in range(len(S) - 1):
            if S1[i] == S1[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break
                    else:
                        if c != S1[i-1] and c != S1[i+1]:
                            count_1st += 1
                            S1[i+1] = c
                            break

        count_2nd = 0
        S2 = list(S * 2)
        for i in range(len(S) * 2 - 1):
            if S2[i] == S2[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break
                    else:
                        if c != S2[i-1] and c != S2[i+1]:
                            count_2nd += 1
                            S2[i+1] = c
                            break

        count_3rd = 0
        S3 = list(S * 3)
        for i in range(len(S) * 3 - 1):
            if S3[i] == S3[i+1]:
                for c in string.ascii_lowercase:
                    if i == 0:
                        if c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break
                    else:
                        if c != S3[i-1] and c != S3[i+1]:
                            count_3rd += 1
                            S3[i+1] = c
                            break

        # count_k = 0
        # SK = list(S * K)
        # for i in range(len(S) * K - 1):
        #     if SK[i] == SK[i+1]:
        #         for c in string.ascii_lowercase:
        #             if i == 0:
        #                 if c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break
        #             else:
        #                 if c != SK[i-1] and c != SK[i+1]:
        #                     count_k += 1
        #                     SK[i+1] = c
        #                     break

        d = count_3rd - count_2nd
        # print(K, d, count_1st)
        # print(count_1st, count_2nd, count_3rd)
        print(count_1st * 2 + d * (K-2))
        # print(S1)
        # print(S2)
        # print(S3 == SK)
        # print(SK)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
cnt = 1
ans = 0
a1 = 0
a2 = 0
for i in range(1, len(s)):
    if s[i] == s[i - 1]:
        cnt += 1
    else:
        a1 += cnt // 2
        cnt = 1
a1 += (cnt // 2)
cnt = 1

s2 = s + s
for i in range(1, len(s2)):
    if s2[i] == s2[i - 1]:
        cnt += 1
    else:
        a2 += cnt // 2
        cnt = 1
a2 += (cnt // 2)
r = a2 - a1
ans = a1 + (k - 1) * r
print(ans)

Submission status: Wrong Answer

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

n = len(s)
s2 = s + s

renzoku = False
taisho = 0
aida = False
for i in range(2*n-1):
    if s2[i] == s2[i+1]:
        if not renzoku:
            renzoku = True
            start_i = i
            count = 0
        count += 1

    elif renzoku:
        renzoku = False
        end_i = i
        count += 1
        #print(start_i, end_i)
        if count % 2 == 1:
            indexs = list(range(start_i+1, end_i, 2))
            taisho += len(indexs)
            #print(indexs)
            if (n-1 in indexs) or (n in indexs):
                aida = True
        elif count % 2 == 0:
            indexs1 = list(range(start_i, end_i, 2))
            indexs2 = list(range(start_i+1, end_i, 2))
            taisho += len(indexs1)
            #print(indexs1)
            all_indexs = indexs1 + indexs2
            if (n-1 in all_indexs) or (n in all_indexs):
                aida = True

last_renzoku = False
if renzoku:
    last_renzoku = True
    renzoku = False
    end_i = 2*n - 1
    count += 1
    if count % 2 == 1:
        indexs = list(range(start_i+1, end_i, 2))
        taisho += len(indexs)
        #print(indexs)
        if (n-1 in indexs) or (n in indexs):
            aida = True
    elif count % 2 == 0:
        indexs1 = list(range(start_i, end_i, 2))
        indexs2 = list(range(start_i+1, end_i, 2))
        taisho += len(indexs1)
        #print(indexs1)
        all_indexs = indexs1 + indexs2
        if (n-1 in all_indexs) or (n in all_indexs):
            aida = True


#if True:
if not aida:
    assert taisho % 2 == 0
    hitotsu = taisho // 2
    out = hitotsu * k
else:
    if taisho % 2 == 0: 
        hitotsu = taisho // 2
        out = hitotsu * k
    else:
        if not last_renzoku:
            hitotsu = (taisho+1) // 2
            out = hitotsu * k - 1
        else:
            x = (taisho - 1) // 2
            if k % 2 == 0:
                out = taisho * (k // 2)
            else:
                out = taisho * ((k-1) // 2) + x


print(out)

Submission status: Wrong Answer

Solution code:
from itertools import groupby

S = input()
N = int(input())

if len(S) == 1:
    print(N-1)
    exit()

if len(S) == 2 and S[0] == S[1]:
    print(N)
    exit()

check = S
check_num = sum(len(list(v))//2 for k, v in groupby(check))
check2 = S * 2
check2_num = sum(len(list(v))//2 for k, v in groupby(check2))
check3 = S * 3
check3_num = sum(len(list(v))//2 for k, v in groupby(check3))

if N % 2 == 0:
    print(check2_num * N//2)
else:
    print(check_num + (check3_num-check_num)*(N//2))

#ans = check_num*N + (check2_num-2*check_num)*(N-1)
#ans3 = check_num*3 + (check2_num-2*check_num)*(3-1)
#if check3_num == ans3:
#    print(ans)
#else:
#    print(round(N/2)*check_num + N//2*(check2_num-check_num))

Submission status: Wrong Answer

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) //2)*K)
            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
                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: Wrong Answer

Solution code:
s=input()
k=int(input())
l=list(s)
r=list(s*2)
x=0
y=0
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: Wrong Answer

Solution code:
s = list(input())
k = int(input())
a = '0'
cnt = 0
flag = 1
out = 0

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

    if s[0] == s[len(s)-1]:
        cnt += 1
        flag = -1

out = cnt * k

if flag == -1:
    out -= 1
if len(s) >= 2:
	print(out)
if len(s) == 1:
  print(k//2)

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_last_replace = K - 1
    else:
        first_last_replace = 0

    out = count * K + first_last_replace

print(out)

Submission status: Wrong Answer

Solution code:
s = list(input())
k = int(input())
count = 0
if len(s) == 1:
    count = int(k / 2)
elif k >= 3:
    t1 = s * 5
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
    if count % 5 == 0:
        count = (count / 5) * k
    else:
        count = ((count + 2) / 5) * k - 2
else:
    t1 = s * k
    for i in range(len(t1) - 1):
        if t1[i] == t1[i + 1]:
            t1.pop(i + 1)
            t1.insert(i + 1, "")
            count += 1
print(int(count))

Submission status: Wrong Answer

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

s += s[0]
s = s[::-1]
count = 0
for i in range(1, len(s) - 1):
    if s[i] == s[i + 1]:
        count += 1
        s = s[:i + 1] + 'X' + s[i + 2:]
total = count * k
print(total)

Submission status: Wrong Answer

Solution code:
t=""
s=input()
c=0
k=int(input())
a=False
for i in range(len(s)-1):
  if s[0]==s[len(s)-1] and s[len(s)-2]!=s[len(s)-1]:
    c+=1
    a=True
    continue
  elif s[0]==s[len(s)-1] and s[len(s)-2]==s[len(s)-1]:
    a=True
    continue
  elif i<=len(s)-3 and s[i]==s[i+1] and s[i+1]==s[i+2]:
    c+=1
    i+=1
    continue
  elif s[i]==s[i+1]:
    c+=1
    continue
if a==True:
     c=c*k-1
     print(c)
else:
     c=c*k
     print(c)

Submission status: Wrong Answer

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

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


l = 0
while (True):
    if s[l] != s[l+1]:
        break
    if l == len(s)-2:
        print(int((len(s) * k) / 2))
        sys.exit()
    l += 1



j = 0
counta = 1
while (j<=len(s)-2):
    if s[j] == s[j+1]:
        counta += 1
        j += 1
        continue
    else:
        break

print(counta)
if counta % 2 == 0:
    flaga = 0
else:
    flaga = 1
        


countb = 0
for i in range(len(s)-1):
    if s[i] == s[i+1]:
        s = s[:i+1] + "0" + s[i+2:]
        countb += 1


flagb = 0
if s[len(s)-1] == s[0]:
    if flaga == 1:
        countb += 1
        flagb = 1
        
    


if flagb == 0:
    print(countb * k)
else:
    print(countb * k - 1)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    S = stdin.readline().rstrip()
    K = int(stdin.readline().rstrip())
    count = 0
    skip = False
    for i, c in enumerate(S[:-1]):
        if skip:
            skip = False
            continue
        nc = S[i + 1]
        if c == nc:
            count += 1
            skip = True
    count = count * K
    if 3 <= len(S):
        if (S[0] == S[-1]) and (S[-1] != S[-2]):
            count += K - 1
    print(count)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import random
import string

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

prev_c = S[0]
num_changes = 0
for i, c in enumerate(S[1:]):
    if c == prev_c:
        num_changes += 1
        prev_c = 'replaced'
    else:
        prev_c = c

total_changes = 0
if S[0] == S[-1]:
    num_changes2 = 0
    for i, c in enumerate(S[1:]):
        if c == prev_c:
            num_changes2 += 1
            prev_c = 'replaced'
        else:
            prev_c = c
    total_changes = num_changes + num_changes2 * (K-1)
else:
    total_changes = num_changes * K

print(total_changes)

Submission status: Wrong Answer

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

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

for i in range(len(S)):
  if SR[i]==SR[i+1]:
    SR[i]=1
    cnt+=1

if (SR[-2]==1) and (SR[-3]==0):
  ans = cnt*K-1
else:
  ans = cnt*K
print(ans)

Submission status: Wrong Answer

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

x = [0]*len(s)
for i in range(len(s)-1):
    if (s[i] == s[i+1]) and x[i] == 0:
        x[i+1] = 1


y = [0]*len(s)
if (k > 1) and (s[0] == s[-1]) and (x[-1] == 0):
    y[0] = 1
    for i in range(len(s)-1):
        if (s[i] == s[i+1]) and y[i] == 0:
            y[i+1] = 1
else:
    y = x

z = k//2
print(sum(x)*(k-z) + sum(y)*z)

Submission status: Wrong Answer

Solution code:

if __name__ == "__main__":
    Input = [input() for i in range(2)]
    # = list(map(int,Input[0].split()))
    S = str(Input[0])
    K = int(Input[1])

    n = len(S)
    ren = 1
    bef = S[0]
    cont = []
    if n == 1:
        cont = [1]
    for s in range(1,n):
        if bef == S[s]:
            ren += 1
            bef = S[s]
            if s == n-1:
                cont.append(ren)                
        else:
            cont.append(ren)
            ren = 1
            bef = S[s]

    ans = (sum([c//2 for c in cont]))*K
    if n ==1:
        ans += K//2
    elif S[0] == S[-1]:
        if cont[0]%2==1 and cont[-1]%2==1:
            ans += K-1
    print(ans)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

T = S[:]

if len(T) == 1:
    print(0)
    exit()

count = 0
for i in range(1, len(T)):
    if T[i-1] == T[i]:
        count += 1
        T[i] = '-'

count *= K

T = S[:]

if T[0] == T[-1]:
    if T[0] != T[1] and T[-1] != T[-2]:
        count += (K-1)

print(count)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = s+s[0]
ans = 0
i = 1
while True:
    c = 0
    while True:
        if i>=len(t):
            break
        elif t[i-1] == t[i]:
            i +=2
            c+= 1
        else:
            break
    ans += c
    if i>=len(t):
        break
    i+=1
if len(set(t[-3:]))==1 and len(s)>3:
    print(ans*k-1)
else:
    print(ans*k)

Submission status: Wrong Answer

Solution code:
a = list(input())
b = int(input())
c = len(a)
sum = 0
sum1 = 1
sum2 = 0
for i in range(0,c-1):
  if a[i]==a[i+1]:
    a[i+1]=" "
    sum+=1
if a[0]==a[c-1]:
  sum1=2
  sum2=1
      
print((sum*b*sum1)-sum2)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

S = input()
K = int(input().split()[0])
c = 0
is_change_tail = False

unit_c = 0

# 連続する文字が奇数かどうか
# 先頭と末尾が異なる場合はシンプル
# 先頭と末尾が同じ場合は、
is_seq = False
count_seq_start = 1  # 先頭からの連続文字数
count_seq_end = 1  # 末尾の連続文字数

# 先頭からの連続文字数を数える
for i in range(1, len(S)):
    if S[i] == S[0]:
        count_seq_start += 1
    else:
        break

# 末尾からの連続文字数を数える
count_seq_end = 1  # 末尾の連続文字数

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

count_seq = 1
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        if not is_seq:
            is_seq = True
        count_seq += 1
    else:
        if is_seq:
            is_seq = False
            # count_seqの偶奇判定
            if count_seq % 2 == 0:
                unit_c += count_seq // 2
            else:
                unit_c += (count_seq - 1) // 2
            count_seq = 1

# 末尾まで連続していた時の処理
if is_seq:
    is_seq = False
    # count_seqの偶奇判定
    if count_seq % 2 == 0:
        unit_c += count_seq // 2
    else:
        unit_c += (count_seq - 1) // 2

c += unit_c * K

if S[0] == S[-1] and count_seq_start % 2 == 1 and count_seq_end % 2 == 1:
    c += K - 1

ans = c
print(ans)

Submission status: Wrong Answer

Solution code:
S = input()
N = int(input())
st = []
c = 0
s = S+S
j = [True]*len(s)
for i in range(0,len(s)-1):
  if s[i]==s[i+1] and j[i]==True:
    #print(i,s[i],s[i+1])
    #print(s)
    j[i+1]=False
    c+=1
a = N*c//2
k=0
if s[0]==s[-1] and j[-1]==True:
  k += (N-1)//2
print(a+k)

Submission status: Wrong Answer

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

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

flag = False
if s[0] == s[-1]:
#    print("flag")
    flag = True
else:
    pass

#sの中だけ考える
count = 0
dp = ""
for i in range(len(s)):
    if dp == s[i]:
        count += 1
        dp = ""
    else:
        dp = s[i]
ans = count * k


if flag:
    try:
        #もし後ろ３つが同じなら
#        print("here")
        if len(set([s[-1],s[-2],s[-3]])) == 1:
            if len(set([s[0],s[1]])) == 1:
                pass
                if len(set([s[0],s[1],s[2]])) == 1:
                    ans = ans + (k-1)
                else:
                    pass
            else:
                ans = ans + (k-1)
    except IndexError:
        pass
else:
    pass

print(ans)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
n = len(s)
p = ['']*(4*n)
for i in range(n):
    p[i] = s[i]
    p[i+n] = s[i]
    p[i+2*n] = s[i]
    p[i+3*n] = s[i]

a = [0,0,0,0]

for i in range(1,4*n):
    if p[i] == p[i-1]:
        a[i//n] += 1
        p[i] = ''
print((k//4)*sum(a) + sum(a[:k%4]))