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:
S = input()
K = int(input())
S_list = [i for i in S]
single_S = 0
if K == 1:
    for i in range(len(S_list) - 1):
        if S_list[i] == S_list[i + 1]:
            single_S += 1
            S_list[i + 1] = "A"
    print(single_S)
elif K % 2 == 0:
    S_list2 = S_list + S_list
    for i in range(len(S_list2) - 1):
        if S_list2[i] == S_list2[i + 1]:
            single_S += 1
            S_list2[i + 1] = "A"
    print(single_S * K // 2)
elif K % 2 == 1:
    S_list2 = S_list + S_list
    for i in range(len(S_list2) - 1):
        if S_list2[i] == S_list2[i + 1]:
            single_S += 1
            S_list2[i + 1] = "A"
    ans = single_S * (K - 1) // 2
    for i in range(len(S_list) - 1):
        if S_list[i] == S_list[i + 1]:
            ans += 1
            S_list[i + 1] = "A"
    print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
K=int(input())
Length=len(S)
change=0

#S内の文字連続をcountする
count=0
compare_flag=True

for i in range(1,Length):
  if compare_flag==False:
    compare_flag=True
  else:
    if S[i]==S[i-1]:
      count+=1
      compare_flag=False
    else:
      pass

if Length>=2:
  if S[Length-1]==S[Length-2]:
    pass
  else:
    if S[Length-1]==S[0]:
      change+=1

ans=(K-1)*change+K*count
print(ans)

Submission status: Wrong Answer

Solution code:
s=input()
l=len(s)
k=int(input())
ans=0
head=0
body=-1
tail=l-1
for i in range(l-1):
  if s[i]==s[i+1]:
    head=i+1
  else:
    break
for i in range(l-1,0,-1):
  if s[i]==s[i-1]:
    tail=i-1
  else:
    break
head_chars=head+1
tail_chars=l-tail
if head>tail:
  print((l*k)//2)
elif head+1==tail:
  print((head_chars//2+tail_chars//2)*k)
else:
  body_text=s[head+1:tail]
  ans=0
  cnt=1
  for i in range(1,len(body_text)):
    if body_text[i]==body_text[i-1]:
      cnt+=1
    else:
      ans+=cnt//2
      cnt=1
  ans+=cnt//2
  ans*=k
  ans+=head_chars//2
  ans+=tail_chars//2
  headtail_text=s[tail:]+s[:head+1]
  tmp=0
  cnt=1
  for i in range(1,len(headtail_text)):
    if headtail_text[i]==headtail_text[i-1]:
      cnt+=1
    else:
      tmp+=cnt//2
      cnt=1
  tmp+=cnt//2
  ans+=tmp*(k-1)
  print(ans)

Submission status: Wrong Answer

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

# sが1種類の文字の繰り返しの場合
if len(set(s)) == 1:
    print((len(s) * k) // 2)

# 複数種の場合
else :
    # 同じ文字の連続する箇所をカウント
    before_mozi = ''
    count_l = [0]
    same_f = False
    for mozi in s:
        if mozi == before_mozi:
            if not same_f:
                count_l.append(1)
            count_l[-1] += 1
            same_f = True
        else:
            same_f = False
        before_mozi = mozi

    result = sum([x // 2 for x in count_l]) * k

    # 先端と末端の文字が同じで，
    # かつお互いが2文字以上連続していないとき
    if s[0] == s[-1] and (s[0] != s[1] or s[-1] != s[-2]):
        result += k-1

    print(result)

Submission status: Wrong Answer

Solution code:
s=list(input())
c=0
k=int(input())
n=len(s)
l=set(s)

# flag=False
if s[0]==s[-1]:
    s[-1]=-1
    c+=1
    # flag=True

def ccount(v, l):  # v: finding val, l: list
    ans=[]
    nv = 0
    i = 0
    n = len(l)
    while i < n:
        if l[i]!=v:
            i += 1
        else:
            while i < n and l[i] == v:
                nv += 1
                i += 1
        if nv>1: ans.append(nv)
        nv = 0
    return ans

for i in l:
    for v in ccount(i,s):
        if v%2==0:
            c+=v//2
        else:
            c+=(v-1)//2

c*=k

# if flag:
#     c-=1

print(c)

Submission status: Wrong Answer

Solution code:
s = input()
k = int(input())
t = ''
i = 0
count =0



if s[0] != s[len(s)-1]:
	while i < len(s)-2:
		if s[i] == s[i+1]:
			if i == len(s)-2:
				count +=1
				i += 1
			if s[i+1] == s[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
	else:
		i += 1
	if i == len(s)-2:
		if s[i] == s[i+1]:
			count +=1
	print(count*k)


elif s[0] == s[len(s)-1]:
	for i in range(k):
		t += s
	while i < len(t)-2:
		if t[i] == t[i+1]:
			if i == len(t)-2:
				count +=1
				i += 1
			if t[i+1] == t[i+2]:
				count += 1
				i += 2
			else: 
				count += 1
				i += 1
		else:
			i += 1
	if i == len(t)-2:
		if t[i] == t[i+1]:
			count +=1

	print(count)

Submission status: Wrong Answer

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
s = input()		# 文字列の入力  abcdefg...
k = int(input())						# 5


ans=0

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

if len(s)==2:
	if s[0]==s[-1]: ans=k
	print(ans)
	exit(0)

count=0
ans_s=0
for i in range(len(s)-1):
	
	if s[i]!=s[i+1]:
		count=0
		continue
	
	if count%2==0:
		ans_s+=1
	
	count+=1
	p(s[i], s[i+1], count, ans_s)

if s[0]==s[-1]:
	if count%2==0:
		ans+=k-1

ans+=(ans_s * k)
print(ans)

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 numpy as np

S = raw_input()
K = int(raw_input())

S_twice = S + S

A = np.full(len(S_twice),0)
A_twice = np.full(len(S_twice),0)

count = 0
for i in range(0, len(S)-1):
    if S[i] == S[i-1]:
        count = count + 1
    else:
        A[count] = A[count] + 1
        count = 0

count = 0
for i in range(0, len(S_twice)-1):
    if S_twice[i] == S_twice[i-1]:
        count = count + 1
    else:
        A_twice[count] = A_twice[count] + 1
        count = 0

if count == len(S_twice)-1:
    print len(S)*K/2
    exit()

A_k = A + (K-1)*(A_twice-A)

ans = 0

for i in range(A_k.shape[0]):
    ans = ans + (i+1)/2*A_k[i]

print ans

Submission status: Wrong Answer

Solution code:
s = list(x for x in input())
s2 = s + s
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)
else:
  print((count2-count)*(k-1) + count)

Submission status: Wrong Answer

Solution code:
s=input()
d= [c  for c in s]
n=int(input())
l=len(d)
count=0

for i in range(l-2):
  if i==0 and d[0]==d[l-1]:
    d[0]="1"
    count+=1
  if d[i]==d[i+1]:
    if d[i]!=d[i+2]:
       d[i]="1"
       count+=1
    elif d[i]==d[i+2]:
      d[i+1]="1"
      count+=1
if d[l-2]==d[l-1]:
  count+=1
print(count*n)

Submission status: Wrong Answer

Solution code:
def ConnectionAndDisconnection(S, K):
	total = 0
	altS = ''
	if S[0] == S[-1]:
		char = S[0]
		S1 = ''
		for i in range(len(S)):
			if S[i] != char:
				S1 = S[i:]
				break

		if S1 == '':
			rslt = int(len(S) * K / 2)
			print(rslt)
			exit
		else:
			rev1 = S1[::-1]
			for i in range(len(rev1)):
				if rev1[i] != char:
					rev2 = rev1[i:]
					break
			altS = rev2[::-1]
			total += int((len(S) - len(altS)) / 2)
	else:
		altS = S

	if altS != '':
		T = altS + '_'
		tmpcnt = 0
		for i in range(len(T) - 1):
			if T[i] == T[i+1]:
				tmpcnt += 1
			else:
				total += int((tmpcnt+1) / 2)
				tmpcnt = 0
		print(total * K)

S = str(input())
K = int(input())
ConnectionAndDisconnection(S, K)

Submission status: Wrong Answer

Solution code:
import copy
def main():
    S = input()
    K = int(input())
    a = list(S)
    b = copy.copy(a)
    cnt = 0
    if len(set(a))==1 and len(a) != 1:
        cnt = (len(a)*K)//2
        print(cnt)
    elif len(a) ==1:
        cnt += K//2
        print(cnt)
    else:
        cnt_2 = 0
        for i in range(len(a)-1):
            if a[i+1] == a[i]:
                a[i+1] = str(i)
                cnt += 1
        cnt *= K
        if a[0] == a[-1] and len(a) > 1:
            cnt += K-1
        for j in range(len(b)-1):
            if b[j+1] == b[j]:
                b[j] = str(j)
                cnt_2 += 1
        cnt_2 *= K
        if b[0] == b[-1] and len(b) > 1:
            cnt_2 += K-1

        print(min(cnt,cnt_2))

    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
lists=[]
flag=False
for i in range(len(s)):
    lists.append(s[i]) 
lists.append("good")
if len(s)>=2:
    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
                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]:
            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:
    if k%2==0:
        print(k//2)
    else:
        print((k-1)//2)

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()
pre = ""
count = 0
first = S[0]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if pre != S[i] and first == S[i]:
            flag = True
    if pre != S[i]:
        pre = S[i]

    elif pre == S[i]:
        count += 1
        pre = ""


count *= int(K)
if flag:
    count = count + 1*int(K) -1
print(count)

Submission status: Wrong Answer

Solution code:
# A - Connection and Disconnection
s = input()
k = (int)(input())

beforeStr = ''
count = 0


if len(s) == 1:
    print(k)
else:
    firstChar = s[0]
    endChar_ = s[len(s) - 2]
    endChar = s[len(s) -1]

    list_s = list(s)

    if endChar != endChar_ and firstChar == endChar:
        count += 1
        list_s[0] = 'Z'

    for i in range(len(s)):

        if beforeStr == list_s[i]:
            count += 1
            list_s[i] = 'Z'
        beforeStr = list_s[i]

    print(count * k)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import math

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

cnt = 0

tttt = []
ttt = [[k, len(list(g))] for k, g in groupby(s)]
for tt in ttt:
  for i,v in enumerate(tt):
    tttt.append(tt[1])

for tt in tttt:
    if tt % 2 == 0:
      cnt += tt / 2
    else:
      cnt += math.ceil(tt//2)

print(int(cnt/2*k))

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:
#!/usr/bin/env python3
#AGC39 A

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
k = I()
n = len(s)
res = 0
i = 0
lst = deque()
while i < n:
    tmp = s[i]
    i += 1
    cnt = 1
    while i < n and tmp == s[i]:
        cnt += 1
        i += 1
    lst.append(cnt)
flg = True
if len(lst) > 2:
    if s[0] == s[-1]:
        x = lst[0]
        y = lst[-1]
        res = 0
        m = len(lst)
        for i in range(1,m-1):
            res += lst[i]//2
        res *= k
        res += x//2*(k-1) + y//2*(k-1)
        res += (x+y)//2*(k-1)
        print(res)
    else:
        res = 0
        for i in lst:
            res += i//2
        print(res*k)
else:
    print(n*k//2)

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:
                ans += 1
                last_char_change = True
    
    if i == (len(s) // 2) - 1:
        single_cost = ans
    
ans *= (n // 2)
if n % 2 == 1:
    ans += single_cost

if last_char_change:
    ans -= 1

print(ans)

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

print(answer)

Submission status: Wrong Answer

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

double = s*2
arr = [-1]
for i in range(len(double)):
    if arr[-1] != i-1:
        if double[i] == double[i-1]:
            arr.append(i)
arr.remove(-1)
if k%2 == 0:
    count = len(arr) * k // 2
else:
    count = len(arr) * (k // 2) + len(arr) // 2
print(count)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
#ts=time.time()
#sys.setrecursionlimit(10**6)
#input=sys.stdin.readline
show_flg=False
show_flg=True

def solve2(b,k):
    if len(b)*k<100:#0**5:
        return naive(b,k)
    
    rt=naive(b,1)*k
    if b[0]==b[-1] and (len(b)>=3 and (b[0]==[1] or b[-2]==[-1])):
        rt+=-1
    return rt
    
def solve(b,k):
    '''
    b=StoI()
    a=b+[b[0]]
    k=I()
    '''
    if len(b)*k<10:#0**5:
        return naive(b,k)
    else:
        naive(b,3)
        
    a=b+[b[0]]
    n=len(a)
    
    
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
    
    ans=t*k
    if n==2:
        ans=k//2
    if len(Counter(b))==1:
        ans=(k*len(b))//2
    if b[0]==b[-1]:
        ans=(naive(b,4))*(k//4)+(naive(b,k%4) if k%4!=0else 0)-1
    #print(ans)
    return ans

def naive(b,k):
    a=b*k
    n=len(a)
    
    p=a[0]
    t=0
    for i in range(n-1):
        if p==a[i+1]:
            t+=1
            p=-1
        else:
            p=a[i+1]
            
    #print()
    return t


b=StoI()
k=I()
print(solve2(b,k))

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

total = score * K
if last_chk == 1: total = total - 1
print(total)


# 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 = input()
K = int(input())
a = []
ans = 0
count = 0
for i in range(len(S)-1):
    if S[i] == S[i+1] :
        count += 1
    else:
        if count!=0:
            ans += (count+1)//2
            a.append(count+1)
            count = 0
if count !=0:
    ans += (count+1)//2 
ans = ans*K
a.append(0)
if (count+1)%2==1 and a[0]%2 == 1:
    ans+=K-1
 
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
S_=S+"A"
K=int(input())
cnt=0
if len(S)==1:
    ans=K//2
elif len(S)==2:
    ans=K
else:
    import queue
    q=queue.Queue()
    for i in S_:
        if not q.empty():
            if not i == q.get():
                n=1
                while not q.empty():
                    q.get()
                    n+=1
                cnt+=n//2
                q.put(i)
            else:
                q.put(i)
                q.put(i)#出した分戻さないと           
        else:
            q.put(i)
        #最後、入れっぱなしで終わってしまっていた
    if S[0]==S[-1]:
        if S[-2]==S[-1]:
            ans=cnt*K
        else:
            ans=((cnt+1)*K)-1
    else:
        ans=cnt*K
        
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

def connect_cnt(S):
	cnt_tmp = 1
	cnt = []

	for i in range(len(S) - 1):

		if S[i] == S[i+1]:
			cnt_tmp += 1
		else:
			cnt.append(cnt_tmp)
			cnt_tmp = 1

		if i == len(S)-2:
			cnt.append(cnt_tmp)

	connect_cnt = sum(np.array(cnt) // 2)
	return connect_cnt

if K == 1:
	print(connect_cnt(S))
	exit(0)

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

	connect_cnt = connect_cnt(S)
	ans = connect_cnt * K
	print(ans)

else:

	S_dash = S + S[0]
	connect_cnt_Sdash = connect_cnt(S_dash)
	connect_cnt_S = connect_cnt(S)

	ans = connect_cnt_Sdash * (K - 1) + connect_cnt_S
	print(ans)

Submission status: Wrong Answer

Solution code:
#coding: utf-8
strings = str(input())
k = int(input())
s = strings + strings
# print(s)
count = 0

for i in range(len(s)-1):
	# print('strj'+str(j))
	if s[i] == s[i+1]:
		# print(str('s[i]=') + s[i] + str(' s[i+1]=') + s[i+1])
		count += 1
		# s[i] = str(i)
		s = s[:i+1] + str('A') + s[i+2:]
		# print(s)

# print(count)
ans = count*k//2

# if k % 2 == 0:
# 	count = count * k // 2
# else:
# 	print(count*k//2)
# 	print(count//2)
# 	count = count * k // 2 - (count // 2)
if strings[0] == strings[len(strings)-1] and k % 2 == 1:
	ans += (k - 1) // 2

print(ans)

Submission status: Wrong Answer

Solution code:
S = list(input())
N = len(S)
K = int(input())
T = S.copy()
S += S
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"
ans += cnt
if T[0] == T[-1] and S[N - 1] != "-1":
    cnt2 = 1
    T[0] = "-1"
    for i in range(1, N):
        if T[i] == T[i - 1]:
            cnt2 += 1
            T[i] = "-1"
    ans += cnt2 * (K - 1)
else:
    ans += cnt * (K - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
S=list(input())
K=int(input())
c=0
cc=0
if len(S)==1:
  if K==1:
    print(0)
    exit()
  else:
    print(K//2)
    exit()
for i in range(1,len(S)):
  if S[i]==S[i-1]:
    S[i]="!"
    c+=1
for i in range(len(S)-1):
  if S[i]==S[i+1]:
    S[i]="!"
    cc+=1
if S[0]!=S[-1]:
  print(c*K)
else:
  print(min(c*K+K-1,cc*K+K-1))

Submission status: Wrong Answer

Solution code:
s = input()
k = input()
w = s * 2

s = list(s)
w = list(w)
ans_s = 0
ans_w = 0
for i in range(0, len(s)-1):
    if s[i] == s[i+1]:
        ans_s += 1
        if (i+2 <= len(s)-1) and (s[i+1] == s[i+2]):
            s[i+1] = "0"

for i in range(0, len(w)-1):
    if w[i] == w[i+1]:
        ans_w += 1
        w[i+1] = "0"

if (ans_s*2) == ans_w:
    ans_s *= int(k)
else:
    ans_s = ans_s*int(k) + int(k) - 1

print(ans_s)

Submission status: Wrong Answer

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


for s in S:
    if s.isupper():
        exit()
    try:
        float(s)
    except ValueError:
        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()
pre = ""
count = 0
first = S[0]
flag = False
for i in range(len(S)):
    if i == (len(S) - 1):
        if pre != S[i] and first == S[i]:
            flag = True
    if pre != S[i]:
        pre = S[i]

    elif pre == S[i]:
        count += 1
        pre = ""


count *= int(K)
if flag:
    count = count + 1*int(K) -1
print(count)

Submission status: Wrong Answer

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

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    s = lines[0]
    k = int(lines[1])

    return (s, k)


def solve(s, k):

    target_1 = s[0]
    index_1 = 0
    for i in range(len(s)):
        if s[i] != target_1:
            index_1 = i
            break
    
    target_3 = s[-1]
    index_3 = len(s) - 1
    for i in range(len(s) - 1, -1, -1):
        if s[i] != target_3:
            index_3 = i
            break
    
    s_head = s[0:index_1]
    s_body = s[index_1:index_3 + 1]
    s_tail = s[index_3 + 1:]
    if debug:
        log("s_head=%s" % s_head)
        log("s_body=%s" % s_body)
        log("s_tail=%s" % s_tail)

    total_replace_count = 0

    last_c = None
    count = 1
    replace_count = 0
    for c in s_body:
        if c == last_c:
            count = count + 1    
        else:
            replace_count = replace_count + count // 2
            count = 1
        last_c = c
    replace_count = replace_count + count // 2

    total_replace_count = total_replace_count + replace_count * k
    if debug:
        log("total_replace_count=%d" % total_replace_count)

    if s[0] == s[-1]:
        s_h_t = s_head + s_tail
        total_replace_count = total_replace_count + len(s_h_t) // 2 * (k-1)
        if debug:
            log("total_replace_count=%d" % total_replace_count)

    
    total_replace_count = total_replace_count + len(s_head) // 2
    if debug:
        log("total_replace_count=%d" % total_replace_count)
    total_replace_count = total_replace_count + len(s_tail) // 2
    if debug:
        log("total_replace_count=%d" % total_replace_count)

    return total_replace_count


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

cnt,ans=0,0
if len(S)==1:
  print(k//2)
  exit()
for i in range(1,len(S)):
  if S[i-1]==S[i]:
    cnt+=1
  else:
    cnt+=1
    ans+=cnt//2
    cnt=0
ans+=(cnt+1)//2
flag=0
for i in range(len(S)-1,-1,-1):
  if S[i]==S[i-1]:
    flag+=1
if len(S)!=2 and S[0]==S[-1]:
  if flag%2==0:
    print(ans*k)
  else:
    print(ans+(ans+1)*(k-1))
else:
  print(ans*k)

Submission status: Wrong Answer

Solution code:
#coding: utf-8

S=list(input())
K=int(input())
n=len(S)*K

if len(S)==1:	
	print(n//2 if n%2==0 else (n-1)//2)
	exit()

c = 0

if S[0] == S[-1] and K>=2:
	c+=K-1
	S[0] = "!"


for i in range(1,len(S)):
	if S[i]==S[i-1]:
		c+=K
		S[i] = "?"
#		print(S)

	
#print(S*K)
print(c)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

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

count = 0
s1 = ''
s2 = ''
s3 = ''
prev_rewrite = False
for s2 in S:
    if s1 == s2 and not prev_rewrite:
        code = ord(s2) + 1
        s3 = chr(code) if code <= ord('z') else 'a'
        count += 1
        prev_rewrite = True
    else:
        s3 = s2
        prev_rewrite = False

    s1 = s3

ans = count * K

if s1 == S[0] and not prev_rewrite:
    ans += K // 2

print(ans)

Submission status: Wrong Answer

Solution code:
s = raw_input()
k = raw_input()
c = 0
i = 0
out = 0
while( i < len(s)-1 ):
  if s[i] == s[i+1]:
    c += 1
    i += 1
  i += 1
if i == len(s)-1 and s[0] == s[len(s)-1]:
  out = out + 1
out = out + int(k)*c
print(out)

Submission status: Wrong Answer

Solution code:
s=input()
k=int(input())
l=len(s)
s_list=[s[i] for i in range(l)]
ser_list=[]
ser_list.append(int(1))
for i in range(l-1):
    if s_list[i]==s_list[i+1]:
        ser_list[-1]=ser_list[-1]+1
    else:
        ser_list.append(1)
if s_list[0]!=s_list[l-1]:
    divide = [x//2 for x in ser_list]
    print(sum(divide)*k)
elif s_list.count(s_list[0])==len(s_list):
    print(l*k//2)
else:
    ser_list[0]=ser_list[0]+ser_list[-1]
    ser_list.pop(-1)
    a= sum([x//2 for x in ser_list])
    b=sum([x//2 for x in ser_list])
    print(a*(k-1)+b)

Submission status: Wrong Answer

Solution code:
S = input()
K = int(input())
count = 0
count2 = 1
count3 = 1
flag = 0
flag2 = 0
if len(S) == 1:
    print(K // 2)
else:
    for i in range(len(S)-1):
        if S[i] == S[i+1] and flag == 0:
            count += 1
            flag = 1
        else:
            flag = 0
        if S[i] != S[i+1]:
            flag2 = 1
    num = count * K
    if flag2 == 0:
        print(len(S) * K //2)
    else:

        if S[0] == S[-1]:
            if len(S) == 2:
                print(num)
            else:
                flag3 = 0
                for i in range(len(S)-1):
                    if S[i] == S[i+1] and flag3 == 0:
                        count2 += 1
                        flag3 = 1
                flag3 = 0
                for i in range(len(S)-1, 0, -1):
                    if S[i] == S[i-1] and flag3 == 0:
                        count3 += 1
                        flag3 = 1
                if count2 % 2 != 0 and count3 % 2 != 0:
                    print(num + K - 1)
                else:
                    print(num)
        else:
            print(num)

Submission status: Wrong Answer

Solution code:
def cad(s, k):
    if len(s) == 1:
        return k // 2
    l = list(s)

    count = 0
    new_l = l.copy()

    for i, c in enumerate(s[1:]):

        if c == new_l[i]:
            new_l[i + 1] = 0
            count += 1

    result = count * k

    if new_l[0] == new_l[-1] and s[0] != s[1]:
        result += k - 1
    return result


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

Submission status: Wrong Answer

Solution code:
S=list(str(input()))
s=set(S)
K=int(input())
num=0
i=0
lst=[]
def dp(i,lst):
  if i==len(S)-1:
    return lst
  if S[i]!=S[i+1]:
    return dp(i+1,lst)
  j=i+1
  while j<=len(S)-1  and S[i]==S[j]:
    j+=1
  lst.append(list(S[i:j]))
  return dp(j-1,lst)
if len(s)>=2:
  L=dp(i,lst)
  for i in range(len(L)):
    num+=(len(L[i])//2)
  print((num)*K)
else:
  print((len(S)*K)//2)

Submission status: Wrong Answer

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


result = 0

# S内の重複
dup_c = 0
c_list = ['XXXXX']
for s in S:
    if c_list[0] == s:
        dup_c += 1
        c_list = ['XXXXX']
    else:
        c_list[0] = s
result = dup_c * K

# 間の重複
if len(S) > 1:
#    if S[0] == S[len(S)-1]:
#        if S[1] != S[0] and S[len(S)-2] != S[0]:
#            result += K - 1
    #後ろから同じ文字をカウント
    Sr = S[::-1]
    same_s = 0
    for i in range(len(Sr)):
        if Sr[0] == Sr[i]:
            same_s += 1
        else:
            break
    
    if same_s != 1 and same_s % 2 == 1:
        result += K - 1

else:
    result += K//2

print(result)

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("b")
    print(result)